如何在类别页面上的循环外显示特色帖子,其中帖子必须与类别匹配
How to display featured posts outside of the loop on a category page, where posts must match the category
我正在尝试在所有类别页面上建立一个“热门文章”区域,位于显示 posts 的循环上方。我在每个 post 上添加了一个“精选 Post”复选框,允许管理员将某些 post 标记为精选,并且我已经能够显示这些 post 位于每个类别页面的顶部。但它目前在所有类别页面上显示所有特色 posts,我需要系统过滤 posts 以显示 ONLY posts 属于同一类别作为它们显示的页面。
这是我在我的函数文件中使用的,它可以很好地显示特色 posts - 任何帮助添加类别过滤都将受到赞赏!
$args = array(
'posts_per_page' => 5,
'meta_key' => 'meta-checkbox',
'meta_value' => 'yes'
);
$featured = new WP_Query($args);
if ($featured->have_posts()): while($featured->have_posts()): $featured->the_post(); ?>
<h3><a href="<?php the_permalink(); ?>"> <?php the_title(); ?></a></h3>
<p class="details">By <a href="<?php the_author_posts() ?>"><?php the_author(); ?> </a> / On <?php echo get_the_date('F j, Y'); ?> / In <?php the_category(', '); ?></p>
<?php if (has_post_thumbnail()) : ?>
<figure> <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a> </figure>
<p ><?php the_excerpt();?></p>
<?php
endif;
endwhile; else:
endif;
?>```
如果您只需要将此用于 类别 存档页面而不是其他或自定义分类法,则您只需要全局变量 $category_name
。尽管它被称为“名称”,但它实际上是类别 slug,它允许我们在将附加到查询的 tax_query
字段中使用它。
首先,我们使 $category_name
可用,然后在我们的查询中将其与您的元字段一起使用:
global $category_name;
$featured_posts = new WP_Query(
[
"showposts" => 5,
"meta_key" => "meta-checkbox",
"meta_value" => "yes",
"tax_query" => [
[
"taxonomy" => "category",
"field" => "slug",
"terms" => $category_name,
],
]
]
);
这将为我们提供
- 在当前分类存档页面的分类里面,
- 管理员通过
meta-checkbox
元键标记为 精选帖子。
现在我们可以使用这些帖子并循环浏览它们。这是一个非常简单的循环,几乎没有标记:
if ($featured_posts->have_posts()) {
while ($featured_posts->have_posts()) {
$featured_posts->the_post();
?>
<h3>
The post "<?php the_title(); ?>" is in category "<?php the_category(" "); ?>"
</h3>
<?php
}
}
else {
?>
<h3>No featured posts were found :(</h3>
<?php
}
这是它的外观示例。如您所见,所有五个帖子都与存档页面类别属于同一类别。
希望对您有所帮助。
我正在尝试在所有类别页面上建立一个“热门文章”区域,位于显示 posts 的循环上方。我在每个 post 上添加了一个“精选 Post”复选框,允许管理员将某些 post 标记为精选,并且我已经能够显示这些 post 位于每个类别页面的顶部。但它目前在所有类别页面上显示所有特色 posts,我需要系统过滤 posts 以显示 ONLY posts 属于同一类别作为它们显示的页面。
这是我在我的函数文件中使用的,它可以很好地显示特色 posts - 任何帮助添加类别过滤都将受到赞赏!
$args = array(
'posts_per_page' => 5,
'meta_key' => 'meta-checkbox',
'meta_value' => 'yes'
);
$featured = new WP_Query($args);
if ($featured->have_posts()): while($featured->have_posts()): $featured->the_post(); ?>
<h3><a href="<?php the_permalink(); ?>"> <?php the_title(); ?></a></h3>
<p class="details">By <a href="<?php the_author_posts() ?>"><?php the_author(); ?> </a> / On <?php echo get_the_date('F j, Y'); ?> / In <?php the_category(', '); ?></p>
<?php if (has_post_thumbnail()) : ?>
<figure> <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a> </figure>
<p ><?php the_excerpt();?></p>
<?php
endif;
endwhile; else:
endif;
?>```
如果您只需要将此用于 类别 存档页面而不是其他或自定义分类法,则您只需要全局变量 $category_name
。尽管它被称为“名称”,但它实际上是类别 slug,它允许我们在将附加到查询的 tax_query
字段中使用它。
首先,我们使 $category_name
可用,然后在我们的查询中将其与您的元字段一起使用:
global $category_name;
$featured_posts = new WP_Query(
[
"showposts" => 5,
"meta_key" => "meta-checkbox",
"meta_value" => "yes",
"tax_query" => [
[
"taxonomy" => "category",
"field" => "slug",
"terms" => $category_name,
],
]
]
);
这将为我们提供
- 在当前分类存档页面的分类里面,
- 管理员通过
meta-checkbox
元键标记为 精选帖子。
现在我们可以使用这些帖子并循环浏览它们。这是一个非常简单的循环,几乎没有标记:
if ($featured_posts->have_posts()) {
while ($featured_posts->have_posts()) {
$featured_posts->the_post();
?>
<h3>
The post "<?php the_title(); ?>" is in category "<?php the_category(" "); ?>"
</h3>
<?php
}
}
else {
?>
<h3>No featured posts were found :(</h3>
<?php
}
这是它的外观示例。如您所见,所有五个帖子都与存档页面类别属于同一类别。
希望对您有所帮助。