WordPress conditional statement for front-page or

2019-08-31 01:42发布

I need help with the templating in wordpress.

On the front-page I want a custom static header, while all the other pages should use the title for their page instead. Also, if the page is not the front-page I want to add some custom HTML next to the title.

What is the best way to do this?

1条回答
你好瞎i
2楼-- · 2019-08-31 02:12

You can do it like this, including the following in header.php (I've just added some example content);

        <?php 
             if ( is_front_page() ) { 
                echo '<div id="homestuff">Home stuff in here</div>';
              } elseif (is_page()) { 
                echo '<div id="pagestuff">'. the_title() .'</div>'; 
              } 
        ?>

Take note, there is also a is_home() in Wordpress and it's sometimes easy to get in a bit of a tangle about whether is_front_page() or is_home() is the correct one to use, this stackexchange answer is a good source of info about that;

Whether to use is_front_page() or is_home() in Wordpress

查看更多
登录 后发表回答