2
votes

Here's the code. It's only displaying one image regardless of the category name that the shortcode uses.

<?php


// Create Slider

function wptuts_slider_template($category_name) {
    global $post;
    // Query Arguments
    $args = array(
                'post_type' => 'slides',
                'order' => 'ASC',
                'category_name' => $category_name
            );

    // The Query
    $the_query = new WP_Query( $args );

    // Check if the Query returns any posts
    if ( $the_query->have_posts() ) {

    // Start the Slider ?>
        <ul class="slides <?php wp_title(); ?> <?php echo $category_name ?>" data-options="animation:fade;
                          pause_on_hover:true;
                          resume_on_mouseout: true;
                          timer_speed:3000;
                          animation_speed:500;
                          navigation_arrows:true;
                          bullets:true;
                          slide_number:false;
                          next_on_click:true;
                          timer: true;" data-orbit>

            <?php       
            // The Loop
            while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
                <li>

                <?php // Check if there's a Slide URL given and if so let's a link to it
                if ( get_post_meta( get_the_id(), 'wptuts_slideurl', true) != '' ) { ?>
                    <a href="<?php echo esc_url( get_post_meta( get_the_id(), 'wptuts_slideurl', true ) ); ?>">
                <?php }

                // The Slide's Image
                echo get_the_post_thumbnail();

                // Close off the Slide's Link if there is one
                if ( get_post_meta( get_the_id(), 'wptuts_slideurl', true) != '' ) { ?>
                    </a>
                <?php } ?>

                </li>
            <?php endwhile; ?>

        </ul><!-- .slides -->

    <?php }

    // Reset Post Data
    wp_reset_postdata();
}

// Slider Shortcode

function wptuts_slider_shortcode($atts) {
    extract(shortcode_atts(array(
       "category_name" => 'other',
    ), $atts));
    ob_start();
    wptuts_slider_template($category_name);
    $slider = ob_get_clean();
    return $slider;
}
add_shortcode( 'slider', 'wptuts_slider_shortcode' );

What is causing this not to work? I've echoed the category_name in the loop's class and it outputs the variable correctly. Please help!

Update* [slider category_name="main"] is shortcode example.

1
Are you using your shortcode like this [slider category_name="our-super-category"]? - Dima Knivets
Yes, that is the shortcode format. - jamzth
add 'posts_per_page' => -1 to your $args ... then try. currently you have not defined the number of posts it can get ... may be that is the problem. - Nishant
That's a no go. I previously had defined that 5, and it doesn't work with -1 either. Thanks for the help so far. - jamzth

1 Answers

0
votes

You are extracting to $atts['other'] in the shortcode function. You should update

wptuts_slider_template($category_name);

to

wptuts_slider_template($atts['other']);

in that same function to pass in the data correctly.