如何在最近发布的帖子小工具区域中不显示子类别?

时间:2016-06-06 作者:Noey

我对Wordpress和一般的编码都比较陌生。我正在尝试编辑特定主题的最近帖子小部件,以仅显示特定类别内的帖子。目前,该主题将在一个类别中显示带有视频缩略图的最新帖子,但也将在该类别的任何子类别中包含最新帖子。我do not want 显示出现在该类别子类别中的帖子。这能做到吗?代码如下所示。

小部件主页发布要分类的文件:

function widget( $args, $instance ) {
    extract( $args );

    // If user have not selected any category then display none.
    if ( empty( $instance[\'cat\'] ) ) {
        return;
    }

    // Output the theme\'s $before_widget wrapper.
    echo $before_widget;

        // Pull the selected category.
        $cat_id = $instance[\'cat\'];

        // Get the category.
        $category = get_category( $cat_id );

        // Get the category archive link.
        $cat_link = get_category_link( $cat_id );

        // Posts query arguments.
        $args = array(
            \'post_type\'      => \'post\',
            \'posts_per_page\' => 3
        );

        // Limit to category based on user selected tag.
        if ( ! empty( $instance[\'cat\'] ) ) {
            $args[\'cat\'] = $instance[\'cat\'];
        }

        // Allow dev to filter the post arguments.
        $query = apply_filters( \'videoz_home_recent_args\', $args );

        // The post query.
        $posts = new WP_Query( $query );
最近发布的小部件代码:

 class VideoZ_Recent_Widget extends WP_Widget {

/**
 * Sets up the widgets.
 *
 * @since 1.0.0
 */
function __construct() {

    // Set up the widget options.
    $widget_options = array(
        \'classname\'   => \'widget-videoz-recent posts-thumbnail-widget\',
        \'description\' => __( \'Display recent posts with thumbnails.\', \'videoz\' )
    );

    // Create the widget.
    parent::__construct(
        \'videoz-recent\',                                   // $this->id_base
        __( \'» Recent Posts Thumbnails\', \'videoz\' ), // $this->name
        $widget_options                                      // $this->widget_options
    );

    // Flush the transient.
    add_action( \'save_post\'   , array( $this, \'flush_widget_transient\' ) );
    add_action( \'deleted_post\', array( $this, \'flush_widget_transient\' ) );
    add_action( \'switch_theme\', array( $this, \'flush_widget_transient\' ) );

}

/**
 * Outputs the widget based on the arguments input through the widget controls.
 *
 * @since 1.0.0
 */
function widget( $args, $instance ) {
    extract( $args );

    // Output the theme\'s $before_widget wrapper.
    echo $before_widget;
if ( $instance[\'title\'] ) {
        echo $before_title . apply_filters( \'widget_title\', $instance[\'title\'], $instance, $this->id_base ) . $after_title;
    }
    // Display the recent posts.
        if ( false === ( $recent = get_transient( \'videoz_recent_widget_\' . $this->id ) ) ) {

        // Posts query arguments.
        $args = array(
            \'post_type\'      => \'post\',
            \'posts_per_page\' => $instance[\'limit\']
        );

        // The post query
        $recent = get_posts( $args );

        // Store the transient.
        set_transient( \'videoz_recent_widget_\' . $this->id, $recent );

    }

    global $post;
    if ( $recent ) {
        echo \'<ul>\';

            foreach ( $recent as $post ) :
                setup_postdata( $post );

                echo \'<li>\';
                    echo \'<a class="entry-thumbnail" href="\' . esc_url( get_permalink( $post->ID ) ) . \'" rel="bookmark">\' . get_the_post_thumbnail( $post->ID, \'videoz-widget\', array( \'class\' => \'entry-thumb\', \'alt\' => esc_attr( get_the_title( $post->ID ) ) ) ) . \'<span class="play-button"><i class="fa fa-play"></i></span></a>\';
                    echo \'<a href="\' . esc_url( get_permalink( $post->ID ) ) . \'" rel="bookmark">\' . esc_attr( get_the_title( $post->ID ) ) . \'</a>\';
                    if ( $instance[\'show_date\'] ) :
                        echo \'<time class="entry-date" datetime="\' . get_the_date( \'c\' ) . \'">\' . get_the_date() . \'</time>\';
                    endif;
                echo \'</li>\';

            endforeach;

        echo \'</ul>\';
    }

    // Reset the query.
    wp_reset_postdata();

1 个回复
SO网友:cjbj

你需要通过more query arguments. 在这种情况下,您需要:

    $args = array(
        \'post_type\'      => \'post\',
        \'posts_per_page\' => $instance[\'limit\'],
        \'category__in\'   => $id
    );
在哪里$id 是类别id,而不是帖子id。