WordPress:如何通过自定义分类法在作者页面中显示帖子数 [英] Wordpress: How to display post count in author page by custom taxonomy

查看:96
本文介绍了WordPress:如何通过自定义分类法在作者页面中显示帖子数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在作者页面上显示带有计数器的自定义分类法,但似乎我不知道该怎么做。


我在函数中有一个代码。 php

  add_action('pre_get_posts',function($ q){

if(!is_admin( )&& $ q-> is_main_query()&& $ q-> is_author()){

$ q-> set('posts_per_page',100);
$ q-> set('post_type','custom_feedback');

}

});

,在我的作者页面中:

 < div class =反馈响应> 
< h3 class = feedback-title>用户反馈< / h3>
<?php if(have_posts()):while(have_posts()):the_post(); ?>
<?php the_content(); ?>
<?php endwhile;否则:?>
< p><?php _e(此作者没有任何帖子。); && lt; / p>
<?php endif; ?>
< / div>

该代码适用于所有作者个人资料,但我不知道如何获取自定义分类法以这种方式显示:


用户反馈


6积极反馈4消极反馈


所有反馈都在这里


所有反馈都在这里


所有反馈都在这里


顺便说一下,这是一个自定义帖子类型(custom_feedback)和自定义分类法(feedback_taxonomy),具有正和负两个类别。


请帮助高手吗?


<解决方案

您唯一的解决方法是运行两个单独的查询并计算从两个单独的查询返回的帖子。为此,我们将使用 get_posts ,因为 get_posts 已经将一些重要的默认值传递给 WP_Query 使查询更快,更注重性能。



我们将为查询添加一个节省大量时间和资源的资源,字段 => ids 。这是什么,它仅获取帖子ID,而不获取完整的post对象。这样可以将查询时间和数据库查询减少99%,因此,即使您要在整个数据库上运行两个单独的查询,页面性能的损失也不会引起注意。



让所有内容都放入代码中(这进入author.php中,请注意,此代码未经测试,至少需要PHP 5.4 +

  $ author_id = get_queried_object_id(); //在查看作者页面时获取作者ID 

//将术语词条添加到数组中。
$ terms = [正,负]; //只要确保这些值正确
$ count = [];
foreach($ terms为$ term){
//建立查询
$ args = [
nopaging =>是的,
'post_type'=> ‘custom_feedback’,
‘author’=> $ author_id,
tax_query => [
[
分类 => ‘feedback_taxonomy’,
‘field’=> ‘slug’,
’terms’=> $ term
],
],
fields => 'ids'
];
$ q = get_posts($ args);

//计算帖子数量并添加到数组
$ count [$ term] = count($ q);
}

//显示带有帖子计数的文本,只需确保您的数组键与您使用过的词条相符
$ positive =(isset($ count ['positive' ]))? $ count ['positive']:0;
$ neative =(isset($ count [’negative’]))吗? $ count ['negative']:0;

echo $ positive。 ' 正面反馈 ' 。 $ negative。 ' 负面反馈';


I am trying to display the custom taxonomy in author page with a counter but seems i don't know how to do it.

i have a code in function.php

    add_action( 'pre_get_posts', function ( $q ) {

    if( !is_admin() && $q->is_main_query() && $q->is_author() ) {

        $q->set( 'posts_per_page', 100 );
        $q->set( 'post_type', 'custom_feedback' );

    }

});

and in my author page :

<div class="feedback-respond"> 
         <h3 class="feedback-title">User Feedback </h3>
             <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
               <?php the_content(); ?>
             <?php endwhile; else: ?>
               <p><?php _e('No posts by this author.'); ?></p>
             <?php endif; ?> 
            </div>

The code works for all the author profile but i don't know how to get the the custom taxonomy to display like this:

User Feedback

6 POSITIVE feedback 4 NEGATIVE feedback

all feedback goes here

all feedback goes here

all feedback goes here

By the way it is a custom post type (custom_feedback)and custom taxonomy(feedback_taxonomy) with two category Positive and Negative.

Please help masters?

解决方案

Your only way to acvieve this will be to run two separate queries and counting the posts returned from the two separate queries. For this we will use get_posts as get_posts already passes a few important defaults to WP_Query to make the query faster and more performance orientated.

We will add one huge time and resource saver to the query, 'fields' => 'ids'. What this does is, it fetches only the post ids, and not the complete post object. This can cut query time and db queries by 99%, so even though you are going to run 2 separate queries on the complete database, the loss in page performance will be unnoticable.

Lets put everything in code (This goes into author.php, and note, this code is untested and needs at least PHP 5.4+)

$author_id = get_queried_object_id(); // Gets the author id when viewing the author page

// Add our term slugs into an array. 
$terms = ['positive', 'negative']; // Just make sure these values are correct
$count = [];
foreach ( $terms as $term ) {
    // Build our query
    $args = [
        'nopaging' => true,
        'post_type' => 'custom_feedback',
        'author' => $author_id,
        'tax_query' => [
            [
                'taxonomy' => 'feedback_taxonomy',
                'field' => 'slug',
                'terms' => $term
            ],
        ],
        'fields' => 'ids'
    ];
    $q = get_posts( $args );

    // Count the amount of posts and add in array
    $count[$term] = count( $q );
}

// Display our text with post counts, just make sure your array keys correspond with your term slugs used
$positive = ( isset( $count['positive'] ) ) ? $count['positive'] : 0;
$negative =( isset( $count['negative'] ) ) ? $count['negative'] : 0;

echo $positive . ' POSITIVE feedback ' . $negative . ' NEGATIVE feedback';

这篇关于WordPress:如何通过自定义分类法在作者页面中显示帖子数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆