[英]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个帖子的类别列表。
注意:请参阅下面的功能请求解释,因为我为您提供的代码是第二个代码。
第一个代码解决方案执行以下操作:
为了实现可重用性,我已将该功能分为多个有目的的功能:
/**
* 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;
}
获取此列表的另一种方法是编写本机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列表。
有关代码的几点注意事项:
esc_attr_e
和esc_html_e
。 保持网页清洁,卫生和安全。 同样,有多种方法可以完成此功能请求。 我给了你两个选择。 您可以根据需要调整它们,然后选择适合您的需求。
功能请求具有两种不同的含义:
仅需要显示一个包含最近更新/添加的帖子的10个类别名称的列表作为菜单。
我们可以将请求解释如下:
此功能请求的目的有所不同。 第一种解释说,无论最近的帖子数量如何,它都希望有10个类别。
让我们考虑一下。 如果最后10个帖子中只有5个类别,那么您必须扩大帖子搜索范围,直到收集所有10个类别。
与第二种解释的要求不同,它将需要不同的代码。
这个说最后10个帖子,获取他们的类别。 由于列表与最新的内容相关联,因此此功能要求更为合理。
确保找到他们想要的版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.