Pergunta

Hi I'm trying to do something rather tricky in my Wordpress Theme I'm more of a designer front-end developer so its a bit of a guess work with php. Anyone have any ideas why this code only displays one slide. I want it to run a slideshow pulling in multipe slides from one category in my theme.

Thanks For your help

    //Add Nivo Short Codes
function nivo_slider_function($atts){

   extract(shortcode_atts(array(
      'posts' => 5,
      'category' => ''
   ), $atts));

 $args = array(
        'numberposts' => -1,
        'orderby' => 'menu_order',
        'order' => 'ASC',
        'post_type' => 'homepage_slider'
    );

    if ( ! empty( $category ) ) {
        $args['category_name'] = $category;
    }

    $posts = get_posts( $args );

    $homepage_slider  = '<div id="slider">'; //Open the container
    foreach ( $posts as $post ) { // Generate the markup for each Question
        $homepage_slider .= sprintf(('<h3><a href="">%1$s</a></h3><div>%2$s</div>'),
            $post->post_title,
            wpautop($post->post_content)
        );
    }
    $homepage_slider .= '</div>'; //Close the container


   $return_string = '<div id="slider" class="nivoSlider">';

   query_posts(array('post_type' => 'homepage_slider', 'orderby' => 'menu_order', 'order' => 'ASC' , 'showposts' => $posts));

   if (have_posts()) :
      while (have_posts()) : the_post();

         $slider_img_src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'homepage-slide', false);
         $target_link = get_post_meta(get_the_ID(), 'slide_target_link', true);   
         $return_string .= '<a href="'.$target_link.'" rel="nofollow"><img src="' . $slider_img_src[0] .'" /></a>';

      endwhile;
   endif;
   $return_string .= '</div>';

   wp_reset_query();
   return $return_string;

}

function register_shortcodes(){
   add_shortcode('slider-demo', 'nivo_slider_function');
}

add_action( 'init', 'register_shortcodes');
Foi útil?

Solução

I see what you're doing and there is an easier way of doing this. Replace your slider with SMART SLIDER 2. It has different settings including one called dynamic. With this dynamic slider, you can use slides from a category within your site.

I discovered this just last week and I absolutely love the flexibility of this slider. It is rather heavy because of the different features it lets you do. So, if you are worried about that. Set it up as you would want it in a bogus WP site, strip the HTML, PHP, JS, and CSS and migrate it in your site's page. This obviously would be more work but you'll have a smaller code and you don't have to worry about figuring this out.

Good luck

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top