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="https://wordpress.stackexchange.com/questions/23679/<?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.