Paginate为一个简单的循环

时间:2016-03-01 08:27:23

标签: php wordpress pagination

我已经获得了此代码,只显示特定作者的所有帖子:

<?php
    $all_active_tasks = get_posts(array(
    'numberposts'       => -1,
    'offset'            => 0,
    'post_status'       => 'publish',
    'author'            => '1',
    'post_type'         => 'post'
    )
);
foreach($all_active_tasks as $post) :
$category = get_the_category();
setup_postdata($post);
?>
<div class="the-post">
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <p><?php echo $category[0]->cat_name; ?></p>
</div>

<?php endforeach; ?>
<?php wp_reset_postdata(); ?>

但我无法弄清楚的是如何将结果分页为每页10个。我看过官方的手抄本,但我尝试的任何东西似乎都没有用。

感谢任何帮助。

3 个答案:

答案 0 :(得分:1)

您可以将pagedposts_per_page参数一起使用。 WP_Query。你可以抓住这样的当前页面:

$paged = get_query_var( 'paged' ) ?: ( get_query_var( 'page' ) ?: 1 );

然后在您的查询中使用它:

$all_active_tasks = get_posts(array(
   'posts_per_page'    => 10,
   'post_status'       => 'publish',
   'author'            => '1',
   'post_type'         => 'post',
   'paged'             => $paged
));

这样,如果您将/page/2/放在网址的末尾,则查询会将帖子从11回复到20。

如何创建分页本身,您可以查看这些文章: herehere

答案 1 :(得分:0)

设置'numberposts' => -1表示获取所有记录。您必须设置前10条记录(0-9条记录)

'numberposts'       => 10,
'offset'            => 0,

接下来的10,(10-19记录)

'numberposts'       => 10,
'offset'            => 10,

接下来的10,(20-29记录)

'numberposts'       => 10,
'offset'            => 20,

答案 2 :(得分:0)

试试这个有效的代码

将此函数放入活动主题的functions.php文件中

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
<xsl:template match="/">    
    <Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <xsl:apply-templates/>                          
    </Document>
</xsl:template> 
<xsl:template match="Configurations">
    <xsl:copy>
        <xsl:for-each select="Configuration" >
            <xsl:sort select="@type"/>
            <xsl:copy-of select="." /> 
            <xsl:apply-templates select="."/>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>
<xsl:template match="Configurations/Configuration/Section">
    <xsl:copy>
        <xsl:for-each select="Parameter" >
            <xsl:sort select="@name"/>
            <xsl:copy-of select="." />
        </xsl:for-each>
    </xsl:copy>         
</xsl:template> 
    <xsl:template match="Adaptors">
    <xsl:copy>
        <xsl:for-each select="Adaptor">
            <xsl:sort select="@type"/>
            <xsl:copy-of select="." />
        </xsl:for-each>     
    </xsl:copy>
</xsl:template>     
</xsl:stylesheet>

这是您修改后的代码,用于分页

function custom_pagination($numpages = '', $pagerange = '', $paged='') {



  if (empty($pagerange)) {

    $pagerange = 2;

  }





  global $paged;

  if (empty($paged)) {

    $paged = 1;

  }

  if ($numpages == '') {

    global $wp_query;

    $numpages = $wp_query->max_num_pages;

    if(!$numpages) {

        $numpages = 1;

    }

  }



  $pagination_args = array(

    'base'            => get_pagenum_link(1) . '%_%',

    'format'          => 'page/%#%',

    'total'           => $numpages,

    'current'         => $paged,

    'show_all'        => False,

    'end_size'        => 1,

    'mid_size'        => $pagerange,

    'prev_next'       => True,

    'prev_text'       => __('<i class="fa fa-angle-double-left"></i>'),

    'next_text'       => __('<i class="fa fa-angle-double-right"></i>'),

    'type'            => 'plain',

    'add_args'        => false,

    'add_fragment'    => ''

  );



  $paginate_links = paginate_links($pagination_args);



  if ($paginate_links) {

    echo "<div class='col-md-12'><nav class='custom-pagination pagination'>";

      echo $paginate_links;

    echo "</nav></div>";

  }



}