$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>pagination - Custom category.php paging shows the same posts|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

pagination - Custom category.php paging shows the same posts

matteradmin18PV0评论

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);
Post a comment

comment list (0)

  1. No comments so far