最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

loop - the_title() shows title of the first post instead of the page title?

programmeradmin2浏览0评论

I'm occasionally facing this problem and not sure what causes it, any ideas?

<?php get_header(); ?>
    <div class="content" role="main">
            <h1><?php the_title(); ?></h1>
            <?php get_template_part( 'loop', 'index' ); ?>
    </div>
<?php get_footer(); ?>

For a normal page the_title returns "Page title".

But for a blog (using loop.php as in index file above) it returns "First posts title".

Any ideas?

I'm occasionally facing this problem and not sure what causes it, any ideas?

<?php get_header(); ?>
    <div class="content" role="main">
            <h1><?php the_title(); ?></h1>
            <?php get_template_part( 'loop', 'index' ); ?>
    </div>
<?php get_footer(); ?>

For a normal page the_title returns "Page title".

But for a blog (using loop.php as in index file above) it returns "First posts title".

Any ideas?

Share Improve this question asked Mar 12, 2013 at 19:30 WordpressorWordpressor 5,05119 gold badges70 silver badges102 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 10

the_title is a Loop tag. It "Displays or returns the title of the current post" and it is supposed to be used inside the Loop, not outside of it.

What you are doing-- calling it outside the Loop-- is not quite correct, and you are getting inconsistent results. What happens is this:

  1. The $post variable gets set to the first post in the Loop very early in the page load. For some pages, like single posts pages which have only one post in the Loop, that means $post is "the page you are on"-- more or less. For archive pages it is the first page in the Loop. You can put var_dump($post); before wp_head runs and see that the variable is already set.
  2. Tags like the_title use that global $post variable. You have to trace it through a couple of functions to get there but eventually you get to the get_post function and you can see in the source that this is the case. In this case the chain is the_title->get_the_title->get_post

So, what you are describing is exactly what should be happening. You are using the tag incorrectly. It sometimes works the way you want only because of a quirk of the code. It isn't really supposed to work that way, or so it seems to me.

If you want "the page you are on" you will sometimes need to use get_queried_object, but watch it since it returns different kinds of data depending on the page, and for some pages returns NULL. In other cases you are better off using the is_home, is_category, etc. conditionals than you are depending on the query data like that. In fact, in most cases you are better off with those conditionals, or just with a call to wp_title as toscho suggests, but the context you are trying to use this in makes me wonder if that is correct. Plus, the output of wp_title can be, and frequently is, manipulated by plugins (SEO plugins for example), which may or may not be what you want.

On archive pages – blog, year, category and so on – use wp_title() to get the page title. the_title() relies on data of a single post.

On archive pages – blog, year, category and so on – use single_cat_title() to get the page title. Hope this helps you

Just hit the same thing today, but I found a great solution:

  <?php 
    if ( is_front_page() ) {
      echo "Home";
    } else if (is_single()) { // POST, not needed for my site
      echo "";
    } else if (is_page()) { // PAGE
      echo get_the_title();
    } else if(is_category()) {
      $arr = get_the_category();
      if ( ! empty( $arr ) ) {
        echo esc_html( $arr[0]->name );   
      }
    }

One thing that can help you is going to

WordPress Dashboard>Settings>Reading>Posts Page

Worked in my case.

发布评论

评论列表(0)

  1. 暂无评论