2015-06-17 144 views
0

我有以下查询输出我的自定义帖子类型名为STORIES的类别列表。自定义帖子类型Wordpress查询按类别

<?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> '; 
} 
?> 

这会输出我的类别的链接列表,效果很好。

我的问题是,我不知道如何在下一页上写出查询,该列表将列出所选类别中的所有帖子。

所以我的查询列出类别... - 苹果 - 橙子 - 香蕉

如果你点击了苹果和进入该页面,查询什么我用它来列出所有有故事类别APPLES?

任何想法?无法获得任何解决方案。

我有以下查询,但它列出了所有的类别和他们内的所有帖子。我如何修改它只显示我所在页面的帖子?

<?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; 
} 
} 
?> 

回答

2

您可以自定义后创建自定义分类模板:LINK

+0

这将输出所有类别及其帖子。我只想列出我所在页面的内容。如果我在APPLES页面 - 我只想列出APPLES帖子。而不是每个类别的附加链接。 – lowercase

+0

您是否创建了模板或自定义分类模板..? – vrajesh

+0

根据你的分类标准你的文件名是什么? – vrajesh

1

希望这有助于:

$term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy')); 
$args = array(
    'post_type' => 'post', 
    'tax_query' => array(
     array(
      'taxonomy' => 'story-category', 
      'field' => 'slug', 
      'terms' => $term->slug, 
     ), 
    ), 
); 
$query = new WP_Query($args); 

Class Reference/WP Query

+0

那会,如果你所在的页面是 '鲍勃' 工作。 如何列出我已经在类别中的所有帖子?所以我不必为100页的页面重写查询? – lowercase

+0

刚刚添加了一个新的查询,可能会有所帮助。 – lowercase

+0

当你点击苹果,它将采取苹果分类。页。创建一个单独的分类模板(taxonomy.php)并将代码放入其中。那肯定会在那里工作! –

相关问题