• Resolved tathros

    (@tathros)


    What a great plug-in! Thank you so much!

    I only have one question: I added the code to my function.php of my Child Theme and that works perfectly. However, there is one post, where I do not want to show the “Related Posts” section. How would I do that? Thanks!

    add_filter( 'the_content', 'add_related_posts_after_post_content' );
    function add_related_posts_after_post_content( $content ) {
     
        //check if it's a single post page.
        if ( is_single() ) {
     
            // check if we're inside the main loop
            if ( in_the_loop() && is_main_query() ) {
     
                // add your own attributes here (between the brackets [ ... ])
                $shortcode = '[related_posts_by_tax posts_per_page="4" related_posts_by_tax title="Related Articles"]';
     
                // add the shortcode after the content
                $content = $content . $shortcode;
            }
        }
     
        return $content;
    }

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

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author keesiemeijer

    (@keesiemeijer)

    Hi tathros

    You can use the WP function is_single() with a post ID to exclude the post.

    Something like this

    
    add_filter( 'the_content', 'add_related_posts_after_post_content' );
    function add_related_posts_after_post_content( $content ) {
     
        //check if it's a single post page.
        if ( is_single() && ! is_single( 9419 ) ) {
     
            // check if we're inside the main loop
            if ( in_the_loop() && is_main_query() ) {
     
                // add your own attributes here (between the brackets [ ... ])
                $shortcode = '[related_posts_by_tax posts_per_page="4" related_posts_by_tax title="Related Articles"]';
     
                // add the shortcode after the content
                $content = $content . $shortcode;
            }
        }
     
        return $content;
    }
    

    See this line. I’ve added the post ID 9419 so the related posts are not displayed there.

    
    if ( is_single() && ! is_single( 9419 ) ) {
    
    Thread Starter tathros

    (@tathros)

    Works like a charm! Thanks a lot!

    Thread Starter tathros

    (@tathros)

    If I may ask one follow up question: If I have another post that I want to exclude as well, how would I modify this line?

    if ( is_single() && ! is_single( 9419 ) ) {
    

    Thanks a lot!

    Plugin Author keesiemeijer

    (@keesiemeijer)

    Try it with something like this:

    
    add_filter( 'the_content', 'add_related_posts_after_post_content' );
    function add_related_posts_after_post_content( $content ) {
    
        // Exclude related posts
        $exclude = array(
            9419,
            9420,
            9421,
        );
     
        //check if it's a single post page.
        if ( is_single() && ! is_single( $exclude ) ) {
     
            // check if we're inside the main loop
            if ( in_the_loop() && is_main_query() ) {
     
                // add your own attributes here (between the brackets [ ... ])
                $shortcode = '[related_posts_by_tax posts_per_page="4" related_posts_by_tax title="Related Articles"]';
     
                // add the shortcode after the content
                $content = $content . $shortcode;
            }
        }
     
        return $content;
    }

    It doesn’t show related posts in single post 9419, 9420 and 9421. You can add post ids separated by a comma.

    Thread Starter tathros

    (@tathros)

    Hello and thanks for your answer. However, what I meant was how to exclude the display of Related Posts on another post. In your first answer (which worked great), this line

    if ( is_single() && ! is_single( 9419 ) ) {
    

    caused Related Posts not be shown on post #9419. My question now was, if I wanted to excluded the display of RP on another post, how could I specify that?

    Thanks much!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘H2 exclude Related Posts on a given post page when defined in Child Theme’ is closed to new replies.