WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Pagination to WP_Query loop (2 posts)

  1. the.green.timtam
    Member
    Posted 1 year ago #

    I have a custom loop which includes custom taxonomies:

    <?php $args = array(
                        'post_type' => 'videos',
                        'posts_per_page' => 10,
                        'tax_query' => array(
                                               'relation' => 'AND',
                                               array(
                                                      'taxonomy' => 'category',
                                                      'field' => 'slug',
                                                      'terms' => array(
                                                                        'visual-arts'
                                                                       )
                                                    )
                                            )
                      ) ;
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <div id="archive-entry">
    <div id="archive-thumbnail"><a href="<?php the_permalink(); ?>"><?php
    if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
      the_post_thumbnail( array(200,200) );
    }
    else { ?>
    <img src="<?php echo get_bloginfo('template_url') ?>/image75px.png" />
    <?php } ?>
    </a></div><!-- #thumbnail -->
    <div id="archive-title">
    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
    </div><!-- #archive-title -->
    <div id="archive-excerpt">
    <?php the_excerpt(); ?>
    </div><!-- #archive-excerpt -->
    </div><!-- #archive-entry -->
    <?php endwhile;?>

    I want to add pagination to it. I found this: but it doesn't quite fit with the array method.

  2. the.green.timtam
    Member
    Posted 1 year ago #

    It was actually much easier than I thought:

    For anyone else with the same problem, simply add

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

    above the $args,

    'paged' => $paged,

    within the $args and

    <?php
    if($loop->max_num_pages>1){?>
        <p class="navrechts">
        <?php
        for($i=1;$i<=$loop->max_num_pages;$i++){?>
            <a href="<?php echo '?paged=' . $i; ?>" <?php echo ($paged==$i)? 'class="selected"':'';?>><?php echo $i;?></a>
            <?php
        } ?>
        </p>
    <?php } ?>

    after the loop to add navigation.

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array(
                        'post_type' => 'videos',
                        'posts_per_page' => 10,
                        'paged' => $paged,
                        'tax_query' => array(
                                               'relation' => 'AND',
                                               array(
                                                      'taxonomy' => 'category',
                                                      'field' => 'slug',
                                                      'terms' => array(
                                                                        'visual-arts'
                                                                       )
                                                    )
                                            )
                      ) ;
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <div id="archive-entry">
    <div id="archive-thumbnail"><a href="<?php the_permalink(); ?>"><?php
    if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
      the_post_thumbnail( array(200,200) );
    }
    else { ?>
    <img src="<?php echo get_bloginfo('template_url') ?>/image75px.png" />
    <?php } ?>
    </a></div><!-- #thumbnail -->
    <div id="archive-title">
    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
    </div><!-- #archive-title -->
    <div id="archive-excerpt">
    <?php echo substr(get_the_excerpt(), 0,1400); ?>
    </div><!-- #archive-excerpt -->
    </div><!-- #archive-entry -->
    <?php endwhile;?>
    <?php
    if($loop->max_num_pages>1){?>
        <p class="navrechts">
        <?php
        for($i=1;$i<=$loop->max_num_pages;$i++){?>
            <a href="<?php echo '?paged=' . $i; ?>" <?php echo ($paged==$i)? 'class="selected"':'';?>><?php echo $i;?></a>
            <?php
        } ?>
        </p>
    <?php } ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.