Site logo

Archived topic

How could I build related post section like this?

37 replies · Started by kiant123 on August 31, 2021

Viewing posts 1–15 of 38

Hi,

Is there a simple, clean and lightweight way to add related posts (like this https://share.getcloudapp.com/7KuEE8Jl) at the bottom of each post?

I'd only need the related posts to be displayed that are related by the same tag as the current post. Is there a way that a hook can be used for this?

Hi David,

That worked pretty well!

One issue I have found is that the 'related posts' title bumps right up against the end of my content with no space.

Have I placed the code in the best hook position? And do I need to select 'execute shortcodes'?

And do I need to select ‘execute shortcodes’?

If thats the option in the Hook Element then yes :)

Wrap you shortcode in a <div> with a custom class that you can style with CSS e.g

<div class="related-posts">
Shortcode goes here
</div>

then add some CSS:

.related-posts {
    padding: 20px 0;
}

That will add 20px of top and bottom padding to the container.

Hi, I don't think I'm following correctly.

The code at the top:

<div class="related-posts">
Shortcode goes here
</div>

Am I supposed to add this to the hook with the code I got from the previous post? Or is this a new hook?

Then the css code I add to the additional CSS section?

Doh ... ignore that completely.

Just add this CSS to give it some padding:

.wpsp-related-posts1  {
    padding: 20px 0;
}

Thanks! That worked.

Is there a way for the related posts to ignore certain tags?

For instance around half of my posts have two tags, a generic tag on them (used for my own purposes), and the other tag describes the type of content. Is it possible that when the related posts are grabbed it ignores the generic tag?

So in the Code Tom provided, you will see the $args:

$args = array(
    'numberposts' => 5,
    'tag__in' => $tag_ids,
    'post__not_in' => array( get_the_ID() )
);

You should be able to add the tag__not_in arg:

$args = array(
    'numberposts' => 5,
    'tag__in' => $tag_ids,
    'post__not_in' => array( get_the_ID() ),
    'tag__not_in' => array( 21 , 99 )
);

Change the 21 and 99 for IDs of the tags you want to exclude. You can get there IDs by editing the Tag term in the admin area, and checking the browser URL.

Hi,

I seem to be getting strange behaviour on my site when I add this code. When I add the code in the page layout totally changes (i've taken it out for now) as you can see in the screenshot.

Have I added the code incorrectly?

bumping this just in case it hasn't been seen by support.

thanks for the updated code.

I still find that the code isn't working in that it keeps bringing up the tags I am trying to block.

For example, my post should only be bringing up the 'pain product' tag, but I can see that the tags are my 'best products' tag which is the one I am trying to block.

Could you check that I have put in the right code?

Can you double check the ID for the tag_not_in is correct ?

It is correct, assuming that I am supposed to grab the number in the URL as shown in the last screenshots I sent.

Hmmm... can't see why that don't work.
Could try this snippet instead, it will check the ID before doesn't match the one you exclude before creating the ID array:

<?php if ( has_tag() ) { ?>
<div class="wpsp-related-posts1  grid-container">
    <h2>Related Posts</h2>
    <?php
    $tax = 'post_tag';
    $tax_term = '';

    if ( is_single() ) {
        $tags =  get_the_tags();
        $tags_list = [];
        $tag_ids = [];
        foreach ( (array) $tags as $tag) {
            if ( isset( $tag->slug ) ) {
                if ( 162 != $tag->term_id || 220 != $tag->term_id || 161 != $tag->term_id ) {
                    $tags_list[] = $tag->slug;
                    $tag_ids[] = $tag->term_id;
                }
            }
        }

        $args = array(
            'numberposts' => 5,
            'tag__in' => $tag_ids,
            'post__not_in' => array( get_the_ID() )
        );

        $posts = get_posts( $args );

        if ( ! empty( $posts ) && ! empty( $tag_ids ) ) {
            $tax_term = implode( ', ', $tags_list);
        } else {
            $cats =  get_the_category();

            if ( ! empty( $cats ) ) {
                $tax_term = $cats[0]->slug;
            }

            $tax = 'category';
        }
    }

    $list = get_page_by_title( 'related', 'OBJECT', 'wp_show_posts' );
    wpsp_display( $list->ID, 'taxonomy=' . $tax . '&tax_term=' . $tax_term );
    ?>
</div>
<?php } ?>
This archived topic is closed to new replies.