• Resolved goutte_de_mer

    (@marion0)


    I would like to put the page title in the page header (next to the site title) but I can’t find what file(s) I’m supposed to edit or how I should edit them. I found some answers but they’re from 2015 and I don’t think that they’re still relevant. I am just starting to learn Php so it is a bit hard to understand which file does what. Does someone know how to move the page title, what hook should I use and where ?

    Thanks ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi there,

    GeneratePress has multiple hooks including ones in the header.
    The HTML is output by the generate_construct_header() function that is hooked into the generate_header hook. Here is that function:

    https://github.com/tomusborne/generatepress/blob/4895a2e7595bb809075b375201fd735112f41570/inc/structure/header.php#L19-L49

    You can see that has 2 x Hooks inside:

    generate_before_header_content and generate_after_header_content
    And the comments also tell you what callbacks may be attached to those.

    In addition to that and within that same file you will see that some other hooks exist within the other functions that are hooked into the header. Such as generate_before_logo
    https://github.com/tomusborne/generatepress/blob/4895a2e7595bb809075b375201fd735112f41570/inc/structure/header.php#L107

    Now you just need to make a callback to the the_title function.
    And here is an example PHP Snippet for that:

    add_action('generate_after_header_content', function(){
        // check if the template is a single post
        if ( is_single() ) {
            // display the title with custom HTML before after
            the_title( '<h1 class="custom-title">', '</h1>' );
       } 
    });
    Thread Starter goutte_de_mer

    (@marion0)

    Hi thank you for your response. I tried the three hooks that you mentioned and generate_before_logo seems to be the right one for me. It does work but for it to work I had to activate the logo which the person I am managing the website for doesn’t have. Right now I have a random image to which I added display: none;. Is there a better way to make it so that the generate_before_logo hook works without activating the logo ?

    So the generate_before_logo hook only exists if there is a logo.
    I would expect you can get it to a similar position using the generate_after_header_content hook and set its priority to 4 .
    See here for adding the priority to the hook:

    add_action('generate_after_header_content', function(){
        // check if the template is a single post
        if ( is_single() ) {
            // display the title with custom HTML before after
            the_title( '<h1 class="custom-title">', '</h1>' );
       } 
    }, 4);

    If thats not correct – can i see the site and let me know where it needs to go as there are other options

    Thread Starter goutte_de_mer

    (@marion0)

    I didn’t know of this hook but I tried and It worked great thank you ! That’s exactly what I wanted ?? And I didn’t even need to set a priority.

    Glad to be of help!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Move page title to page header’ is closed to new replies.