7

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?

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.

4

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.

1
  • Thanks for sharing this. In a pinch, this fixed my problem.
    – Pegues
    Jan 16 '19 at 17:00
1

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

0

One thing that can help you is going to

WordPress Dashboard>Settings>Reading>Posts Page

Worked in my case.

-1

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 );   
      }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.