Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
What happens if you take out the parameter 'cat=39' ? –  kevin Jul 21 '11 at 16:05
    
the cat=39 is used to display 5 posts from that category in the list. I need the title of that list to link to a category archive list. It does go to the category archive page, but nothing is there. it's just blank. –  chris_s Jul 21 '11 at 16:11
    
add comment

1 Answer 1

up vote 3 down vote accepted

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.