0

I'm newbie, I need some help.

There are two columns, each with their own independent pagination. How would i do this?

and if you can tell me how to add the function of the comments, I will thank you even more.

Here is my code so far:

            $paged1 = isset( $_GET['paged1'] ) ? (int) $_GET['paged1'] : 1;
            $paged2 = isset( $_GET['paged2'] ) ? (int) $_GET['paged2'] : 1;

$args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'orderby' => 'ID',
    'paged'          => $paged1,
    'order' => 'desc');

$blank_posts = array();
$content_post =array();

$posts = new WP_Query( $args );

if ( $posts->have_posts() ) : 
    while ( $posts->have_posts() ) : $posts->the_post();
    global $post;
       $content = get_the_content();
        if(empty($content)) { 
            array_push( $blank_posts, $post);
        }else{
            array_push( $content_post, $post);
        }
    endwhile;
endif;

if(!empty($blank_posts)){
    foreach ($blank_posts as $pst) {
        echo "ID= ". $pst->ID . ', '. "Title= ". $pst->post_title .'<hr />';
    }
            $pag_args1 = array(
                'format'  => '?paged1=%#%',
                'current' => $paged1,
                'total'   => $posts->max_num_pages,
                'add_args' => array( 'paged2' => $paged2 )
            );
        echo paginate_links( $pag_args1 );
}

if(!empty($content_post)){
    foreach ($content_post as $pst) {
        echo '<a href='. $pst->guid .'>' .$pst->post_title. '</a> ';
    }
            $pag_args2 = array(
                'format'  => '?paged1=%#%',
                'current' => $paged2,
                'total'   => $posts->max_num_pages,
                'add_args' => array( 'paged2' => $paged2 )
            );
        echo paginate_links( $pag_args2 );
}
  • Have you considered using the built in functionality and setting a homepage and a posts page? Then you can just use home.php and remove the custom query, use a standard main loop, and not have to worry about pagination ( also have you looked at the pre_get_posts filter? Replacing the main query is expensive and has all sorts of pitfalls such as pagination, but modifying the main query is much better, and avoids all the issues ) – Tom J Nowell Aug 3 '17 at 14:43
  • Also the pagination functions always refer to the main query, not your custom queries. A twin column page where each column has a separate pagination, but they both get their content from the same query sounds super, super complicated, and a terrible UX. I would strongly recommend against it – Tom J Nowell Aug 3 '17 at 14:45
  • @TomJNowell actually i need to do this, i want two columns of content, one column showing have featured image/video and no content, other one column showing have content, and i need this two column content have paging, because there's a lot of content, so i need you help me, give me some simple example, thanks man – metalbug Aug 3 '17 at 15:43
  • that makes no sense, you'll need a complicated URL schema just to keep track of which page each column is on, and 100% custom pagination, you wont be able to rely on the pagination links. Usually those kinds of UIs are 100% javascript, what you're asking for is not simple, it's complex. As an experienced developer I strongly recommend against this – Tom J Nowell Aug 3 '17 at 16:54
0

To do this, you will need to:

  • Use 2 queries not 1
  • 100% custom pagination, the built in pagination functions will never work with this, WP has no concept of 2 post main loops
  • Custom URLs

But before we go into this

A Significantly Easier Alternative That's Nicer for Users

Just list the first 5 posts with a view more button underneath. Have the button send the user to the archive WP generates automatically, and hey presto, you sidestep your issues, improve usability, remove all the complicated mess you needed for your original idea, and it's much more browser friendly. There's probably an SEO benefit too.

For example:

<a href="<?php echo get_term_link( 'videos', 'cat' ) ?>">View more Videos</a>

Will take the user to an archive of posts in the videos category, which you can theme with category.php, or even category-videos.php or archive.php etc. Significantly easier, and could even be turned into a widget or shortcode with minimal changes.

Complicated Pagination

Warning, what you're trying to do is complicated, and not in the sophisticated kind of way, but the kludgey kind of way. There's a good reason you don't see this often, and when it is done, it's a rich JS interface, not a PHP one.

First, you need 2 queries, you'll need to figure out how to query for what you want, I recommend using tags or categories. Sorting them using PHP will end up with strange results, e.g. if you fetch 20 posts, and 19 of them go in the left column, it'll be weird.

First things first, you need 2 queries, not 1:

$args1 = [
];
$q1 = new WP_Query( $args1 );

// display left column

$args2 = [
];
$q2 = new WP_Query( $args2 );

// display right column

Second, you'll need to set up pagination, you will need 2 pagination fields in the URL, and you'll need to handle pagination twice:

// figure out which page we're on, 1 if not set
$page1 = ( !empty( $_GET['column1'] )) ? (int)$_GET['column1'] : 1;
$page2 = ( !empty( $_GET['column2'] )) ? (int)$_GET['column2'] : 1;

$args1 = [
    'posts_per_page' => 5,
    'paged' => $page1
];
$q1 = new WP_Query( $args1 );

// display left column

$args2 = [
    'posts_per_page' => 5,
    'paged' => $page2
];
$q2 = new WP_Query( $args2 );

// display right column

Great, now we have 2 columns, and we can change which page they're on URL parameters, aka ?column1=5&column2=3 for page 5 and 3 respectively

But we have no pagination controls! And what if we ask for page 9000? Well we can't use the standard pagination system, because we decided to go for a complicated setup, so we have to build our own from scratch.

Here is the first columns pagination, assuming a simple next and previous page link:

if ( $page1 > 1 ) {
    ?>
    <a href="?column1=<?php echo $page1-1; ?>&column2=<?php echo $page2; ?>">Previous Page</a>
    <?php
}
?>
<a href="?column1=<?php echo $page1+1; ?>&column2=<?php echo $page2; ?>">Next Page</a>

The second columns pagination is the same, but with the math on the $page2 variable instead.

If you'd like pagination with page numbers instead that can be clicked to, this will be a lot more complicated as you'll need to figure out how many pages there are, loop through and print out each number with the relevant link, highlight the current page, etc

Comments

Having comments inline, and multiple comment areas, is not straight forward, and it sounds like a nightmarish user interface, but this question has the answer. Remember, Stack Exchange has a 1 question per question policy, you will need to ask a follow up question for comments if that doesn't satisfy your query.

Conclusion

Now you have all the pieces and info necessary to do what you want to do. Again I advise against doing it. If it breaks it'll be fiddly to debug, and it isn't user friendly which will put off visitors. The standard pattern on the web is a link that leads to a more comprehensive focused archive. Multiple levels of pagination is just confusing

Further Reading

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.