• Resolved averixus

    (@averixus)


    I want to display the page title (“Articles”) as a header on the main posts page. I can’t figure out how to add this. The template for the main posts page is meant to be home.php, but this file doesn’t seem to exist in storefront. Where is the main posts page template, and how do I edit it? Or is there a hook or a simpler way to add the title to the main posts page without overriding the entire template?

    The page I need help with: [log in to see the link]

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hi @averixus,

    One way you could do that is by creating a child theme and copying the index.php file over to it. Then edit the index.php file in your child theme. Look for this line:

    
    <main id="main" class="site-main" role="main">
    

    Right below that, add this:

    
    <?php if ( is_home() ) { ?>
    	<h1 class="page-title">
    		<?php echo get_the_title( get_option('page_for_posts', true) ); ?>
    	</h1>
    <?php } ?>
    

    On my test site, that added the page title for the posts page to the main blog archive. See if it works for you.

    Cheers

    Thread Starter averixus

    (@averixus)

    Thank you, that worked! I was too busy assuming I needed to use a buried storefront hook, it didn’t occur to me that I could just add it to a raw template.

    I am looking to do the same thing (add the page title “Articles” to my main posts page). However, when I copy the index.php code from my main theme to an index.php file that I created in my child theme, there is no line that says “<main id=”main” class=”site-main” role=”main”>.” Adding the second code provided underneath the existing code does not have the desired effect. Am I looking at the wrong index.php file? Or is there some other mistake I’m making? Sorry if this is a dumb question, I am very much a beginner!

    Hi @kristelbelle248 I see you said this almost two weeks ago but if you haven’t found a solution, I don’t have such a line in my theme either, so the trick is to find the right place to put it, which will change from theme to theme.

    For my theme, the trick was to place it right under the line that says:
    <div id="content" role="main">

    So if your theme has that, try it! If not, look for the first use of the word “main,” and put it just under that. Good luck!

    Hi @3sonsdevelopment, thanks for that, I was looking all over for this! : )

    Would this work in functions.php? If so, how would the code change? I tried putting it in there like this

    if ( is_home() ) { ?>
        <h1 class="entry-title">
            <?php echo get_the_title( get_option('page_for_posts', true) ); ?>
        </h1>
    <?php } ?>

    But it had no effect. Note that at the top of my file there a <?php, some code, and a ?>, and I pasted the above right after the previous code and right before the closing ?>. Also, I changed page-title to entry-title, because that’s the terminology that my theme uses.

    I’m asking this question because I’m thinking that if the theme were to ever update it’s index.php file, I’d miss out on those changes because my index.php would over-rule it.

    Hi @davidsortof,

    Thanks for the reply! I actually did figure out how to make this work later. I created an index.php file in my Astra child theme folder (creating a child theme helps prevent the problem of having the code erased when the theme updates). Then I copied all of the code from my Astra index.php file into the new index.php file in my child theme. Then below where it said “get_header(); ?>” I inserted this line of code:

    <div align=”center”><span style=”font-size: 60px;”> </br> <?php single_post_title(); ?></div>

    You can then change the alignment and font size to suit your needs. This finally fixed my problem! I hope this might be helpful to anyone else trying to make this work!

    @kristelbelle248 Nice! Glad you found something that worked, and now we’ve got two solutions on this thread! For me, 3 Son’s code did the trick.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to show page title on posts page’ is closed to new replies.