[Resolved] Show sidebar for specific Post templates

Home Forums Support [Resolved] Show sidebar for specific Post templates

Home Forums Support Show sidebar for specific Post templates

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #1586613
    Joshua

    Hi,

    Is it possible to show the sidebar for only specific Post templates? For example, I’ve got three Post templates: Default, Best of, Reviews. All three Post templates are in the default Post post type. If I turn the sidebar on, by default it will turn on for all three Post templates. However, I’d like it to only show on the Reviews Post template. Is this possible?

    Thanks guys!

    #1586667
    Leo
    Staff
    Customer Support

    Hi there,

    Would you be able to use a layout element for that?
    https://docs.generatepress.com/article/layout-element-overview/#sidebar

    Let me know πŸ™‚

    #1586697
    Joshua

    Hi Leo!

    That would be perfect β€” is there a way to add new location rules to the Display Rules on the layout element? It looks like there are only rules for taxonomies, but since the post templates aren’t taxonomies, they don’t appear in the location rules as an option.

    Alternatively, if there isn’t a way to add custom location rules, I could just use a custom taxonomy. When that taxonomy is added to a post, it can turn the sidebar on or off.

    #1587476
    Leo
    Staff
    Customer Support
    #1587598
    Joshua

    Hey Leo,

    I’m not 100% sure I understand how to use that filter. Would I replace the is_author with
    is_page_template and then where it says Tom replace that with the name of my post template?

    #1587608
    Leo
    Staff
    Customer Support
    #1587611
    Joshua

    Okay, so my code looks like this so far:

    add_filter( 'generate_layout_element_display', function( $display, $element_id ) {
        if ( 10 === $element_id && is_page_template( 'single-bank-reviews.php' ) ) {
            $display = true;
        }
    
        return $display;
    }, 10, 2 );

    I want to show a right sidebar on the single Bank Reviews page template. I’ve got this code in my functions.php, but how would I go about using the layout element to actually show the sidebar? Or do I not need to use the layout element anymore because this code bypasses it?

    #1587659
    Leo
    Staff
    Customer Support

    Try choosing content/sidebar in the layout element:
    https://docs.generatepress.com/article/layout-element-overview/#sidebar

    You can ignore the display rules.

    Then add the PHP snippet and make sure to update the 10 in 10 === $element_id

    #1592503
    Joshua

    Thanks Leo!

    I’m going to give this a shot this evening and let you know if it worked. Appreciate your help as always!

    #1592630
    Leo
    Staff
    Customer Support

    Sounds good.

    #1595280
    Joshua

    Hey Leo,

    It worked, thanks so much!

    For anyone else reading this for a solution, keep this in mind:

    I was struggling for a few minutes because it wasn’t working. But I realized that I had to include the directory that the single template was in. So instead of this code:

    add_filter( 'generate_layout_element_display', function( $display, $element_id ) {
        if ( 10 === $element_id && is_page_template( 'single-bank-reviews.php' ) ) {
            $display = true;
        }
    
        return $display;
    }, 10, 2 );

    I used this code:

    add_filter( 'generate_layout_element_display', function( $display, $element_id ) {
        if ( 10 === $element_id && is_page_template( 'single/single-bank-reviews.php' ) ) {
            $display = true;
        }
    
        return $display;
    }, 10, 2 );

    Notice the single/ added? That fixed it! You would of course have to change single/ to be whatever your directory is called.

    Also, the 10 in 10 === $element_id has to be replaced with the ID of the Element you wish to remove. The ID can be found by editing the Element and checking the URL for the “post=”.

    Thanks so much again, Leo!

    #1595288
    Leo
    Staff
    Customer Support

    Awesome note on the single part.

    Thanks for sharing πŸ™‚

Viewing 12 posts - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.