【问题标题】:Custom category pagination Wordpress自定义类别分页 Wordpress
【发布时间】:2016-11-05 11:59:12
【问题描述】:

我在 WordPress 的自定义模板页面上有一个自定义循环,它显示来自特定类别的帖子。这一切都很好,并显示了我需要的一切,但是我最终需要添加一些分页。由于这是一个自定义循环,因此本机 WP '博客页面最多显示'似乎不起作用。有没有办法为我的自定义循环添加分页?

   <?php
    // add journal posts to the journal page
    query_posts( array ( 'category_name' => 'journals', 'posts_per_page' => -1 ) );
    ?>
    <?php
    // The Loop
    while ( have_posts() ) : the_post();
        echo '<div class="journal-posts">';
        echo '<h2 class="entry-title">';
        echo '<div><a href="'. esc_url( get_permalink() ) . '">' . sprintf(__( get_the_title() )) . '</a> <img src="http://localhost/website.co.uk/wp-content/themes/themename/images/icons/icon.png" alt="Icon Stuff"/></div>';
        echo '</h2>';
        echo '<span class="entry-meta">Posted on ';
        echo '<span class="date-link">';
        the_date();
        echo '</span>';
        echo ' by ';
        echo '<span class="author vcard"><a class="url fn n" href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '">' . esc_html( get_the_author() ) . '</a></span>';
        echo '</span>';
        the_content();
        echo '</div>';
        endwhile; ?>
  <?php
  // Reset Query
  wp_reset_query();
?>

我为这个网站调用了三个不同的类别,因此我需要在每个页面上都有这个自定义循环。除非有更好的方法吗?

提前致谢!

【问题讨论】:

  • 你看过this article了吗...?只是快速搜索,没有自己经历过......
  • 嘿@webeno,你指给我的文章也给了我需要的东西,谢谢!
  • 我添加了它作为答案,如果您愿意,可以选择它作为正确的答案。

标签: php html wordpress pagination posts


【解决方案1】:

我认为关键是在查询中设置/使用$paged 属性,如下所示:

$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$query_args = array(
  'post_type' => 'post',
  'category_name' => 'tutorials',
  'posts_per_page' => 5,
  'paged' => $paged
);

这摘自 this articlethis question 并进行了很好的解释。

【讨论】:

    猜你喜欢
    • 2017-04-16
    • 1970-01-01
    • 2012-06-21
    • 1970-01-01
    • 2012-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多