Forums

[resolved] How can I show recent posts from same taxonomy as the post currently being viewe (3 posts)

  1. greedz
    Member
    Posted 4 months ago #

    I'm wondering how I can show recent posts from the same taxonomy as the post that's currently being viewed (working with custom post types and custom taxonomies).

    If it was simply a category of a regular post, it would look like this:

    <?php global $post;
    $categories = get_the_category();
    foreach ($categories as $category) :
    ?>
    <h3>More News From This Category</h3>
    <ul>
    <?php
    $posts = get_posts('numberposts=20&category='. $category->term_id);
    foreach($posts as $post) :
    ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
    <li><strong><a href="<?php echo get_category_link($category->term_id);?>" title="View all posts filed under <?php echo $category->name; ?>">ARCHIVE FOR '<?php echo $category->name; ?>' CATEGORY &raquo;</a></strong></li>
    <?php endforeach; ?>
    </ul>

    (found the above searching these forums)
    --
    How would I go about customizing this code for a situation that involves custom post types and custom taxonomies ?

  2. shahar
    Member
    Posted 4 months ago #

    Hi, to get a custom taxonomy use get_the_terms()

    $custom_cats = get_the_terms( $id, 'custom_category_name' );

    http://codex.wordpress.org/Function_Reference/get_the_terms

    You can then get the posts using get_posts:

    $args = array(
     'post_type' => [custom_post_type],
     [custom_cat_type] => [custom_cat_slug]
    );
    
    $custom_posts = get_posts( $args );

    Obviously, you can also use the other parameters for WP_Query too.
    http://codex.wordpress.org/Class_Reference/WP_Query#Parameters

  3. greedz
    Member
    Posted 4 months ago #

    Thank you shahar, that was it. :)

Reply

You must log in to post.

About this Topic