Php 使用WP_Query()仅在wordpress中显示10篇最近添加的文章类别名称
现在,我显示类别列表,如下代码所示:Php 使用WP_Query()仅在wordpress中显示10篇最近添加的文章类别名称,php,wordpress,wordpress-theming,custom-wordpress-pages,Php,Wordpress,Wordpress Theming,Custom Wordpress Pages,现在,我显示类别列表,如下代码所示: <?php $arg = array( 'orderby' => 'name', 'number' => 6, ); $categories = get_categories($arg); foreach ($categories as $cat) { ?> <span class="firstMenuSpan" style="padding-right:
<?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_查询参数对类别值进行排序
我需要一个澄清,我可以借助下面的代码获得类别名称:
<?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内置 第一个代码解决方案执行以下操作:
/**
* 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、名称和slug供您使用
/**
* 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;
$categories=get_most_recently_posts_categories();
如果(!$categories){
返回;
}
foreach($类别作为$类别):?>
有多种方法可以解决此问题,并为您提供所需的列表。我将向您展示几种不同的方法来获取最近10篇文章的类别列表
注意:请参阅下面的特性请求解释,因为我给您的代码是第二个
代码片段1-使用WordPress内置
第一个代码解决方案执行以下操作:
获取post ID的数组
如果返回一个数组,那么它将从该列表中获取类别
为了可重用性,我将功能分解为单独的有目的的功能:
/**
* 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、名称和slug供您使用
/**
* 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;
$categories=get_most_recently_posts_categories();
如果(!$categories){
返回;
}
foreach($类别作为$类别):?>
有道理,会回到你的超级@hellofromTonya,两种方法都是超级的。是的,我需要看看在你们的面试之后,主要的要求是什么+1.有意义的话,我会回到你的超级@hellofromTonya,两种方法都非常好。是的,我需要看看在你们的面试之后,主要的要求是什么+1美元。