自定义帖子类型的帖子不会显示在类别页面上

时间:2015-05-30 17:18:31

标签: php wordpress

我为客户创建了一个子主题并制作了一些新的帖子类型。到目前为止,一切都很好,除了自定义帖子类型中的帖子没有显示在类别页面下面;虽然他们属于那一类。

以下是我创建自定义帖子类型的代码...

// Register Custom Post Type
function directory_listing() {

$labels = array(
    'name'                => _x( 'Directory Items', 'Post Type General Name', 'text_domain' ),
    'singular_name'       => _x( 'Directory Item', 'Post Type Singular Name', 'text_domain' ),
    'menu_name'           => __( 'Directory Listing', 'text_domain' ),
    'name_admin_bar'      => __( 'Directory', 'text_domain' ),
    'parent_item_colon'   => __( 'Directory:', 'text_domain' ),
    'all_items'           => __( 'All Listings', 'text_domain' ),
    'add_new_item'        => __( 'Add New Listing', 'text_domain' ),
    'add_new'             => __( 'Add New', 'text_domain' ),
    'new_item'            => __( 'New Listing', 'text_domain' ),
    'edit_item'           => __( 'Edit Listing', 'text_domain' ),
    'update_item'         => __( 'Update Listing', 'text_domain' ),
    'view_item'           => __( 'View Listing', 'text_domain' ),
    'search_items'        => __( 'Search Listing', 'text_domain' ),
    'not_found'           => __( 'Not found', 'text_domain' ),
    'not_found_in_trash'  => __( 'Not found in Trash', 'text_domain' ),
);
$args = array(
    'label'               => __( 'directory_listings', 'text_domain' ),
    'description'         => __( 'Directory Listing', 'text_domain' ),
    'labels'              => $labels,
    'supports'            => array( 'title', 'editor', 'thumbnail', 'comments', 'post-formats', ),
    'taxonomies'          => array( 'category', 'post_tag' ),
    'hierarchical'        => true,
    'public'              => true,
    'show_ui'             => true,
    'show_in_menu'        => true,
    'menu_position'       => 5,
    'menu_icon'           => 'dashicons-flag',
    'show_in_admin_bar'   => true,
    'show_in_nav_menus'   => true,
    'can_export'          => true,
    'has_archive'         => true,
    'exclude_from_search' => false,
    'publicly_queryable'  => true,
    'capability_type'     => 'post',
);
register_post_type( 'directory_listings', $args );

}

// Hook into the 'init' action
add_action( 'init', 'directory_listing', 0 );
}

我不明白的部分是,当我转到自定义帖子中的实际帖子时,键入"查看帖子"我得到的网址就像......

http://otpguide.dev/directory_listings/another-place/

但是,特定帖子标记的类别的类别网址类似于......

http://otpguide.dev/category/directory/alpharetta/

不应该自定义帖子类型的网址更像是...

http://otpguide.dev/category/directory/alpharetta/another-place ??

为什么我的帖子不会出现在类别页面下?

1 个答案:

答案 0 :(得分:0)

想出来了!

自定义帖子类型不包含在archive.php中,除非您指定它们。我添加了这个片段,它完美无缺。

// allow custom post types in archive
add_filter( 'pre_get_posts', 'my_get_posts' );

function my_get_posts( $query ) {
if( !is_admin() ) {
    if ( !is_post_type_archive() && $query->is_archive() ) {
        $query->set( 'post_type', array( 'post', 'your-custom-post-type', 'another_custom_post_type' ) );
    }
    return $query;
    }
}

显然,search.php也是如此;您还必须排除' exclude_from_search' =>假,'注册自定义帖子类型时声明

// add custom post types to search
// Define what post types to search
function searchAll( $query ) {
if ( $query->is_search ) {
    $query->set( 'post_type', array( 'post', 'page', 'feed', 'your-custom-post-type', 'another_custom_post_type'));
}
return $query;
}

// The hook needed to search ALL content
add_filter( 'the_search_query', 'searchAll' );
相关问题