WP_QUERY->max_num_ages在自定义帖子类型上始终返回0

时间:2014-03-12 作者:mviniciusbarreto

您好,我正在开发一个主题,我必须用ajax加载一些帖子,在我的索引页面中,一切都正常工作,问题是当我需要在自定义帖子类型上使用它时,我使用wp\\U localize\\u脚本传递jquery文件的值,当我有常规帖子时,它传递正确的值,但当我有自定义帖子类型时,它总是传递0。你能帮帮我吗?非常感谢。

将参数传递给jQuery脚本的代码

function core_ajax_init() {

    global $wp_query;

    // Add code to index pages.
    if( !is_admin() ) { //!is_singular()

        // Enqueue jQuery Script to Process Ajax
        wp_enqueue_script(
            \'core_custom\',
            get_template_directory_uri(). \'/core/js/ajax-load-posts.js\',
            array(\'jquery\'),
            \'1.0\',
            true
        );

        // What page are we on? And what is the pages limit?
        $max = $wp_query->max_num_pages;
        $paged = ( get_query_var(\'paged\') > 1 ) ? get_query_var(\'paged\') : 1;
        //echo $max;

        // Add some parameters for the JS.
        wp_localize_script(
            \'core_custom\',
            \'core\',
            array(
                \'startPage\' => $paged,
                \'maxPages\' => $max,
                \'nextLink\' => next_posts($max, false)
            )
        );
    }
 }

add_action(\'template_redirect\', \'core_ajax_init\');
下面是一个模板,我在其中调用打印按钮以加载更多帖子

<?php 
global $wp_query;

$found_posts = $wp_query->found_posts;
$per_page = get_option(\'posts_per_page\');
$post_count = $found_posts - $per_page;

if($found_posts > $per_page) :
?>
<div class="row" id="load-more" data-order=\'999\'>
    <div class="col-md-12">
        <div class="load-more-btn">
            <a id="load-more-btn" href="#">     
                <span id="detail-holder">
                    <div id="loader" data-perpage="<?php echo $per_page; ?>"></div>
                    <div class="load-more-text"><?php _e(\'Click here to load more\', CORE_THEME_NAME);  ?></div>
                </span>
            </a>
        </div>
    </div> <!-- /.col-md-12 -->
</div> <!-- /.row -->
<?php endif; ?>
这里有一个片段,我在其中为特定的帖子类型进行自定义查询

global $wp_query;
$paged = ( get_query_var(\'paged\') ) ? get_query_var(\'paged\') : 1;
$wp_query = new WP_Query(array( \'post_type\' => \'course\', \'order\' => \'DESC\', \'orderby\' => \'date\', \'paged\' => $paged ));
if($wp_query->have_posts()): ?>
这里的问题是,无论我有多少个页面,这里的wp\\u query var总是将0返回到我的函数core\\u ajax\\u init(),如果我有常规帖子,它会返回正确的页数,但对于自定义帖子类型,它总是返回0。为什么?谢谢你的回答。

1 个回复
SO网友:Goran Jakovljevic

Yuo也可以这样做,统计已发布的帖子,获取每页的设置帖子,然后划分。

$published_posts = wp_count_posts()->publish;
$posts_per_page = get_option(\'posts_per_page\');
$page_number_max = ceil($published_posts / $posts_per_page);

结束

相关推荐

使用带有jQuery插件Jedable的AJAX在定制表上插入查询

我已经读了很多帖子,但没有找到解决办法。。。我正在努力实现Jeditable 具有以下代码的插件中的功能:jQuery(document).ready(function() { jQuery(\'.edit\').editable(\'http://localhost/www/wp-content/plugins/my_plugin/save.php\' }); 如果在中save.php 我只是回显一些东西,它将返回到我正在更新的文本字段,没有问题。问题是如何将其插入数据