0

I'm very new to the world of php coding and have been learning basic php including 'if'/ 'else' rules but I've hit a brick wall and no amount of Googling has helped.

I am currently working on a WordPress theme template where the header is pulled dynamically:

        <header class="page-title theme-bg-light text-center gradient py-5">
            <h1 class="heading">
        
                <?php the_title()
                ?>
            
            </h1>
        </header>

But as the blog page is set to be the posts page it shows the most recent post as the header title instead of 'Blog'. I've been tearing my hair out over the last two days trying to do if/then rules for instance:

<?php if (is_nav_menu_item($menu_item_id = 55)){
            echo 'Blog';}
            else get_the_title(the_title());
            ?>

To no avail. All I want is for blog page to be hard-coded as 'Blog' else retrieve the page titles as normal. Sometimes it remains unchanged, or it'll change all the page headers to 'Blog'.

A lot of other code I've seen is completely different to mine and I've tried some other things I read on WordPress' codex but I'm not really sure what I'm doing.

Thanks for your time!

1
  • the_title always refers to the title of the current post, so if you are using a page named Blog to show your list of posts, it'll show Blog, but on individual posts it'll show their individual titles. If you want it to always be "Blog" there's a trivial solution, replace <?php the_title() ?> with Blog, literally select it in the editor, press delete and type the word Blog
    – Tom J Nowell
    Commented Sep 14, 2023 at 14:16

1 Answer 1

0

You can determine whether the blog is displayed using is_home():

<?php
if ( is_home() ) {
    echo 'Blog';
}
else {
    the_title();
}
?>

Be aware that the the_title() function only displays the title of the current post. If you're on any other kind of page, such as the blog, a category archive, a date archive, or search results; the post title is probably not what you want to display in that context. You will want to use some of the many available conditional tags to determine what you're currently viewing, so that you can determine what title to display.

If you look at the template hierarchy you can get an idea of what kinds of pages WordPress will query. The conditional tags will help you find out where in that hierarchy you are.

1
  • Thanks so much! I was so close but no cigar - I will read up the links you sent over now. Thanks again!
    – Yaz
    Commented Sep 14, 2023 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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