Archive list for custom post type categories

I am using a recipe plugin that uses a custom post type of ‘recipe’. I currently have multiple lists of recipes from each category on a page. I am trying to get the title of each category list to link to an archive list of those posts within each category. This is not working, and I’m not sure if I need to create a new archive template, or if I can do it within this code.

<ul class="subcats-list">
            <h2 class="subcats-title"><a href="<?php echo get_category_link(39); ?>" title="No Cook"><?php echo get_cat_name(39); ?></a></h2>
                <?php
                $recipes = new WP_Query();
                $recipes->query('showposts=5&orderby=rand&cat=39&post_type=recipe');

                while ($recipes->have_posts()) : $recipes->the_post(); ?>
                  <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
                <?php endwhile; ?>

        </ul><!-- subcat -->

NOTE: the title for each category list does currently link to the category archive page, but there are no posts listed. Just a black archive page.

Related posts

Leave a Reply

1 comment

  1. How did you create the link with your category ’39’? Is it a custom taxonomy?

    Here’s the test i just did and it works fine:

    In functions.php i created the custom post type ‘recipe‘:

    add_action( 'init', 'create_recipe' );
    function create_recipe() {
        register_post_type( 'recipe',
            array(
                'labels' => array(
                    'name' => __( 'Recipes' ),
                    'singular_name' => __( 'Recipe' )
                    ),
                'public' => true,
                'show_ui' => true,
                'publicly_queryable' => true,
                'exclude_from_search' => false,
                'menu_position' => 4,
                'query_var' => true,
                'supports' => array( 'title', 'editor')
    
            )
        );
    }
    

    Then i created a custom taxonomy element to get some categories related to this custom post type:

    add_action( 'init', 'register_cate_livro', 0 );
    function register_cate_livro() {
        register_taxonomy(
            'cate-recipe',
            array( 'recipe' ),
            array(
                'public' => true,
                'labels' => array(
                    'name' => __( 'Categorias' ),
                    'singular_name' => __( 'Categoria' )
                ),
            'hierarchical' => true,
            'query_var' => 'cate-livro'
            )
        );
    }
    

    I nammed it ‘cate-recipe‘.

    I wrote 3 news posts under ‘recipe‘ in the admin and created 2 custom categories ‘cate-1‘ and ‘cate-2‘.

    Then using your code, i loaded the posts from the custom post type. It works fine but doesnt take in account the custom taxonomy parameter. So here’s how i made it in the end:

    <ul class="subcats-list">
        <h2 class="subcats-title"><a href="<?php echo get_category_link(12); ?>" title="No Cook"><?php echo get_cat_name(39); ?></a></h2>
            <?php
            $recipes = new WP_Query();
            $args = array(
                'showposts'     =>  5,  
                'orderby'       =>  'rand',
                'post_type'     =>  'recipe',
                'tax_query'     => array(
                    array(
                        'taxonomy' => 'cate-recipe',
                        'field' => 'slug',
                        'terms' => 'cate-1'
                    )
                )
            );
            $recipes->query($args);
    
            while ($recipes->have_posts()) : $recipes->the_post(); ?>
              <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
            <?php endwhile; ?>
    
    </ul><!-- subcat -->
    

    I’m using the ‘tax_query‘ parameter to query the posts from my custom taxonomy ‘cate-recipe‘ with the term ‘cate-1‘, that i created in the admin.

    And it loads just the posts that have this “special category”.

    Hope this helps, let me know if it suits your needs.