WordPress.org

Ready to get started?Download WordPress

Forums

Edit code for related posts (5 posts)

  1. jumust
    Member
    Posted 1 year ago #

    Hi,
    I have a theme with its own related posts code at the end of each post. It gets related posts by tags, but actually I would like to EXCLUDE 2 TAGS.

    in single.php I found the code:

    <?php $tags = get_the_tags(); ?>
    				<?php if($tags): ?>
    				<?php $related = get_related_posts($post->ID, $tags); ?>
    				<?php if($related->have_posts() && $related->found_posts >= 1 && get_option('reedwan_related') == 'On'): ?>

    It should not be too difficult but can't figure it out. Any help?

    Thanks

  2. Andrew Bartel
    Member
    Posted 1 year ago #

    get_related_posts() is not a native WordPress function, you'd have to find it and modify it somehow, preferrably through a child theme or overwriting it if the developer made the function pluggable.

  3. jumust
    Member
    Posted 1 year ago #

    Thanks, I found out that function. I think we might do something around the tag__in

    http://pastebin.com/1t5B87Vt

  4. Andrew Bartel
    Member
    Posted 1 year ago #

    Ok, change this:

    if($tags) {
            foreach($tags as $tag) {
                   $tagsA[] = $tag->term_id;
            }
    }

    To:

    if($tags) {
            foreach($tags as $tag) {
                   if($tag->term_id != 'Tag to exclude 1' && $tag->term_id != 'Tag to exclude 2') $tagsA[] = $tag->term_id;
            }
    }

    I really do recommend extending this function, writing your own and calling that or otherwise not hacking it directly.

  5. jumust
    Member
    Posted 1 year ago #

    Thanks,
    but it doesn't look like it's excluding those tags...

    Example: There are 2 posts with only one same tag (spotlight), all others tags are different. But still they are showing in the related posts.

    Maybe I'm doing something wrong...

    Thanks

Topic Closed

This topic has been closed to new replies.

About this Topic