如何从带有特定标记的类别中查询帖子?例如,如果我访问“localhost/tag/tutorial“存档页面将显示所有类别中带有教程标记的所有帖子。
但这一观点将按类别划分。例如:
类别1
第1类贴子1带教程标签第2类贴子1带教程标签第2类
第2类贴子2带有教程标签,第2类贴子10带有教程标签,注意:贴子显示在每个类别中,标签相同。
我有一个代码,显示每个类别中的最新帖子
<?php
$categories = get_categories();
foreach($categories as $category) { ?>
<div class="CategoryPost">
<h2><?php echo $category->name; ?></h2>
<ul>
<?php
$post_args = array(
\'category\' => $category->term_id,
\'numberposts\' => 9999,
\'orderby\'=> \'post_date\',
\'order\' => \'ASC\',
\'post_type\' => \'post\'
);
$posts = get_posts($post_args);
foreach($posts as $post) { ?>
<li id="post-<?php the_ID(); ?>"><span><?php the_time(\'d M Y\') ?></span> <i class="Seperate">»</i> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php } ?>
</ul>
</div>
<?php }
wp_reset_postdata(); ?>
上面的代码将显示具有此外观的每个类别中的最新帖子。
类别1名称
第1类岗位1第1类岗位2第2类岗位名称
第1类职位1第2类职位3名称
我想制作一个标签档案,与该模板一起使用,并且即使帖子具有相同的标签,帖子也会显示在每个类别中。
最合适的回答,由SO网友:Sid 整理而成
这将有助于在标签归档页面上获得分类帖子。让我知道。
<?php
$current_tag = single_tag_title( $prefix = \'\', $display = false );
$categories = get_categories( array(\'hide_empty\' => TRUE) );
foreach($categories as $category) { ?>
<?php
$args=array(
\'posts_per_page\' => -1,
\'tag\' => $current_tag,
\'cat\' => $category->term_id,
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
?>
<div class="CategoryPost">
<h2><?php echo "Tag: " . $current_tag; ?></h2>
<h2><?php echo "Category: " . $category->name; ?></h2>
<ul>
<?php
echo \'<ul>\';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo \'<li>Post title: \' . get_the_title() . \'</li>\';
}
echo \'</ul></ul></div><br>\';
}
?>
<?php }
wp_reset_postdata();
SO网友:Sid
请试试这个,如果有帮助请告诉我。
<?php
$categories = get_categories( array(\'hide_empty\' => TRUE) );
foreach($categories as $category) { ?>
<?php
$args=array(
\'posts_per_page\' => -1,
\'tag\' => \'tutorial\',
\'cat\' => $category->term_id,
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
?>
<div class="CategoryPost">
<h2><?php echo $category->name; ?></h2>
<ul>
<?php
echo \'<ul>\';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo \'<li>\' . get_the_title() . \'</li>\';
}
echo \'</ul></ul></div>\';
}
?>
<?php }
wp_reset_postdata(); ?>
顺便说一下,我刚刚打印了帖子的标题。如果这对您有效,请添加其他参数以打印。
谢谢
编辑:显示所有标记。
<?php
$tags_array = get_tags();
$categories = get_categories( array(\'hide_empty\' => TRUE) );
foreach($tags_array as $tag){
foreach($categories as $category) { ?>
<?php
$args=array(
\'posts_per_page\' => -1,
\'tag_id\' => $tag->term_id,
\'cat\' => $category->term_id,
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
?>
<div class="CategoryPost">
<h2><?php echo "Tag: " . $tag->name; ?></h2>
<h2><?php echo "Category: " . $category->name; ?></h2>
<ul>
<?php
echo \'<ul>\';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo \'<li>Post title: \' . get_the_title() . \'</li>\';
}
echo \'</ul></ul></div><br>\';
}
?>
<?php } }
wp_reset_postdata(); ?>
SO网友:BlueSuiter
@这里的GaroPpo是我向您建议的经过调整的代码。我希望这将完全满足您的要求。
<?php
$tag_id = array(12, 13, 14);
$taxonomy = \'testimonial-category\';
$postType = \'testimonial\';
$terms = get_terms([\'taxonomy\' => $taxonomy, \'orderby\' => \'term_id\', \'hide_empty\' => true]);
?> <div class="add-accordion"> <?php
foreach($terms as $term){
if($term->parent == 0){
echo \'<h3>\' . $term->name . \'</h3>\';
continue;
} $posts = get_posts(array(\'post_status\' =>\'publish\',\'post_type\' => $postType, \'tag__in\' => $tag_id,
array(
\'taxonomy\' => $taxonomy,
\'field\' => \'term_id\',
\'terms\' => $term->term_id,
),));
?>
<div class="add-accordion">
<h3><?php echo $term->name ?></h3>
<div class="add-accordion">
<?php foreach($posts as $post){ ?>
<h3><?php echo $post->post_title ?></h3>
<div class="">
<?php echo get_the_content($post->ID) ?>
</div>
<?php } ?>
</div>
</div>
<?php
}
?>