有没有小工具可以显示特定的Custom Post Type术语?

时间:2011-12-14 作者:Lee

我正在寻找一个小部件,用户可以从列表中选择自定义的帖子类型术语。例如,我有一个名为events的自定义帖子类型,它有多个类别。我希望用户能够选择只显示某个类别的帖子。

有人知道吗,或者知道我怎么做?

1 个回复
最合适的回答,由SO网友:Philip 整理而成

我已经建好了,前一段时间有一个小部件,还可以设置帖子的数量、类别和顺序!

<?php
/****** Widget ******/
class Artdev_Category_Posts extends WP_Widget {

    function Artdev_Category_Posts() {
        $widget_ops = array(\'description\' => \'Add Selected Category Posts to the Sidebar\' );
        parent::WP_Widget(false, __(\'&rarr; Category List\', \'artdev\'),$widget_ops);      
    }

    function widget($args, $instance) {  
        extract( $args );
        $title = esc_attr($instance[\'title\']);
        $cat_name = esc_attr($instance[\'cat_name\']);
        $posts = esc_attr($instance[\'posts\']);
        $order = esc_attr($instance[\'order\']);

        echo $before_widget; ?>

            <?php if ( $title ) echo $before_title . $title . $after_title; ?>

            <?php // The Loop
            $wq = new WP_Query();
            $wq->query( array( \'post_type\' => \'events\', \'category_name\' => $cat_name, \'posts_per_page\' => $posts, \'orderby\' => $order )); 
            if( $wq->have_posts() ) :
            ?>
            <ul class="widgetlist">
                <?php while($wq->have_posts()) : $wq->the_post(); ?>
                    <li>
                        <h3><a href="<?php the_permalink(); ?>" rel="bookmark"><?php echo get_the_title(); ?></a></h3>
                    </li>
                <?php endwhile; ?>
            </ul>
            <?php endif; wp_reset_query(); ?>

            <?//php } ?>

       <?php echo $after_widget;
   }

   function update($new_instance, $old_instance) {                
       return $new_instance;
   }

   function form($instance) {        
        $title = esc_attr($instance[\'title\']);
        $cat_name = esc_attr($instance[\'cat_name\']);
        $posts = esc_attr($instance[\'posts\']);
        $order = esc_attr($instance[\'order\']);
        ?>

        <p><!-- Widget Title -->
            <label for="<?php echo $this->get_field_id(\'title\'); ?>"><?php _e(\'Title:\',\'artdev\'); ?></label>
            <input type="text" name="<?php echo $this->get_field_name(\'title\'); ?>" value="<?php echo $title; ?>" class="widefat" id="<?php echo $this->get_field_id(\'title\'); ?>" />
        </p>

        <p><!-- Category Name -->
            <label for="<?php echo $this->get_field_id(\'cat_name\'); ?>"><?php _e(\'Category Name:\',\'artdev\'); ?></label>
            <input type="text" name="<?php echo $this->get_field_name(\'cat_name\'); ?>" value="<?php echo $cat_name; ?>" id="<?php echo $this->get_field_id(\'cat_name\'); ?>" class="widefat" />
        </p>

        <p><!-- Number Of Posts -->
            <label for="<?php echo $this->get_field_id(\'posts\'); ?>"><?php _e(\'Number of posts to show:\',\'artdev\'); ?></label>
            <input type="text" name="<?php echo $this->get_field_name(\'posts\'); ?>" id="<?php echo $this->get_field_id(\'posts\'); ?>" value="<?php echo $posts; ?>" size="2" />
        </p>

        <p><!-- Order Posts -->
            <label for="<?php echo $this->get_field_id(\'order\'); ?>"><?php _e(\'Order of posts:\',\'artdev\'); ?></label>
            <select name="<?php echo $this->get_field_name(\'order\'); ?>">
                <option value="title" <?php if ($order == \'title\') { echo \'selected="selected"\'; } ?>>Order Posts by Title</option>
                <option value="ID" <?php if ($order == \'ID\') { echo \'selected="selected"\'; } ?>>Order Posts by ID</option>
                <option value="date" <?php if ($order == \'date\') { echo \'selected="selected"\'; } ?>>Order Posts by Date</option>
                <option value="rand" <?php if ($order == \'rand\') { echo \'selected="selected"\'; } ?>>Randomize Posts</option>
            </select>
        </p>

        <?php
    }
} 
register_widget(\'Artdev_Category_Posts\'); 
?>

结束

相关推荐