WordPress shortcode not passing variable

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!

Read More

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

Related posts

Leave a Reply

1 comment

  1. 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.