首页  >  问答  >  正文

展示从WordPress文章循环中查看最多的自定义分类法,并删除重复的值

我正在创建一个循环,其中显示网站上访问量最大的分类法。我知道 WordPress 不跟踪分类法和类别视图。因此,我在帖子中插入了一个跟踪器,以创建一个包含浏览次数最多的帖子的循环,然后在主页上显示该帖子的分类法。

代码归功于 isitwp

function setPostViews($postID) {
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    }else{
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
}
 
// Remove issues with prefetching adding extra views
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

插入跟踪器后,创建一个循环以显示查看次数最多的帖子

代码如下:

<?php
$popular = array(
    'post_type'         => 'videos',
    'posts_per_page'    => 8,
    'meta_key'          => 'post_views_count', // setPostViews($postID) function;
    'orderby'           => 'meta_value_num',
    'order'             => 'ASC',
    'offset'            => 1,
);

$popular_loop = new WP_Query( $popular );

if( $popular_loop->have_posts() ){

    while( $popular_loop->have_posts() ) : $popular_loop->the_post();

        $terms = get_the_terms( $post->ID, 'seasons' ); 

        foreach($terms as $term) {

            $ids = $term->term_id;

            $arr = explode( ',', $ids );

            $arr_unique = array_unique( $arr );

            $str = implode( $arr_unique );

            if($term->parent != 0){
                /**
                 * Taxonomie has children get the parent ID
                */
                echo '<p>Return parent unique:' . $str . '</p>';
            
            } else {
                /**
                 * Taxonomie has NO children get the current taxonomy ID 
                */
                echo '<p>Return unique:' . $str . '</p>';
            }

        }

    endwhile;

} else {
    return false;
} wp_reset_postdata();

问题是,因为我希望它显示 name、id、链接、图像等。浏览次数最多的帖子的父分类法中,它们开始重复自己,我想排除重复项,这样每次有人访问与同一分类法不同的帖子时就不会重复我尝试了 array_unique() 但它不断返回我有重复的值。

有什么方法可以删除循环内的重复值吗?

P粉547362845P粉547362845210 天前335

全部回复(1)我来回复

  • P粉545910687

    P粉5459106872024-02-27 12:17:30

    我设法解决了我的问题,如果我的问题令人困惑或没有让某些用户满意,我深表歉意

    代码如下:

     'videos',
        'posts_per_page'    => 8,
        'order'             => 'ASC',
    );
    
    $popular_loop = new WP_Query( $popular );
    
    if( $popular_loop->have_posts() ){
    
        // Get the terms array
        $unique = [];
    
        while( $popular_loop->have_posts() ) : $popular_loop->the_post();
    
            // Taxonomy loop post
            $terms = get_the_terms( $post->ID, 'seasons' ); 
    
            foreach($terms as $term) {
    
                // Get the unique array from loop
                if( !in_array( $term->term_id, $unique ) ){
                    $unique[] = $term->term_id;
    
                    // Get unique ID from Taxonomy
                    echo $term->term_id;
    
                    // Get Unique name fromt Taxonomy
                    echo $term->name;
                    
                }
    
            }
    
        endwhile;
    
    } else {
    
        echo 'Nothing';
    
    } wp_reset_postdata();

    回复
    0
  • 取消回复