Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
325 views
in Technique[技术] by (71.8m points)

Custom post type Wordpress query by category

I have the following query which outputs a list of categories for my custom post type called STORIES.

<?php
$taxonomy = 'story-category';
$tax_terms = get_terms($taxonomy);
?>
<?php
foreach ($tax_terms as $tax_term) {
echo '<div class="category-grid-box">
<div class="category-grid-content">' . '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a> </div>
</div>  ';
}
?>

This outputs a list of links for my categories and works great.

My problem is, I don't know how to write the query on the next page which will list all the posts in that chosen category.

So my query lists the categories... - Apples - Oranges - Bananas

If you click on Apples and go to that page, what query do I use to list all of the STORIES that have the category APPLES?

Any ideas? Can't get any solution to work.

I have the following query, but it lists ALL of the categories and ALL of the posts within them. How can I modify it to just show the posts for the page I am on?

<?php
$custom_terms = get_terms('story-category');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => 'stories',
'tax_query' => array(
array(
'taxonomy' => 'story-category',
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<h2>'.$custom_term->name.'</h2>';

while($loop->have_posts()) : $loop->the_post();
echo '<p><a href="'.get_permalink().'">'.get_the_title().'</a></p>';
endwhile;
}
}
?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you can create custom taxonomy template for custom post : LINK


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...