我想编辑此小部件以显示当前类别中的相关帖子。下面的代码仅显示我必须手动选择的类别中的帖子。我想做的是,这个小部件显示来自该类别的相关帖子。谢谢你的帮助
add_action( \'widgets_init\', \'catposts_load_widgets\' );
function catposts_load_widgets() {
register_widget( \'Catposts_Widget\' );
}
class Catposts_Widget extends WP_Widget {
function Catposts_Widget() {
/* Widget settings. */
$widget_ops = array( \'classname\' => \'catposts\', \'description\' => __(\'Adds posts from a specific category .\', "solostream") );
/* Widget control settings. */
$control_ops = array( \'width\' => 300, \'height\' => 350, \'id_base\' => \'catposts-widget\' );
/* Create the widget. */
$this->WP_Widget( \'catposts-widget\', __(\'Category Posts Widget\', "solostream"), $widget_ops, $control_ops );
}
function widget( $args, $instance ) {
global $post;
$post_old = $post; // Save the post object.
extract( $args );
// If no title, use the name of the category.
if( !$instance["title"] ) {
$category_info = get_category($instance["cat"]);
$instance["title"] = $category_info->name;
}
// Get array of post info.
$cat_posts = new WP_Query("showposts=" . $instance["num"] . "&cat=" . $instance["cat"]);
/* Before widget (defined by themes). */
echo $before_widget;
// Widget title
echo $before_title;
if( $instance["title_link"] )
echo \'<a href="\' . get_category_link($instance["cat"]) . \'">\' . $instance["title"] . \'</a>\';
else
echo $instance["title"];
echo $after_title;
// Post list
echo "<div class=\'cat-posts-widget\'>\\n";
while ( $cat_posts->have_posts() )
{
$cat_posts->the_post();
?>
<div class="post" id="catpost-<?php the_ID(); ?>">
<div class="entry clearfix">
<a href="<?php the_permalink() ?>" rel="<?php _e("bookmark", "solostream"); ?>" title="<?php _e("Permanent Link to", "solostream"); ?> <?php the_title(); ?>"><?php include (TEMPLATEPATH . "/post-thumb.php"); ?></a>
<p class="post-title"><a href="<?php the_permalink() ?>" rel="<?php _e("bookmark", "solostream"); ?>" title="<?php _e("Permanent Link to", "solostream"); ?> <?php the_title(); ?>"><?php the_title(); ?></a></p>
<?php the_excerpt(); ?>
</div>
<?php include (TEMPLATEPATH . "/postinfo.php"); ?>
<div style="clear:both;"></div>
</div>
<?php
}
echo "</div>\\n";
echo $after_widget;
remove_filter(\'excerpt_length\', $new_excerpt_length);
$post = $post_old; // Restore the post object.
}
function form($instance) {
?>
<p>
<label for="<?php echo $this->get_field_id("title"); ?>">
<?php _e( \'Title\' ); ?>:
<input class="widefat" id="<?php echo $this->get_field_id("title"); ?>" name="<?php echo $this->get_field_name("title"); ?>" type="text" value="<?php echo esc_attr($instance["title"]); ?>" />
</label>
</p>
<p>
<label>
<?php _e( \'Category\' ); ?>:
<?php wp_dropdown_categories( array( \'name\' => $this->get_field_name("cat"), \'selected\' => $instance["cat"] ) ); ?>
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id("num"); ?>">
<?php _e(\'Number of Posts to Show\'); ?>:
<input style="text-align: center;" id="<?php echo $this->get_field_id("num"); ?>" name="<?php echo $this->get_field_name("num"); ?>" type="text" value="<?php echo absint($instance["num"]); ?>" size=\'3\' />
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id("title_link"); ?>">
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id("title_link"); ?>" name="<?php echo $this->get_field_name("title_link"); ?>"<?php checked( (bool) $instance["title_link"], true ); ?> />
<?php _e( \'Make widget title link\' ); ?>
</label>
</p>
<?php
}
}
SO网友:Stephen Harris
因此,您希望显示“当前类别”中的帖子。这是可行的,但您需要考虑在没有“当前类别”的情况下要做什么,例如index.php
. 你还需要决定在一个页面上做什么——你是否从当前查看的帖子中选择了一个类别。
以下查询发布:
查看类别时,在同一类别中,查看单个帖子时,在属于帖子的某些类别中,否则不受类别限制showposts
已弃用,请使用posts_per_page
而是:
$args = array(
\'posts_per_page\'=>$instance["num"],
);
if( is_category() ){
$args[\'cat\'] = get_queried_object_id();
}elseif( is_singular() || is_single() ){
$cats = get_the_terms(get_the_ID(),\'category\');
if( $cats ){
$cat = array_pop($cat);
$args[\'cat\'] = (int) $cat->term_id;
}
}
$cat_posts = new WP_Query($args);