1

I have a custom function in functions.php that excludes specific category from the category list:

function incomplete_cat_list() {
      $first_time = 1;
      foreach((get_the_category()) as $category) {
       if ($category->cat_name != 'Category Name') {
         if ($first_time == 1) {
          echo '<a class="cat-list" href="' . get_category_link( 
          $category->term_id ) . '" title="' . sprintf( __( "See all %s" ), 
          $category->name ) . '" ' . '>'  . $category->name.'</a>';
          $first_time = 0;
         } 
      else {
           }
       }
     }
}

I would like to use it in related posts foreach loop, but I'm not sure how to do it... Here's the code for displaying related posts by tags or category:

        <?php

            $max_articles = 4;  // How many articles to display

            echo '<div id="related-articles" class="relatedposts"><h3>Related articles</h3>';

            $cnt = 0;

            $article_tags = get_the_tags();
            $tags_string = '';
            if ($article_tags) {
                foreach ($article_tags as $article_tag) {
                    $tags_string .= $article_tag->slug . ',';
                }
            }

            $tag_related_posts = get_posts('exclude=' . $post->ID . '&numberposts=' . $max_articles . '&tag=' . $tags_string);

            if ($tag_related_posts) {
                foreach ($tag_related_posts as $related_post) {
                    $cnt++; 
                    echo '<div class="child-' . $cnt . '">';
                            echo '<a href="' . get_permalink($related_post->ID) . '">';
                            echo get_the_post_thumbnail($related_post->ID);
                            echo $related_post->post_title . '</a>';
                            echo incomplete_cat_list;
                        echo '</div>';
                }
            }


            // Only if there's not enough tag related articles,
            // we add some from the same category

            if ($cnt < $max_articles) {

                $article_categories = get_the_category($post->ID);
                $category_string = '';
                foreach($article_categories as $category) { 
                    $category_string .= $category->cat_ID . ',';
                }

                $cat_related_posts = get_posts('exclude=' . $post->ID . '&numberposts=' . $max_articles . '&category=' . $category_string);

                if ($cat_related_posts) {
                    foreach ($cat_related_posts as $related_post) {
                        $cnt++; 
                        if ($cnt > $max_articles) break;
                        echo '<div class="child-' . $cnt . '">';
                            echo '<a href="' . get_permalink($related_post->ID) . '">';
                            echo get_the_post_thumbnail($related_post->ID);
                            echo $related_post->post_title . '</a>';
                            echo incomplete_cat_list;
                        echo '</div>';
                    }
                }
            }

            echo '</div>';

        ?>

How to call the custom function from functions.php inside above loop?

2 Answers 2

1

First you need to pass the post_id to this function to use the post_id inside the get_the_category($post_id)

function incomplete_cat_list($post_id = '') {
    global $post;
    $post_id = ($post_id) ? $post_id : $post->ID;
    $first_time = 1;
    $categories = get_the_category($post_id);
    foreach($categories as $category) {
        if ($category->cat_name != 'Category Name') {
            if ($first_time == 1) {
                echo '<a class="cat-list" href="' . get_category_link( 
                $category->term_id ) . '" title="' . sprintf( __( "See all %s" ), 
                $category->name ) . '" ' . '>'  . $category->name.'</a>';
                $first_time = 0;
            }
            else {
            }
        }
    }
}

This function already echo so you don't need to echo it again. So you should call it like this.

incomplete_cat_list($related_post->ID);

Another thing if you want to stop the loop after you get the first category instead of using this $first_time variable just use break; to stop the loop.

3
  • Hi @Shibi, thanks for your answer! I changed the code the way you suggested and the categories showed up. But this function is used in multiple places - on main blog page, categories pages, archives etc, not only on the single blog post, and when I changed the code as you suggested, the category name disappeared from all the pages except single blog post. Should I create two separate functions for this? One for the blog post with your code and the second one for other places with the previous code?
    – Anda
    Dec 17, 2017 at 15:57
  • @Anda Check the function now. if you don't pass the $post_id to the function the post_id will be the global $post->ID
    – Shibi
    Dec 17, 2017 at 16:02
  • excellent, now it works perfectly :)
    – Anda
    Dec 17, 2017 at 16:22
0

@Shibi, for your tip about using break - this is how the function should look?

// Exclude category name from carousel
function incomplete_cat_list($post_id = '') {
global $post;
$post_id = ($post_id) ? $post_id : $post->ID;
$categories = get_the_category($post_id);
foreach($categories as $category) {
    if ($category->cat_name != 'Category name') {
            echo '<a class="cat-list" href="' . 
            get_category_link( 
            $category->term_id ) . '" title="' . sprintf( __( "See all in %s" ), 
            $category->name ) . '" ' . '>'  . $category->name.'</a>';
            break;
    }
}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.