Display the title of the page above the post excerpts


Hello to all. A question for those who can answer: I added the following code in home.php, to display the title of the page above the post excerpts.

//add title at home
add_action( 'beans_header_after_markup', 'beans_child_view_add_title' );
function beans_child_view_add_title() {
?>
  <div class="uk-container uk-container-center">
        <br/><h1 align="center" style="color:#000;"><?php the_title() ?></h1>
         </div>
  <?php
}
// Load Beans document.
beans_load_document();
?>

The bad result is that, it takes the title of the first post published, and not the title of the page. What should I do?

Thanks all. 🙂 Fabio


Hey Fabio,

Because it is the home page which displays the list of posts, the_title() grabs the first post title in the query. The work around is the get the id of the home page assigned in your Settings->Reading and fetch the title using that id. Here is the snippet example:

add_action( 'beans_header_after_markup', 'example_view_add_title' );

function example_view_add_title() {
 $title = get_the_title( get_option( 'page_for_posts' ) );

 ?>
  <div class="uk-container uk-container-center">
    <h1 class="uk-text-center"><?php echo esc_html( $title ); ?></h1>
 </div>
  <?php

}

Happy coding,


It works perfectly, fantastic! Always thanks Thierry.

Write a reply

Login or register to write a reply, it's free!