gpt4 book ai didi

php - 获取页面上的当前类别名称,该页面按类别和自定义元键过滤帖子

转载 作者:行者123 更新时间:2023-12-01 04:37:20 25 4
gpt4 key购买 nike

我成功地使用自定义插件(和meta_key)按喜欢(计数)过滤了我的所有WordPress帖子(在自定义页面模板中),这也让我可以使用以下内容过滤特定类别中最喜欢的帖子

if (isset($_GET['category'])) {
$args = array(
'meta_key' => '_recoed',
'meta_compare' => '>',
'meta_value' => '0',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'category_name' => sanitize_text_field($_GET['category']),
'paged' => $paged
);
}

query_posts($args);

get_template_part('index');

用于过滤每个类别的帖子的类别列表(工作正常)

<?php $categories = get_categories('exclude=' . implode(',', my_blog_cats()) . ', 1'); ?>

<?php if ($categories) { ?>

<?php $categories = get_categories(); ?>

<?php foreach($categories as $category) { ?>
<li>
<a class="popular-categories" href="<?php echo get_permalink(); ?>?category=<?php echo $category->category_nicename; ?>"><?php echo $category->name; ?></a>
</li>
<?php endforeach; ?>

<?php } ?>

例如过滤帖子后的网址 - 看起来像

.../hot-posts/?category=new-posts-category

知道如何仅回显当前页面上的当前类别名称吗?在示例中,它将是“新帖子类别”

最佳答案

有 3 种可能性(WP 类别的分类是 category):

1) ID - 如果 $_GET['category'] 是 WP category 术语 ID你将使用:

if( isset($_GET['category'] ) && term_exists( intval($_GET['category']), 'category' ) ){
$term = get_term( intval($_GET['category']), 'category' );
echo '<p>' . $term->name . '</p>';
}

2) SLUG - 如果 $_GET['category'] 是 WP category 术语 SLUG你将使用:

if( isset($_GET['category'] ) && term_exists( sanitize_text_field($_GET['category']), 'category' ) ){
$term = get_term_by( 'slug', sanitize_text_field($_GET['category']), 'category' );
echo '<p>' . $term->name . '</p>';
}

3) 名称 - 如果它已经是 WP 类别 术语名称,只需使用:

if( isset($_GET['category'] ) && term_exists( sanitize_text_field($_GET['category']), 'category' ) )
echo '<p>' . sanitize_text_field($_GET['category']) . '</p>';

But dont use sanitize_title() on WP category terme NAME as it will become a term SLUG

关于php - 获取页面上的当前类别名称,该页面按类别和自定义元键过滤帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48525133/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com