【问题标题】:Wordpress Custom Query Pagination. How do i add pagination to this?Wordpress 自定义查询分页。我如何为此添加分页?
【发布时间】:2014-09-30 21:38:04
【问题描述】:

我目前正在通过查询从数据库中获取所有帖子。包含的 php 代码是自定义 Wordpress 模板的一部分。我正在使用来自以下位置的自定义元框:https://github.com/WebDevStudios/Custom-Metaboxes-and-Fields-for-WordPress

我需要做什么才能每 6 个帖子分页一次?

         <?php 
            //get_template_part( 'content', 'page' ); 
            echo'<h2 class="section-header-dark">'.get_the_title().'</h2><hr>';

                //adjusting the query
                $args = array(
                    'post_type' => 'blog',
                    'posts_per_page' => -1,
                    'orderby' => 'menu_order',
                    'order' => ASC
                );

                // The Query
                $latest_post = new WP_Query( $args );
                // The Loop
                if ( $latest_post->have_posts() ) 
                {
                    while ( $latest_post->have_posts() ) 
                    {   
                        $latest_post->the_post();

                        $desc = wpautop( get_post_meta( get_the_ID(), '_cw_desc', true ) );

                        echo'<div class=""><h5 class="">'. get_the_title(). '</h5>';
                        echo $desc;
                        echo'<h6 class="news_date f_right"><i>'. get_the_date(). '</i></h6>';
                        echo'</div><hr>';

                    }
                } 
                else 
                {
                    // no posts do nothing
                }
                wp_reset_postdata();
        ?>

【问题讨论】:

    标签: php wordpress pagination


    【解决方案1】:

    您需要设置'posts_per_page' =&gt; 6 并将paged 参数添加到查询中。

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array(
      'posts_per_page' => 6,
      'paged' => $paged
    );
    
    ?>
    

    访问http://codex.wordpress.org/Pagination了解更多详情。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-24
      • 2015-09-21
      • 1970-01-01
      相关资源
      最近更新 更多