最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

pagination - Custom category.php paging shows the same posts

programmeradmin0浏览0评论

I've created a custom category.php for my client. The issue is, that if I click to next/prev pagination links, it shows the same posts. Any idea what can be wrong?

category.php

<?php
    /* Category template file */
    get_header();

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $the_query = new WP_Query( 'posts_per_page=2&paged=' . $paged ); 
?>

<div id="content">
    <img id="eu-logo" src="<?php bloginfo('template_url')?>/img/title.png" />

    <div class="article">

        <?php global $post;
        $args = array('category' => 11, 'posts_per_page' => 2, 'order_by' => 'date');
        $custom_posts = get_posts($args);
        foreach($custom_posts as $post) : setup_postdata($post);

        echo "<div class='post'><h2 class='article__title'><a href='";
        the_permalink();
        echo "'>";
        the_title();
        echo "</a></h2>";

        the_excerpt();
        echo "</div>";

        endforeach;

        echo "<div id='forum-link'>";
        echo "<a class='custom-nav' href=''>Main Page</a><br />";
        echo "<a class='custom-nav' href='/'>Forum</a>";
        echo "</div>";

        ?>

    </div>

    <div><?php next_posts_link('Next Page &raquo;') ?></div>
    <div><?php previous_posts_link('&laquo; Previous Page') ?></div>

    <?php get_sidebar(); ?>
</div>

<?php get_footer(); ?>

I've created a custom category.php for my client. The issue is, that if I click to next/prev pagination links, it shows the same posts. Any idea what can be wrong?

category.php

<?php
    /* Category template file */
    get_header();

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $the_query = new WP_Query( 'posts_per_page=2&paged=' . $paged ); 
?>

<div id="content">
    <img id="eu-logo" src="<?php bloginfo('template_url')?>/img/title.png" />

    <div class="article">

        <?php global $post;
        $args = array('category' => 11, 'posts_per_page' => 2, 'order_by' => 'date');
        $custom_posts = get_posts($args);
        foreach($custom_posts as $post) : setup_postdata($post);

        echo "<div class='post'><h2 class='article__title'><a href='";
        the_permalink();
        echo "'>";
        the_title();
        echo "</a></h2>";

        the_excerpt();
        echo "</div>";

        endforeach;

        echo "<div id='forum-link'>";
        echo "<a class='custom-nav' href='https://test'>Main Page</a><br />";
        echo "<a class='custom-nav' href='https://test/forum/'>Forum</a>";
        echo "</div>";

        ?>

    </div>

    <div><?php next_posts_link('Next Page &raquo;') ?></div>
    <div><?php previous_posts_link('&laquo; Previous Page') ?></div>

    <?php get_sidebar(); ?>
</div>

<?php get_footer(); ?>
Share Improve this question asked Mar 21, 2019 at 12:05 Runtime TerrorRuntime Terror 1032 bronze badges 3
  • You are missing 'paged' element in $args = array('category' => 11, 'posts_per_page' => 2, 'order_by' => 'date'); – Qaisar Feroz Commented Mar 21, 2019 at 12:19
  • And how should I add it? – Runtime Terror Commented Mar 21, 2019 at 12:21
  • Please see my answer – Qaisar Feroz Commented Mar 21, 2019 at 12:26
Add a comment  | 

1 Answer 1

Reset to default 1

This should be

$args = array('category' => 11, 'posts_per_page' => 2, 'order_by' => 'date');

like this

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('category' => 11, 'posts_per_page' => 2, 'order_by' => 'date', 'paged' => $paged);
发布评论

评论列表(0)

  1. 暂无评论