自定义帖子类型在循环中显示,常规帖子不显示

时间:2013-02-23 作者:TheWebs

每个人都知道基本的WordPress循环,每个人都知道如果你只想在一个循环中放置帖子query_posts("post_type=post"); BEFORE 循环。我已经做到了。

问题是我有一个自定义的帖子类型,称为mini feed,它出现在我的循环中,问题是,它是唯一出现的东西。我有60个职位,职位类型下的职位和NONE 其中,只有这一个(因为目前只有一个)迷你提要出现。

自定义帖子类型如下所示:

public function aisis_mini_feed() {
    $labels = array(
        \'name\'                => _x( \'Mini-Feeds\', \'Post Type General Name\', \'text_domain\' ),
        \'singular_name\'       => _x( \'Mini-Feed\', \'Post Type Singular Name\', \'text_domain\' ),
        \'menu_name\'           => __( \'Mini-Feed\', \'text_domain\' ),
        \'parent_item_colon\'   => __( \'Parent Mini-Feed:\', \'text_domain\' ),
        \'all_items\'           => __( \'All Mini-Feeds\', \'text_domain\' ),
        \'view_item\'           => __( \'View Mini-Feed\', \'text_domain\' ),
        \'add_new_item\'        => __( \'Add New Mini-Feed\', \'text_domain\' ),
        \'add_new\'             => __( \'New Mini-Feed\', \'text_domain\' ),
        \'edit_item\'           => __( \'Edit Mini-Feed\', \'text_domain\' ),
        \'update_item\'         => __( \'Update Mini-Feed\', \'text_domain\' ),
        \'search_items\'        => __( \'Search Mini-Feeds\', \'text_domain\' ),
        \'not_found\'           => __( \'No mini-feeds found\', \'text_domain\' ),
        \'not_found_in_trash\'  => __( \'No mini-feeds found in Trash\', \'text_domain\' ),
    );

    $rewrite = array(
        \'slug\'                => \'mini-feed\',
        \'with_front\'          => true,
        \'pages\'               => false,
        \'feeds\'               => true,
    );

    $args = array(
        \'label\'               => __( \'mini-feed\', \'text_domain\' ),
        \'description\'         => __( \'Mini-Feed custom post type\', \'text_domain\' ),
        \'labels\'              => $labels,
        \'supports\'            => array( \'title\', \'editor\', \'author\', \'thumbnail\', \'revisions\', ),
        \'hierarchical\'        => false,
        \'public\'              => true,
        \'show_ui\'             => true,
        \'show_in_menu\'        => true,
        \'show_in_nav_menus\'   => true,
        \'show_in_admin_bar\'   => true,
        \'menu_icon\'           => \'http://icons.iconarchive.com/icons/fatcow/farm-fresh/32/folders-icon.png\',
        \'can_export\'          => true,
        \'has_archive\'         => true,
        \'exclude_from_search\' => true,
        \'publicly_queryable\'  => true,
        \'query_var\'           => \'mini-feed\',
        \'capability_type\'     => \'post\',
    );

    register_post_type( \'mini-feed\', $args );
}

My question?

<为什么这个迷你提要出现在我的循环中

My loop

query_posts("post_type=post");
if(have_posts()){
    while(have_posts()){
        the_post();
        the_title();
        the_content();
    }
}

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

起初,并不是每个人都知道,但肯定应该知道pre_get_posts hook 在大多数情况下,应该使用它来代替query\\u帖子。

此外,仅定义一个自定义帖子类型会导致只带来一个自定义帖子类型。这是正确的。如果希望同时提供自定义post类型和post,则应该使用post\\u type参数acceptiong数组的缩写,而不仅仅是字符串。因此,请尝试以下代码:

function show_mini_feeds_on_home( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( \'post_type\', array(\'post\', \'mini-feed\') );
    }
}
add_action( \'pre_get_posts\', \'show_mini_feeds_on_home\' );

结束