显示注册分类中的自定义帖子类型

时间:2015-01-14 作者:tibewww

我已通过此功能创建了自定义帖子类型:

function custom_post_type() {

    $labels = array(
        \'name\'                => _x( \'advertising type\', \'Post Type General Name\', \'twentythirteen\' ),
        \'singular_name\'       => _x( \'advertising type\', \'Post Type Singular Name\', \'twentythirteen\' ),
        \'menu_name\'           => __( \'advertising type\', \'twentythirteen\' ),
        \'parent_item_colon\'   => __( \'Parent advertising\', \'twentythirteen\' ),
        \'all_items\'           => __( \'All advertising\', \'twentythirteen\' ),
        \'view_item\'           => __( \'View advertising\', \'twentythirteen\' ),
        \'add_new_item\'        => __( \'Add New advertising\', \'twentythirteen\' ),
        \'add_new\'             => __( \'Add New\', \'twentythirteen\' ),
        \'edit_item\'           => __( \'Edit advertising\', \'twentythirteen\' ),
        \'update_item\'         => __( \'Update advertising\', \'twentythirteen\' ),
        \'search_items\'        => __( \'Search advertising\', \'twentythirteen\' ),
        \'not_found\'           => __( \'Not Found\', \'twentythirteen\' ),
        \'not_found_in_trash\'  => __( \'Not found in Trash\', \'twentythirteen\' ),
        \'post_type\'      => \'attachment\',
    \'post_parent\'    => $post->ID,
    \'post_status\'    => \'inherit\',
    \'post_mime_type\' => \'image\',
    );


    $args = array(
        \'label\'               => __( \'advertising\', \'twentythirteen\' ),
        \'description\'         => __( \'advertising news and reviews\', \'twentythirteen\' ),
        \'labels\'              => $labels,
        // Features this CPT supports in Post Editor
        \'supports\'            => array( \'title\', \'editor\', \'excerpt\', \'author\', \'thumbnail\', \'comments\', \'revisions\', \'custom-fields\', ),

        \'hierarchical\'        => true,
        \'public\'              => true,
        \'show_ui\'             => true,
        \'show_in_menu\'        => true,
        \'show_in_nav_menus\'   => true,
        \'show_in_admin_bar\'   => true,
        \'menu_position\'       => 5,
        \'can_export\'          => true,
        \'has_archive\'         => true,
        \'exclude_from_search\' => false,
        \'publicly_queryable\'  => true,
                \'has_archive\' => true,
        \'taxonomies\' => array(\'category\', \'post_tag\') ,// this is IMPORTANT

        \'capability_type\'     => \'page\',

    );
        register_taxonomy( \'advertising_type\', \'advertising\', $args );


    register_post_type( \'advertising\', $args );

}



add_action( \'init\', \'custom_post_type\', 0 );
其中包括分类法“advertising\\u type”

在我的循环中,我将查询帖子添加为只显示特定分类法中的帖子:

<?php
global $wp_query;
$args = array_merge( $wp_query->query_vars, array(\'post_type\' => \'advertising\', \'posts_per_page\' => 50, \'advertising_type\' => \'recruitment\',
 ) );
query_posts( $args );
while(have_posts()): the_post();
?>



        <?php while ( $folio_loop->have_posts() ) : $folio_loop->the_post(); ?>

        <?php endwhile; ?>
其招聘使用:\'advertising_type\' => \'recruitment\'

然而我没能成功。它不显示任何帖子。如果我删除这个查询,我会从我的自定义帖子类型显示中看到帖子,所以我想问题真的来自于此(我无法显示特定分类中的帖子)

任何帮助都是惊人的,

谢谢你的时间和帮助

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

首先,我想指出您的帖子类型和分类设置有一些错误。您的$labels 这没有道理:

$labels = array(
    ...
    \'post_type\'      => \'attachment\',
    \'post_parent\'    => $post->ID,
    \'post_status\'    => \'inherit\',
    \'post_mime_type\' => \'image\',
);
在创建帖子类型标签或分类标签时,最后4个选项(对我来说)没有多大意义。永远不会有$post->ID 在中设置init 功能和labels 不是你想post_type, 而且attachment 是一种保留的内置post类型,因此无法重复使用。要查看可使用的已接受标签的完整列表,请单击以下链接:

Post Type Labels

Taxonomy Labels

您会注意到,分类法的标签与Post类型的标签不同。除了大量其他设置之外,我不建议您在注册分类法和post类型时使用相同的参数,而是create separate arguments for each.

假设您的帖子类型和分类法有效,那么Milo是对的(在评论部分),您不应该使用query_postsHere\'s Why. 如果需要,可以创建new WP_Query 或使用pre_get_posts (如上所述)。

Use pre_get_posts To Modify The Main Query

正如标题所述,pre_get_posts 将在实际输出之前修改主查询。这意味着您可以保留query_vars (大概)已经设置好了,您可以覆盖您需要的任何内容。你想把这个放进你的functions.php 文件,其工作原理如下:

( Pre Get Posts Codex Article )

function advertising_pgp( $query ) {
    if( ! is_admin() ) {    // Do Not run on Admin Pages

        if( $query->is_main_query() ) {     // Only run on the Main Query

            $query->set( \'post_type\', \'advertising\' );
            $query->set( \'posts_per_page\', 50 );
            $query->set( \'tax_query\', array(
                array(
                    \'taxonomy\'  => \'advertising_type\',
                    \'field\'     => \'slug\',
                    \'terms\'     => \'recruitment\'
                )
            ) );

        }
    }
}
add_action( \'pre_get_posts\', \'advertising_pgp\' );
:: Warning :: 以上内容将修改您网站上的所有主要查询。您可能需要在第二个条件语句中添加其他条件语句,例如is_page( \'Your-Page\' )

Create a New WP Query

如果您需要主查询的附加查询,则通常使用WP查询。如果只想将主查询修改为其他内容,可以使用pre_get_posts 既然你想合并query_vars - 我建议pre_get_posts 因为那里已经有他们了。我把这个放在这里以防万一。

global $wp_query;

$new_query = new WP_Query(
    array_merge( 
        $wp_query->query_vars,
        array(
            \'post_type\' => \'advertising\',
            \'posts_per_page\' => 50, 
            \'advertising_type\' => \'recruitment\'
        )
    )
);

<?php if( $new_query->have_posts() ) : ?>

    <?php while( $new_query->have_posts() ) : $new_query->the_post(); ?>

        <h1><?php the_title(); ?></h1>
        <?php the_content(); ?>

    <?php endif; ?>

<?php endif; ?>

结束