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?