1

I have a picture gallery that is working, I am converting it to a shortcode so the positioning can be flexible and I don't have to copy the code over and over again.

The issue I am having is that the pagination doesn't advance to the next page. The images show, the pagination shows, the page number shows...but when I try and click on page two or three the images, pagination, and page numbers don't change. The URL changes, the content doesn't.

I have a page with some manual code (that works and advances or changes when pagination is clicked).

The site I am working on is: http://joshrodg.com/test/banquet/2017-induction-banquet/. My test shortcode is on top (underneath 2017 Induction Banquet), the one that works, is the bottom one (manual code).

My shortcode looks like: [halloffame rml_folder="16"]

My function looks like:

function picture_gallery($atts){
    extract(shortcode_atts(array(
        'rml_folder' => 1
    ), $atts));

    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    query_posts("post_status=inherit&post_type=attachment&rml_folder=".$rml_folder."&orderby=title&order=asc&posts_per_page=12&paged=".$paged);

    if ( have_posts() ) :
        $return_string .= '<div id="album">';
            while ( have_posts() ) : the_post();
                $return_string .= '<div class="gallery">';
                    $image = wp_get_attachment_image(get_post_thumbnail_id($post->ID), 'medium');
                    $return_string .= '<a href="'.wp_get_attachment_url($post->ID).'" class="simplelightbox">'.$image.'</a>';
                    $return_string .= '<p>'.get_the_title().'</p>';
                $return_string .= '</div>';
            endwhile;
        $return_string .= '</div>';
    endif;

    $return_string .= '<div id="pagi">';
        $return_string .= '<div class="wrap">';
            $args = array(
                'prev_text' => __('<span class="left"></span><span class="ion-android-arrow-dropleft"></span>'),
                'next_text' => __('<span class="ion-android-arrow-dropright"></span><span class="right"></span>')
            );
            $return_string .= paginate_links($args);
        $return_string .= '</div>';
    $return_string .= '</div>';

    global $wp_query;
    $current_page = get_query_var( 'paged' );
    $pages = $wp_query->max_num_pages;
    $return_string .= '<p align="center">(Page: '.$current_page.' of '.$pages.')</p>';

    wp_reset_query();

    return $return_string;
}

function register_shortcodes(){
    add_shortcode('halloffame', 'picture_gallery');
}
add_action( 'init', 'register_shortcodes');

Because this is working with the manual code, I'm guessing something is not quite right with my shortcode code.

Any ideas?

** UPDATE **

So, on the page http://joshrodg.com/test/banquet/, the pagination is broken, but on http://joshrodg.com/test/banquet/2017-induction-banquet/ the pagination works. 2017-induction-banquet is the original post, it's a child post of http://joshrodg.com/test/banquet/. On http://joshrodg.com/test/banquet/, I am displaying the content of all my child pages. So, the shortcode itself is working, just not on this page - so there might be something wrong with my loop.

The loop for this page looks like:

<?php query_posts("order=asc&orderby=menu_order&posts_per_page=-1&post_type=page&post_parent=".$post->ID); ?>
    <?php while ( have_posts() ) : the_post(); ?>
        <div class="wrap">
            <h2><?php the_title(); ?></h2>
        </div>
        <p><?php the_content(); ?></p>
    <?php endwhile; ?>
<?php wp_reset_query(); ?>

I'm thinking there might be a conflict with the loop in the shortcode and the loop on this page.

The pagination on the original page looks like: http://joshrodg.com/test/banquet/2017-induction-banquet/page/2/

The pagination on the parent page looks like: http://joshrodg.com/test/banquet/page/2/

Not sure if that makes a difference, but figured it's worth mentioning.

Thanks,
Josh

  • Have you verified that get_query_var( 'paged' ) returns the correct page number? – Milo Jan 10 '18 at 5:04
1

Don't use a hyphen in the shortcode name. See https://codex.wordpress.org/Shortcode_API#Hyphens .

  • This seems more like a comment than an answer. Could you expand upon why hyphens would mess up the shortcode's pagination? – Howdy_McGee Jan 10 '18 at 3:59
  • The explanation is in the Code link specifically regarding hyphens in short codes. They are not recommended as they can cause inconsistent results, as explained in the Codex. – Rick Hellewell Jan 10 '18 at 20:12
  • Hey there, I removed the hyphen, and the issue persists...still looking into this. – Josh Rodgers Jan 16 '18 at 2:17
  • I did notice the shortcode works on the child page...but when the child page content is displayed on the parent page, that's when it breaks. I didn't realize this previously...so, the issue might be with the code I am using on the parent page - not with the shortcode itself. I have updated my original description of the issue. – Josh Rodgers Jan 16 '18 at 2:32
  • It looks like you are using two queries: one for the loop, and one for the shortcode. I think (quick look) that the second query is replacing the first one (for the loop). Maybe this page will help? digwp.com/2011/09/3-ways-to-reset-the-wordpress-loop – Rick Hellewell Jan 16 '18 at 2:58
0

So, I managed to find a solution.

I tried to change query_posts on my shortcode, but for some reason, I wasn't able to get that working. I changed the query on the page which fixed the issue.

The query now looks like:

<?php
    $args = array(
        'order' => 'asc',
        'orderby' => 'menu_order',
        'posts_per_page' => -1,
        'post_type' => 'page',
        'post_parent' => $post->ID

    );

    $query = new WP_Query( $args );
?>

    <?php while ( $query->have_posts() ) : $query->the_post(); ?>
        <div class="wrap">
            <h2><?php the_title(); ?></h2>
        </div>
        <p><?php the_content(); ?></p>
    <?php endwhile; ?>
<?php wp_reset_postdata(); ?>

The shortcode did not change (other than removing the hyphen).

So, the two queries were conflicting with each other. The different queries query_posts and WP_Query fixed the issue.

Thanks,
Josh

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.