首頁  >  問答  >  主體

展示從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 天前338

全部回覆(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
  • 取消回覆