最新活动:买一送一!升级会员,最高返 500 抵扣券! 立即查看

WordPress获取指定文章类型的文章数量

需要调用数指定文章类型的文章数量,找到了 wp_count_posts() 函数,很方便就实现了。wp_count_posts() 介绍wp_count_posts() 函数是用来计算文章类型的文章数量的,还可以设置用户是否有权查看。有两个可用参数:wp_count_posts( string $type = 'pos...

需要调用数指定文章类型的文章数量,找到了 wp_count_posts() 函数,很方便就实现了。

wp_count_posts() 介绍

wp_count_posts() 函数是用来计算文章类型的文章数量的,还可以设置用户是否有权查看。有两个可用参数:

wp_count_posts( string $type = 'post', string $perm = '' )

$type :字符串,可选 。获取指定的文章类型的文章数,默认为: 'post',可以填写文章类型的名称。$perm : 字符串,可选 。检查“可读”值,如果用户可以阅读私密文章,它将为登录用户显示该文章。 可选值 readable 或 空,默认为空 ''获取默认文章的文章数

如果我们要调用默认的 文章 的文章数量,可以使用下面的代码:

$count_posts = wp_count_posts(); //里面没有参数,默认为 post if ( $count_posts ) {    $published_posts = $count_posts->publish;}

wp_count_posts() 函数返回的是一个数组,里面可能包含数据为:

stdClass Object ( [publish] => 12 [future] => 0 [draft] => 0 [pending] => 0 [private] => 0 [trash] => 1 [auto-draft] => 1 [inherit] => 0 [request-pending] => 0 [request-confirmed] => 0 [request-failed] => 0 [request-completed] => 0 ) 1

从中可以看到,可以针对文章的发布状态进行显示。比如在上面的示例代码中,我们采用 $count_posts->publish; 返回的是已发布的文章数量。

获取自定义文章类型的文章数

上面我们已经介绍了, wp_count_posts() 函数的第一个参数是文章类型名称,可以是 postpage 等这种内置的文章类型,也可以是自定义的文章类型。

那么,如果知道文章类型的名称呢,有一个比较直观的办法就是,在后台访问这个文章类型的管理界面:

然后我们就可以在网址栏中看到 post_type=page ,那么 page 就是这个文章类型名称了。

专业一点的话,我们需要去找到这个自定义文章类型的注册代码,在这里需要大家去脑补一下自定义文章类型的注册方式:

下面是一个官方的示例:

add_action( 'init', 'codex_book_init' );/** * Register a book post type. * * @link http://codex.wordpress.org/Function_Reference/register_post_type */function codex_book_init() {$labels = array('name'               => _x( 'Books', 'post type general name', 'your-plugin-textdomain' ),'singular_name'      => _x( 'Book', 'post type singular name', 'your-plugin-textdomain' ),'menu_name'          => _x( 'Books', 'admin menu', 'your-plugin-textdomain' ),'name_admin_bar'     => _x( 'Book', 'add new on admin bar', 'your-plugin-textdomain' ),'add_new'            => _x( 'Add New', 'book', 'your-plugin-textdomain' ),'add_new_item'       => __( 'Add New Book', 'your-plugin-textdomain' ),'new_item'           => __( 'New Book', 'your-plugin-textdomain' ),'edit_item'          => __( 'Edit Book', 'your-plugin-textdomain' ),'view_item'          => __( 'View Book', 'your-plugin-textdomain' ),'all_items'          => __( 'All Books', 'your-plugin-textdomain' ),'search_items'       => __( 'Search Books', 'your-plugin-textdomain' ),'parent_item_colon'  => __( 'Parent Books:', 'your-plugin-textdomain' ),'not_found'          => __( 'No books found.', 'your-plugin-textdomain' ),'not_found_in_trash' => __( 'No books found in Trash.', 'your-plugin-textdomain' ));$args = array('labels'             => $labels,'description'        => __( 'Description.', 'your-plugin-textdomain' ),'public'             => true,'publicly_queryable' => true,'show_ui'            => true,'show_in_menu'       => true,'query_var'          => true,'rewrite'            => array( 'slug' => 'book' ),'capability_type'    => 'post','has_archive'        => true,'hierarchical'       => false,'menu_position'      => null,'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ));register_post_type( 'book', $args );}

注意看倒数的 register_post_type( 'book', $args ); 里面的 book 就是文章类型的名称。所以,如果我们要调用 book 这个文章类型的文章数量,可以使用下面的代码:

$count_posts = wp_count_posts('book'); //参数填写为 book if ( $count_posts ) {    $published_posts = $count_posts->publish;}

好了,就是这样!

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
wordpress教程

WP越来越慢?WordPress完整加速的技巧大盘点

2023-2-16 16:00:20

wordpress教程

什么样的香港服务器才是高质量的服务器呢?我们租用的时候需要注意什么呢?

2023-2-16 16:00:22

!
你也想出现在这里?立即 联系我们吧!
信息
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
搜索