简体   繁体   English

如何为自定义帖子类型调用简码类别

[英]How to call Shortcode categories for custom post types

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. 嗨,我正尝试在我的Wordpress主题中做一些棘手的事情,我更是一名设计师前端开发人员,因此使用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');

I see what you're doing and there is an easier way of doing this. 我知道您在做什么,并且有一种更简单的方法。 Replace your slider with SMART SLIDER 2 . 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. 在虚假的WP网站中进行设置,剥离HTML,PHP,JS和CSS,然后在网站页面中进行迁移。 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 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM