编辑';我的-后周期';Wordpress小部件只发布特定的博客类别


Edit 'My - Post Cycle' Wordpress widget to post only specific blog categories

我有一个wordpress主题,其中有一个名为"My-Post Cycle"的小部件,它允许我发布博客中的条目。我希望能够选择哪些帖子"类别"与小部件一起显示。有了"类别ID",我如何编辑小部件PHP以只显示我选择的类别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

?>

谢谢你的帮助。

更新:

我无法让"我的帖子周期"小部件按我的意愿工作,所以我决定使用另一个主题附带的小部件("我最近的帖子")。它看起来没有另一个好看,但它允许我发布一个类别的帖子。也许看到代码,会让人想办法让第一个小部件像这个一样工作,但保持风格。这是它的代码:

<?php
// =============================== My Recent Posts (News widget) ======================================
class MY_PostWidget extends WP_Widget {
    /** constructor */
    function MY_PostWidget() {
        parent::WP_Widget(false, $name = 'My - Recent Posts');  
    }
  /** @see WP_Widget::widget */
    function widget($args, $instance) {     
        extract( $args );
        $title = apply_filters('widget_title', $instance['title']);
                $category = apply_filters('widget_category', $instance['category']);
                $post_format = apply_filters('widget_post_format', $instance['post_format']);
                $linktext = apply_filters('widget_linktext', $instance['linktext']);
                $linkurl = apply_filters('widget_linkurl', $instance['linkurl']);
                $count = apply_filters('widget_count', $instance['count']);
                $sort_by = apply_filters('widget_sort_by', $instance['sort_by']);
                $excerpt_count = apply_filters('widget_excerpt_count', $instance['excerpt_count']);
        ?>
              <?php echo $before_widget; ?>
                  <?php if ( $title )
                        echo $before_title . $title . $after_title; ?>

                        <?php if($post_format == 'post-format-standard') { 
                            $args = array(
                                        'showposts' => $count,
                                        'category_name' => $category,
                                        'orderby' => $sort_by,
                                        'tax_query' => array(
                                         'relation' => 'AND',
                                            array(
                                                'taxonomy' => 'post_format',
                                                'field' => 'slug',
                                                'terms' => array('post-format-aside', 'post-format-gallery', 'post-format-link', 'post-format-image', 'post-format-quote', 'post-format-audio', 'post-format-video'),
                                                'operator' => 'NOT IN'
                                            )
                                        )
                                        );
                        } else { 
                            $args = array(
                                'showposts' => $count,
                                'category_name' => $category,
                                'orderby' => $sort_by,
                                'tax_query' => array(
                                 'relation' => 'AND',
                                    array(
                                        'taxonomy' => 'post_format',
                                        'field' => 'slug',
                                        'terms' => array($post_format)
                                    )
                                )
                            );
                        } ?>


                        <?php $wp_query = new WP_Query( $args ); ?>
                                <ul class="latestpost">
                                <?php if ($wp_query->have_posts()) : while ($wp_query->have_posts()) : $wp_query->the_post();?>
                                <li class="clearfix">
                                    <?php if(has_post_thumbnail()) { ?>
                                        <?php
                                        $thumb = get_post_thumbnail_id();
                                        $img_url = wp_get_attachment_url( $thumb,'full'); //get img URL
                                        $image = aq_resize( $img_url, 100, 100, true ); //resize & crop img
                                        ?>
                                        <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 } ?>
                  <time datetime="<?php the_time('Y-m-d'TH:i'); ?>"><?php the_time('F j, Y'); ?></time>
                  <h4><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php _e('Permanent Link to', 'theme1831');?> <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h4>
                                    <?php if($excerpt_count!="") { ?>
                                    <div class="excerpt">
                  <?php $excerpt = get_the_excerpt(); echo my_string_limit_words($excerpt,$excerpt_count);?>
                                    </div>
                                    <?php } ?>
                                    <a href="<?php the_permalink() ?>" class="link"><?php _e('Read more', 'theme1831'); ?></a>
                                </li>
                                <?php endwhile; ?>
                                </ul>
                                <?php endif; ?>
                                <?php $wp_query = null; $wp_query = $temp;?>

                                <!-- Link under post cycle -->
                                <?php if($linkurl !=""){?>
                                    <a href="<?php echo $linkurl; ?>" class="button"><?php echo $linktext; ?></a>
                                <?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']);
            $category = esc_attr($instance['category']);
            $post_format = esc_attr($instance['post_format']);
            $linktext = esc_attr($instance['linktext']);
            $linkurl = esc_attr($instance['linkurl']);
            $count = esc_attr($instance['count']);
            $sort_by = esc_attr($instance['sort_by']);
            $excerpt_count = esc_attr($instance['excerpt_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('category'); ?>"><?php _e('Category Slug:', 'theme1831'); ?> <input class="widefat" id="<?php echo $this->get_field_id('category'); ?>" name="<?php echo $this->get_field_name('category'); ?>" type="text" value="<?php echo $category; ?>" /></label></p>
            <p><label for="<?php echo $this->get_field_id('post_format'); ?>"><?php _e('Post format:', 'theme1831'); ?><br />
      <select id="<?php echo $this->get_field_id('post_format'); ?>" name="<?php echo $this->get_field_name('post_format'); ?>" style="width:150px;" > 
            <option value="post-format-standard" <?php echo ($post_format === 'post-format-standard' ? ' selected="selected"' : ''); ?>>Standard</option>
      <option value="post-format-aside" <?php echo ($post_format === 'post-format-aside' ? ' selected="selected"' : ''); ?>>Aside</option>
            <option value="post-format-quote" <?php echo ($post_format === 'post-format-quote' ? ' selected="selected"' : ''); ?> >Quote</option>
            <option value="post-format-link" <?php echo ($post_format === 'post-format-link' ? ' selected="selected"' : ''); ?> >Link</option>
            <option value="post-format-image" <?php echo ($post_format === 'post-format-image' ? ' selected="selected"' : ''); ?> >Image</option>
      <option value="post-format-gallery" <?php echo ($post_format === 'post-format-gallery' ? ' selected="selected"' : ''); ?> >Gallery</option>
            <option value="post-format-audio" <?php echo ($post_format === 'post-format-audio' ? ' selected="selected"' : ''); ?> >Audio</option>
            <option value="post-format-video" <?php echo ($post_format === 'post-format-video' ? ' selected="selected"' : ''); ?> >Video</option>
      </select>
      </label></p>
      <p><label for="<?php echo $this->get_field_id('sort_by'); ?>"><?php _e('Post order:', 'theme1831'); ?><br />
      <select id="<?php echo $this->get_field_id('sort_by'); ?>" name="<?php echo $this->get_field_name('sort_by'); ?>" style="width:150px;" > 
            <option value="date" <?php echo ($sort_by === 'date' ? ' selected="selected"' : ''); ?>>Date</option>
          <option value="title" <?php echo ($sort_by === 'title' ? ' selected="selected"' : ''); ?>>Title</option>
          <option value="comment_count" <?php echo ($sort_by === 'comment_count' ? ' selected="selected"' : ''); ?>>Comment Count</option>
          <option value="rand" <?php echo ($sort_by === 'rand' ? ' selected="selected"' : ''); ?>>Random</option>
      </select>
      </label></p>
      <p><label for="<?php echo $this->get_field_id('count'); ?>"><?php _e('Posts per page:'); ?><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('excerpt_count'); ?>"><?php _e('Excerpt length (words):'); ?><input class="widefat" style="width:30px; display:block; text-align:center" id="<?php echo $this->get_field_id('excerpt_count'); ?>" name="<?php echo $this->get_field_name('excerpt_count'); ?>" type="text" value="<?php echo $excerpt_count; ?>" /></label></p>
             <p><label for="<?php echo $this->get_field_id('linktext'); ?>"><?php _e('Link Text:', 'theme1831'); ?> <input class="widefat" id="<?php echo $this->get_field_id('linktext'); ?>" name="<?php echo $this->get_field_name('linktext'); ?>" type="text" value="<?php echo $linktext; ?>" /></label></p>
             <p><label for="<?php echo $this->get_field_id('linkurl'); ?>"><?php _e('Link Url:', 'theme1831'); ?> <input class="widefat" id="<?php echo $this->get_field_id('linkurl'); ?>" name="<?php echo $this->get_field_name('linkurl'); ?>" type="text" value="<?php echo $linkurl; ?>" /></label></p>
        <?php 
    }
} // class Widget
?>

您应该将参数post_typecategory_name更改为widget方法内部调用的query_posts()函数。

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

参考:WP_Query Category Parameters