Php 编辑';My-Post循环';Wordpress小部件只发布特定的博客类别

Php 编辑';My-Post循环';Wordpress小部件只发布特定的博客类别,php,wordpress,widget,Php,Wordpress,Widget,我有一个wordpress主题和一个名为“My-Post Cycle”的小部件,它允许我从我的博客发布条目。我希望能够选择小部件显示哪些帖子“类别”。有了“category ID”,我如何编辑小部件PHP以只显示我选择的category ID。代码如下: <?php // =============================== My Post Cycle widget ====================================== class MY_CycleWidg

我有一个wordpress主题和一个名为“My-Post Cycle”的小部件,它允许我从我的博客发布条目。我希望能够选择小部件显示哪些帖子“类别”。有了“category ID”,我如何编辑小部件PHP以只显示我选择的category ID。代码如下:

<?php
// =============================== My Post Cycle widget ======================================
class MY_CycleWidget extends WP_Widget {
    /** constructor */
    function MY_CycleWidget() {
        parent::WP_Widget(false, $name = 'My - Post Cycle');    
    }

    /** @see WP_Widget::widget */
    function widget($args, $instance) {     
        extract( $args );
        $title = apply_filters('widget_title', $instance['title']);
                $limit = apply_filters('widget_limit', $instance['limit']);
                $category = apply_filters('widget_category', $instance['category']);
                $count = apply_filters('widget_count', $instance['count']);
        ?>
              <?php echo $before_widget; ?>
                  <?php if ( $title )
                        echo $before_title . $title . $after_title; ?>

                        <?php if($category=="testi"){?>
                    <script type="text/javascript">
                                    jQuery(function(){
                                        jQuery('#slides').slides({
                                            effect: 'fade',
                                            crossfade: true,
                                            preload: true,
                                            generateNextPrev: true,
                                            autoHeight: true
                                        });
                                    });
                                </script>
                <div id="slides">
                                    <div class="slides_container">

                                    <?php $limittext = $limit;?>
                                    <?php global $more; $more = 0;?>
                                    <?php query_posts("posts_per_page=". $count ."&post_type=" . $category);?>

                                    <?php while (have_posts()) : the_post(); ?> 

                                        <?php 
                                        $custom = get_post_custom($post->ID);
                                        $testiname = $custom["my_testi_caption"][0];
                                        $testiurl = $custom["my_testi_url"][0];
                                        $testiinfo = $custom["my_testi_info"][0];
                                        ?>

                                    <div class="testi_item">

                                    <?php if($limittext=="" || $limittext==0){ ?>
                                        <?php the_excerpt(); ?>
                                    <?php }else{ ?>
                                        <?php $excerpt = get_the_excerpt(); echo my_string_limit_words($excerpt,$limittext);?>
                                    <?php } ?>

                                     <div class="name-testi">
                                     <span class="user"><?php echo $testiname; ?></span>,
                                     <a href="http://<?php echo $testiurl; ?>"><?php echo $testiurl; ?></a><br/>
                                     <span class="info"><?php echo $testiinfo; ?></span>
                                     </div>

                                    </div>

                                    <?php endwhile; ?>
                                    <?php wp_reset_query(); ?>
                                </div>
                            </div>
                            <!-- end of testimonials -->



                        <?php } elseif($category=="portfolio"){ ?>
                            <script type="text/javascript">
                                jQuery(function(){
                                    jQuery('#slides').slides({
                                        effect: 'fade',
                                        crossfade: true,
                                        preload: true,
                                        generateNextPrev: true,
                                        autoHeight: true
                                    });
                                });
                            </script>
                            <div id="slides">
                                <div class="slides_container">
                                    <?php $limittext = $limit;?>
                                    <?php global $more; $more = 0;?>
                                    <?php query_posts("posts_per_page=". $count ."&post_type=" . $category);?>
                                    <?php while (have_posts()) : the_post(); ?> 

                                    <?php
                                    $thumb = get_post_thumbnail_id();
                                    $img_url = wp_get_attachment_url( $thumb,'full'); //get img URL
                                    $image = aq_resize( $img_url, 250, 150, true ); //resize & crop img
                                    ?>
                                    <div class="item">
                                        <?php if($limittext=="" || $limittext==0){ ?>
                                        <figure class="featured-thumbnail">
                                            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><img src="<?php echo $image ?>" alt="<?php the_title(); ?>" /></a>
                                        </figure>
                                        <?php }else{ ?>
                                        <figure class="featured-thumbnail">
                                            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><img src="<?php echo $image ?>" alt="<?php the_title(); ?>" /></a>
                                        </figure>
                                        <?php $excerpt = get_the_excerpt(); echo my_string_limit_words($excerpt,$limittext); } ?>
                                    </div>
                                    <?php endwhile; ?>
                                    <?php wp_reset_query(); ?>
                                </div>
              </div>
              <!-- end of portfolio_cycle -->

                        <?php } else { ?>

<script type="text/javascript">
    jQuery(document).ready(function(){

        jQuery('#slider-code').tinycarousel({ axis: 'y'});

    });
</script>

<div id="slider-code">
    <a class="buttons prev" href="#">left</a>
    <div class="viewport">
        <ul class="overview">
                                    <?php $limittext = $limit;?>
                                    <?php global $more; $more = 0;?>
                                    <?php query_posts("posts_per_page=" . $count . "&post_type=" . $category);?>
                                    <?php while (have_posts()) : the_post(); ?> 

                                    <?php 
                                        $custom = get_post_custom($post->ID);
                                        $period = $custom["period"][0];
                                    ?>

                                    <li>
                                        <?php if($limittext=="" || $limittext==0){ ?>
                                            <h5><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h5>
                                            <b><?php echo $period; ?></b>
                                            <em><a href="#"><?php _e('view details', 'theme1831'); ?></a></em>
                                        <?php }else{ ?>
                                            <h5><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h5>
                                            <b><?php echo $period; ?></b>
                                            <div class="excerpt"><?php $excerpt = get_the_excerpt(); echo my_string_limit_words($excerpt,$limittext); ?></div>
                                            <em><a href="#"><?php _e('view details', 'theme1831'); ?></a></em>
                                        <?php } ?>
                                    </li>
                                    <?php endwhile; ?>
                                    <?php wp_reset_query(); ?>
        </ul>
    </div>
    <a class="buttons next" href="#">right</a>
</div>

                            <!-- end of post_cycle -->
                            <?php }?>
              <?php echo $after_widget; ?>
        <?php
    }

    /** @see WP_Widget::update */
    function update($new_instance, $old_instance) {             
        return $new_instance;
    }

    /** @see WP_Widget::form */
    function form($instance) {              
            $title = esc_attr($instance['title']);
            $limit = esc_attr($instance['limit']);
            $category = esc_attr($instance['category']);
            $count = esc_attr($instance['count']);
    ?>
      <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'theme1831'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></label></p>

      <p><label for="<?php echo $this->get_field_id('limit'); ?>"><?php _e('Limit Text:', 'theme1831'); ?> <input class="widefat" id="<?php echo $this->get_field_id('limit'); ?>" name="<?php echo $this->get_field_name('limit'); ?>" type="text" value="<?php echo $limit; ?>" /></label></p>
      <p><label for="<?php echo $this->get_field_id('count'); ?>"><?php _e('Posts per page:', 'theme1831'); ?><input class="widefat" style="width:30px; display:block; text-align:center" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" type="text" value="<?php echo $count; ?>" /></label></p>

      <p><label for="<?php echo $this->get_field_id('category'); ?>"><?php _e('Show profile link:', 'theme1831'); ?><br />

      <select id="<?php echo $this->get_field_id('category'); ?>" name="<?php echo $this->get_field_name('category'); ?>" style="width:150px;" > 
      <option value="testi" <?php echo ($category === 'testi' ? ' selected="selected"' : ''); ?>>Testimonials</option>
      <option value="portfolio" <?php echo ($category === 'portfolio' ? ' selected="selected"' : ''); ?> >Portfolio</option>
      <option value="" <?php echo ($category === '' ? ' selected="selected"' : ''); ?>>Blog</option>
      </select>
      </label></p>

      <?php 
    }

} // class Cycle Widget


?>

jQuery(函数(){
jQuery(“#幻灯片”)。幻灯片({
效果:“褪色”,
是的,
预加载:正确,
generateNextPrev:对,
自动高度:真
});
});
,

jQuery(函数(){ jQuery(“#幻灯片”)。幻灯片({ 效果:“褪色”, 是的, 预加载:正确, generateNextPrev:对, 自动高度:真 }); }); jQuery(文档).ready(函数(){ jQuery(“#滑块代码”).tinycarousel({axis:'y'}); });

您应该将参数
post\u type
category\u name
更改为
query\u posts()
函数,该函数在
小部件中调用

<?php query_posts("posts_per_page=". $count . "&category_name=" . $category); ?>


参考资料:

在查看发布的参考资料时,我使用了&cat并得出了以下结论:
,然后
为什么使用
cat
?我使用了
category\u name
,因为您处理的是类别slug而不是id;我将其更改为您提供的代码,并得出以下结论:
应该是:
我注意到它实际上被调用了3次,我用您提供的代码替换了所有3个实例,然后添加了

<?php query_posts("posts_per_page=". $count . "&category_name=" . $category); ?>