Wordpress如何根据标签显示所有帖子
Wordpress how to display all posts based on tags
我有 3 个标签:
- 标记 1
- 标签 2
- 标签 3
我需要显示我 custom post type
和指定术语中的所有帖子。他们需要 be sorted by tag
并像这样隐藏未使用的标签:
标记 1
- Post一个
- Post C
标记 2
- Post B
但是显示是这样的:
标记 1
- Post一个
- Post C
标记 2
- Post B
标记 3
- (空)
即使当前没有帖子使用标签 3,也会显示标签...
这是我使用的代码:
// These 3 variables change dynamically depending on page slug.
$cpt = houses;
$tax = type;
$term = small;
$args = array(
'posts_per_page' => -1,
'orderby' => 'name',
'order' => 'ASC',
'post_type' => $cpt,
'tax_query' => array(
array(
'taxonomy' => $tax,
'field' => 'name',
'terms' => $term
)
)
);
$tags = get_tags($args); // Tag 1, Tag 2, Tag 3
$posts = get_posts($args);
foreach($tags as $tag)
{
?>
<h5><?php echo $tag->name.' (Tag)'; ?></h5>
<?php
foreach($posts as $post)
{
if( has_tag( $tag ) )
{
?>
<a href="<?php echo the_permalink();?>"><?php echo the_title().' (Post)';?></a><br>
<?php
}
}
?>
<hr>
<?php
}
?>
感谢您的帮助!
你的代码逻辑应该是这样的:
- get_tags
- 每个标签
- 获得关联 posts(运行
get_posts
这里)
- 只有有 post
- 回显标签的标题
- for each associated post (运行 foreach loop for posts here)
- 回显 post
的标题
所以您可以将 get_posts
查询移到 foreach($tags as $tag)
中并使用 $tag
参数来查看是否有任何 post(s) 与该标签关联。
请注意,在 $posts_args
中我使用了一个名为 tag
的额外参数,它将在 tags foreach loop
中为您解决问题。 tag
参数的值是标签 slug。
另外,通过使用 get_tags
函数,您不必指定 orderby
或 order
,因为 'orderby' 的默认值是 'name' ,'order' 的默认值为 'ASC',因此您无需将它们作为参数传递。
所以整个代码应该是这样的:
$cpt = houses;
$tax = type;
$term = small;
$posts_args = array(
'posts_per_page' => -1,
'orderby' => 'name',
'order' => 'ASC',
'post_type' => $cpt,
'tag' => $tag->slug,
//OR
//'tag_slug__in' => array($tag->slug),
'tax_query' => array(
array(
'taxonomy' => $tax,
'field' => 'name',
'terms' => $term
)
)
);
$tags = get_tags(); // Tag 1, Tag 2, Tag 3
foreach ($tags as $tag) {
$posts = get_posts($posts_args);
if ($posts) { ?>
<h5><?php echo $tag->name . ' (Tag)'; ?></h5>
<?php
foreach ($posts as $post) { ?>
<a href="<?php echo the_permalink(); ?>"><?php echo the_title() . ' (Post)'; ?></a><br>
<?php
}
?>
<hr>
<?php
}
}
?>
这将输出:
Tag 1
Post A
Post C
--------
Tag 2
Post B
如果你能让它工作,请告诉我!
我有 3 个标签:
- 标记 1
- 标签 2
- 标签 3
我需要显示我 custom post type
和指定术语中的所有帖子。他们需要 be sorted by tag
并像这样隐藏未使用的标签:
标记 1
- Post一个
- Post C
- Post一个
标记 2
- Post B
- Post B
但是显示是这样的:
标记 1
- Post一个
- Post C
- Post一个
标记 2
- Post B
- Post B
标记 3
- (空)
即使当前没有帖子使用标签 3,也会显示标签...
这是我使用的代码:
// These 3 variables change dynamically depending on page slug.
$cpt = houses;
$tax = type;
$term = small;
$args = array(
'posts_per_page' => -1,
'orderby' => 'name',
'order' => 'ASC',
'post_type' => $cpt,
'tax_query' => array(
array(
'taxonomy' => $tax,
'field' => 'name',
'terms' => $term
)
)
);
$tags = get_tags($args); // Tag 1, Tag 2, Tag 3
$posts = get_posts($args);
foreach($tags as $tag)
{
?>
<h5><?php echo $tag->name.' (Tag)'; ?></h5>
<?php
foreach($posts as $post)
{
if( has_tag( $tag ) )
{
?>
<a href="<?php echo the_permalink();?>"><?php echo the_title().' (Post)';?></a><br>
<?php
}
}
?>
<hr>
<?php
}
?>
感谢您的帮助!
你的代码逻辑应该是这样的:
- get_tags
- 每个标签
- 获得关联 posts(运行
get_posts
这里) - 只有有 post
- 回显标签的标题
- for each associated post (运行 foreach loop for posts here)
- 回显 post 的标题
- 获得关联 posts(运行
- 每个标签
所以您可以将 get_posts
查询移到 foreach($tags as $tag)
中并使用 $tag
参数来查看是否有任何 post(s) 与该标签关联。
请注意,在 $posts_args
中我使用了一个名为 tag
的额外参数,它将在 tags foreach loop
中为您解决问题。 tag
参数的值是标签 slug。
另外,通过使用 get_tags
函数,您不必指定 orderby
或 order
,因为 'orderby' 的默认值是 'name' ,'order' 的默认值为 'ASC',因此您无需将它们作为参数传递。
所以整个代码应该是这样的:
$cpt = houses;
$tax = type;
$term = small;
$posts_args = array(
'posts_per_page' => -1,
'orderby' => 'name',
'order' => 'ASC',
'post_type' => $cpt,
'tag' => $tag->slug,
//OR
//'tag_slug__in' => array($tag->slug),
'tax_query' => array(
array(
'taxonomy' => $tax,
'field' => 'name',
'terms' => $term
)
)
);
$tags = get_tags(); // Tag 1, Tag 2, Tag 3
foreach ($tags as $tag) {
$posts = get_posts($posts_args);
if ($posts) { ?>
<h5><?php echo $tag->name . ' (Tag)'; ?></h5>
<?php
foreach ($posts as $post) { ?>
<a href="<?php echo the_permalink(); ?>"><?php echo the_title() . ' (Post)'; ?></a><br>
<?php
}
?>
<hr>
<?php
}
}
?>
这将输出:
Tag 1
Post A
Post C
--------
Tag 2
Post B
如果你能让它工作,请告诉我!