簡體   English   中英

僅使用WP_Query()在wordpress中顯示10個最近添加的帖子類別名稱

[英]Display 10 recently added posts category name only in wordpress using WP_Query()

現在,我將顯示類似以下代碼的類別列表:

<?php
    $arg = array(
       'orderby' => 'name',
       'number'   => 6,
    );
    $categories = get_categories($arg);
    foreach ($categories as $cat) {
?>
     <span class="firstMenuSpan" style="padding-right:34px;color:#000;">
          <a href="?catId=<?php echo $cat->cat_ID; ?>"><?php echo $cat->name; ?></a>
     </span>
<?php
}
?>

現在,類別按字母順序顯示,但是我需要顯示最近添加的帖子的類別。

任何想法或代碼都會有所幫助。

否則是否通過內置的WP_Query參數對類別值進行排序

我需要澄清一下,我可以借助以下代碼的方法來獲取類別名稱:

<?php
    $args = array(
     'numberposts' => 10,
     'offset' => 0,
     'category' => 0,
     'orderby' => 'post_date',
     'order' => 'DESC',
     'include' => '',
     'exclude' => '',
     'meta_key' => '',
     'meta_value' =>'',
     'post_type' => 'post',
     'post_status' => 'draft, publish, future, pending, private',
     'suppress_filters' => true
   );

   $recent_posts = wp_get_recent_posts( $args, ARRAY_A );
?>

如果通過上述方法獲得類別,是否可以限制值並顯示為重復的帖子類別值。

關於上述方法的任何幫助也很有用。

主要報價/目標/要求或終點:

僅需要顯示一個包含最近更新/添加的帖子的10個類別名稱的列表作為菜單。

有多種方法可以解決此問題並獲得所需的列表。 我將向您展示幾種不同的方式來獲取最近10個帖子的類別列表。

注意:請參閱下面的功能請求解釋,因為我為您提供的代碼是第二個代碼。

代碼段1-使用WordPress內置函數

第一個代碼解決方案執行以下操作:

  1. 獲取帖子ID的數組
  2. 如果返回數組,則它將從該列表中獲取類別。

為了實現可重用性,我已將該功能分為多個有目的的功能:

/**
 * Get a list of the most recent Post IDs.
 *
 * @since 1.0.0
 *
 * @return array|false Returns an array of post
 *                      IDs upon success
 */
function get_most_recent_post_ids() {
    $args = array(
        'posts_per_page' => 10,
        'orderby'        => 'post_date',
        'post_status'    => 'publish',
        'fields'         => 'ids',
    );

    $query = new WP_Query( $args );
    if ( ! $query->have_posts() ) {
        return false;
    }

    wp_reset_postdata();

    return $query->posts;
}

/**
 * Get the categories of the most recent posts.
 *
 * @since 1.0.0
 *
 * @return array|bool Returns an array of WP_Term objects upon success;
 *                      else false is returned.
 */
function get_most_recent_posts_categories() {
    $most_recent_post_ids = get_most_recent_post_ids();
    if ( ! $most_recent_post_ids ) {
        return false;
    }

    $categories = wp_get_object_terms( $most_recent_post_ids, 'category');
    if ( ! $categories || is_wp_error( $categories ) ) {
        return false;
    }

    return $categories;
}

代碼段2-編寫自己的SQL查詢

獲取此列表的另一種方法是編寫本機SQL查詢並使用$wpdb 在此代碼示例中,我們進行了一次數據庫命中以獲取類別列表。 對於每個類別,它返回術語ID,名稱和子詞供您使用。

/**
 * Get a list of categories for the most recent posts.
 *
 * @since 1.0.0
 *
 * @param int $number_of_posts Number of the most recent posts.
 *                              Defaults to 10
 * 
 * @return array|bool
 */
function get_most_recent_posts_categories( $number_of_posts = 10 ) {
    global $wpdb;

    $sql_query = $wpdb->prepare(
"SELECT t.term_id, t.name, t.slug
FROM {$wpdb->term_taxonomy} AS tt
INNER JOIN {$wpdb->terms} AS t ON (tt.term_id = t.term_id)
INNER JOIN {$wpdb->term_relationships} AS tr ON (tt.term_taxonomy_id = tr.term_taxonomy_id)
INNER JOIN {$wpdb->posts} AS p ON (tr.object_id = p.ID)
WHERE p.post_status = 'publish' AND tt.taxonomy = 'category'
GROUP BY t.term_id
ORDER BY t.term_id ASC, p.post_date DESC
LIMIT %d;", 
        $number_of_posts );

    $categories = $wpdb->get_results( $sql_query );
    if ( ! $categories || ! is_array( $categories ) ) {
        return false;
    }

    return $categories;
}

使用上面的代碼

您可以使用上述兩個選項:

$categories = get_most_recent_posts_categories();
if ( ! $categories ) {
    return;
}

foreach ( $categories as $category ) : ?>
    <span class="firstMenuSpan" style="padding-right:34px;color:#000;">
              <a href="?catId=<?php esc_attr_e( $category->term_id ); ?>"><?php esc_html_e( $category->name ); ?></a>
    </span>
<?php endforeach;

上面的代碼獲取類別列表。 如果沒有歸還,則您可以紓困。 否則,您可以遍歷它們中的每一個並構建HTML列表。

有關代碼的幾點注意事項:

  1. 如果未返回任何類別,則代碼將失敗。 如果您在此代碼段下運行其他代碼,則需要對此進行調整。
  2. 您希望在將值呈現給瀏覽器之前正確地轉義這些值。 請注意,代碼使用esc_attr_eesc_html_e 保持網頁清潔,衛生和安全。

同樣,有多種方法可以完成此功能請求。 我給了你兩個選擇。 您可以根據需要調整它們,然后選擇適合您的需求。

解釋功能請求

功能請求具有兩種不同的含義:

僅需要顯示一個包含最近更新/添加的帖子的10個類別名稱的列表作為菜單。

我們可以將請求解釋如下:

  1. 從字面上看,它說我想要10個類別名稱。 我希望他們來自最近的帖子。
  2. 或者...對於最近10篇帖子(最新),獲取類別名稱列表。

釋義1

此功能請求的目的有所不同。 第一種解釋說,無論最近的帖子數量如何,它都希望有10個類別。

讓我們考慮一下。 如果最后10個帖子中只有5個類別,那么您必須擴大帖子搜索范圍,直到收集所有10個類別。

與第二種解釋的要求不同,它將需要不同的代碼。

解釋2(我使用的那個)

這個說最后10個帖子,獲取他們的類別。 由於列表與最新的內容相關聯,因此此功能要求更為合理。

確保找到他們想要的版本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM