如何获取function.php文件中每页的帖子数量?

时间:2017-12-27 14:02:27

标签: php wordpress

从Wordpress我获得每页帖子数$posts_per_page

的数据
global $wp_query;
$args = array(
    'post_type' => 'post',
    'category_name' => $categories,
    'posts_per_page' => $posts_per_page,
);
$posts = new WP_Query( $args );

现在如何在functions.php中获取此数字?

wp_register_script( 'loadmore', get_stylesheet_directory_uri() . '/assets/scripts/load-more.js', '', '', true );
wp_localize_script( 'loadmore', 'loadmore_params', array(
    'ajaxurl' => admin_url( 'admin-ajax.php' ),
    'posts' => $wp_query->query_vars,
    'current_page' => get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1,
    'max_page' => $posts_per_page,  
) );
wp_enqueue_script( 'loadmore' );

1 个答案:

答案 0 :(得分:0)

function.php

function custom_posts_per_page($query) {
    if (is_home()) {
        $query->set('posts_per_page', $posts_per_page);
    }
    if (is_search()) {
        $query->set('posts_per_page', $posts_per_page);
    }
    if (is_archive()) {
        $query->set('posts_per_page', $posts_per_page);
    } //endif
} //function