• Hi everyone,

    Little problem that I hope someone can help with.

    I’d like to have my page title the same as the most recent entry in my blog.

    I have at present:

    <title><?php bloginfo(‘name’); ?><?php wp_title(); ?> : <?php the_title(); ?></title>

    Which kinda works, but the title has the last blog entry on the page. It makes sense why it does this, but I can’t understand how to get the most recent entry title and use this as my page title whilst still having several entries on my main page.

    Any help, really appreciated.

    TIA

Viewing 3 replies - 1 through 3 (of 3 total)
  • The template tag the_title() is specifically made to run in The Loop section of your template. Placing it anywhere else can lead to weird behavior, as you’ve discovered.

    I recommend using the function get_posts(), like so:

    <title><?php bloginfo("name"); ?><?php wp_title(); ?> :
    <?php $titles = get_posts('numberposts=1'); foreach ($titles as $post) : the_title(); endforeach; ?>
    </title>

    A little on the overkill side, but it’ll do the job.

    Forgot to note that this retrieves the most recent post, period. As for most recent within the context of a specific page, that might be a bit harder to accomplish…

    Thread Starter jmhirst

    (@jmhirst)

    Hi Kafkaesqui, many thanks for that, it’s worked exactly how I wanted it.

    Thanks again, really appreciate it….

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Page title same as most recent post title?’ is closed to new replies.