WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] how to get custom-post-type post in same category on a category list (3 posts)

  1. csleh
    Member
    Posted 2 years ago #

    On a custom category template, I've got it listing all sub-categories using get_category.
    What I need to add is to get content from a custom post type, in that same sub-category. Something like:

    - get sub-category (foo)
    - now, is there a post in category-content (a custom post type) in that same (sub)category (foo)?
    - show this sub category title, content from the custom post type, and link to the subcategory page
    - rinse and repeat for each sub-category

  2. csleh
    Member
    Posted 2 years ago #

    Got it!

    [Code moderated as per the Forum Rules. Please use the pastebin]

    Used this on a custom template for this (parent) category.

    I needed more info for a category than description allows. I set up a custom post type to hold the info each category needed, including special fields.

  3. csleh
    Member
    Posted 2 years ago #

    It isn't long.

    <?php $args=array('category_name' => 'mycategoryname' , 'post_type' => 'mycustomposttypename',);
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <h1><?php the_title(); ?></h1>
        <?php  endwhile; }
    wp_reset_query(); ?>

    If you have trouble put the last ?> on it's own line.

Topic Closed

This topic has been closed to new replies.

About this Topic