• Hi Everyone,

    In an effort to silo my site, I am trying to show related posts by category, so that posts in different categories dont link to each other (which is essential to siloing).

    I found the below code. It works to some extend. However, it seems to show only the same posts ll the time (no matter what post page you are on). Also, it also displays a link back to the actual post that you are on as related posts.

    I have very minimal knowledge of coding. What, if anything can be added to this code to…

    1- Make it so that it doesn’t always display the same set of 5 posts.
    2- So that it doesn’t display a link back to the current post?…

    Thank you very much in advance.

    <!-- Start Category Link Code -->
    php if ( is_single() ) { ?>
    <ul>
    php foreach((get_the_category($post->ID)) as $category) { ?>
    php $catVal = $category->cat_ID; }
       $IDOutsideLoop = $post->ID;
       global $post;
    $myposts = get_posts('category='.$catVal.'&numberposts=5&order=ASC');
       foreach ($myposts as $post) { ?>
         php if($IDOutsideLoop == $post->ID) { echo " class=\"current\""; } ?>>
         php the_permalink() ?>">
         </li>
    php }; ?>
    </ul>
    php } ?>
    <!-- End Category Link Code -->
Viewing 4 replies - 1 through 4 (of 4 total)
  • caveat: the code in the above post is unsuitable for copy/paset because all php open tags are incorrect.

    try to add global $post; before the ‘foreach’;

    Thread Starter satrap

    (@satrap)

    @alchymyth
    Thanks for taking time to help.
    Sorry, didn’t know the code would be broken like this.

    I am sorry I am not very familiar with coding. ‘foreach’ is used twice in the code, so I would have to just add the word ‘global $post;’ right before the actual word ‘foreach’, or there is more to it?….

    Thank you.

    try to add it here:

    <!-- Start Category Link Code -->
    <?php if ( is_single() ) {
    global $post; ?>
    <ul>
    <?php foreach((get_the_category($post->ID)) as $category) { ?>
    Thread Starter satrap

    (@satrap)

    Thanks very much for your help again Alchymyth.
    Unfortunately, it still only shows the same set of posts of the specific category on every post of that category.

    However, while waiting for this I did some more digging and was able to find another code that is actually working well.

    I’ll post it here just in case anyone else needs it one day:

    <?php $this_post = $post;$category = get_the_category(); $category = $category[0]; $category = $category->cat_ID;$posts = get_posts('numberposts=5&offset=0&orderby=rand&order=DESC&category='.$category);$count = 0;foreach ( $posts as $post ) {if ( $post->ID == $this_post->ID || $count == 5) {unset($posts[$count]);}else{$count ++;}}?>
    <?php if ( $posts ) : ?>
    <div><ul>
    <?php foreach ( $posts as $post ) : ?>
    <li><a href="<?php the_permalink() ?>">
    <?php if ( get_the_title() ){ the_title(); } else { echo "No Related Post"; } ?></a></li>
    <?php endforeach // $posts as $post ?>
    </ul></div>
    <?php endif // $posts ?>
    <?php $post = $this_post;unset($this_post);?>

    Thank you again for taking time out of your busy day. I really appreciate it.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Related post by category displays same set of posts all the time!?’ is closed to new replies.