简体   繁体   English

Wordpress:如何显示特定类别的标签?

[英]Wordpress : How to display tags from a specific category?

I tried many solutions, but not good in my case. 我尝试了很多解决方案,但在我的情况下并不好。 Do you have an idea to display "best tags" (most use of last 30 days) from a specific category with function get_terms() of Wordpress ? 您是否有想法使用Wordpress的函数get_terms()从特定类别显示“最佳标签”(大多数使用最近30天)?

<?php $wpdb->show_errors(); ?>
<?php
global $wpdb;
$term_ids = $wpdb->get_col(
"SELECT term_id FROM $wpdb->term_taxonomy
INNER JOIN $wpdb->term_relationships ON $wpdb->term_taxonomy.term_taxonomy_id = $wpdb->term_relationships.term_taxonomy_id
INNER JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->term_relationships.object_id
WHERE DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= $wpdb->posts.post_date"
                    );

if(count($term_ids) > 0){
$tags = get_terms(array(
    'orderby' => 'count', 
    'order' => 'DESC', 
    'number' => 8, 
    'include' => $term_ids,
));

foreach ( (array) $tags as $tag ) { ?>
    <li>
        <a href="<?php echo get_tag_link($tag->term_id); ?>" rel="tag" class="text-center">
            <img src="<?php the_field('image', $tag); ?>" class="trend-artist-img">
            <p><?php echo $tag->name ?></p>
        </a>
    </li>
<?php }} ?>

With this code, I have the complete list of the "best tags" but not for a specific category. 使用此代码,我有“最佳标签”的完整列表,但不是特定类别。

The category what I need have the ID : 18. 我需要的类别有ID:18。

Thanks! 谢谢!

You can filter by taxonomy by passing the taxonomy to get_terms: 您可以通过将分类法传递给get_terms来按分类法进行过滤:

`$tags = get_terms(array( 
        'orderby' => 'count', 
        'order' => 'DESC', 
        'number' => 8, 
        'include' => $term_ids,
        'taxonomy' => 'my_category', // e.g 'post_tag'
    )); 

Or, to filter by a specific category within a taxonomy pass the 'name' to get_terms: 或者,通过一个分类的特定类别进行过滤通过的“名称”到get_terms:

`$tags = get_terms(array( 
        'orderby' => 'count', 
        'order' => 'DESC', 
        'number' => 8, 
        'include' => $term_ids,
        'name' => 'my_category_name', 
    ));

These can be combined. 这些可以结合起来。 Does this help? 这有帮助吗?

I've re-read your question and I think what you're trying to do is probably not possible with a single query. 我已经重新阅读了您的问题,我认为您尝试做的事情可能无法通过单个查询完成。

The trouble is that both tags and categories are applied to posts rather than to each other, so there's no easy relationship between the two. 问题是标签和类别都应用于帖子而不是相互之间,因此两者之间没有简单的关系。 You could do a new WP_Query for each best tag to check if there are any posts that have both the tag and the category, but that's going to be quite slow and inefficient. 您可以为每个最佳标记执行新的WP_Query,以检查是否有任何帖子同时包含标记和类别,但这将非常缓慢且效率低下。 Or you could do a WP_Query to get all posts that have that category and ANY of the best tags and then use the results of that query to work out which, if any, of the best tags haven't been used. 或者您可以执行WP_Query以获取具有该类别和任何最佳标记的所有帖子,然后使用该查询的结果来确定哪些(如果有)最佳标记未被使用。

Both these options seem more trouble than they're worth to me! 这些选项似乎比它们对我的价值更麻烦!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM