在前端显示来自分类页面中的自定义帖子类型的帖子

时间:2013-09-26 作者:Paxjah

我创建了我的自定义帖子类型(bizdirectory),创建了一个新帖子(测试业务)和一个新类别(业务目录)。但是如果我将业务目录类别(slug=Business)添加到我的主导航中,链接会显示,它会注册为该类别,但会显示“找不到”。

如果我在我的网站(使用首页上的搜索栏)上搜索测试业务,它会找到它-它显示的URL是-http://www.domain.com/dev/?bizdirectory=test-business - 我的函数代码。php如下:

// Load Custom Post Type 
function add_bizdirectory() {
    $labels = array( 
        \'name\' => __( \'Businesses\', \'text_domain\' ),
        \'singular_name\' => __( \'Business\', \'text_domain\' ),
        \'add_new\' => __( \'Add New Business\', \'${4:Name}\', \'text_domain\' ),
        \'add_new_item\' => __( \'Add New Business\', \'text_domain}\' ),
        \'edit_item\' => __( \'Edit Business\', \'text_domain\' ),
        \'new_item\' => __( \'New Business\', \'text_domain\' ),
        \'view_item\' => __( \'View Business\', \'text_domain\' ),
        \'search_items\' => __( \'Search Businesses\', \'text_domain\' ),
        \'not_found\' => __( \'No Businesses found\', \'text_domain\' ),
        \'not_found_in_trash\' => __( \'No Businesses found in Trash\', \'text_domain\' ),
        \'parent_item_colon\' => __( \'Parent Business:\', \'text_domain\' ),
        \'menu_name\' => __( \'Business Directory\', \'text_domain\' ),
    );
    $args = array( 
        \'labels\' => $labels,
        \'hierarchical\' => true,
        \'description\' => \'description\',
        \'taxonomies\' => array( \'category\' ),
        \'public\' => true,
        \'show_ui\' => true,
        \'show_in_menu\' => true,
        \'menu_position\' => 5,
        \'show_in_nav_menus\' => true,
        \'publicly_queryable\' => true,
        \'exclude_from_search\' => false,
        \'has_archive\' => true,
        \'query_var\' => true,
        \'can_export\' => true,
        \'rewrite\' => true,
        \'capability_type\' => \'post\', 
        \'supports\' => array( \'title\', \'editor\', \'author\', \'thumbnail\', \'custom-fields\', \'revisions\', \'post-formats\' ),
    );
    register_post_type( \'bizdirectory\', $args );
}
add_action( \'init\', \'add_bizdirectory\' );
// End
我尝试了所有的重写规则选项,除了从wp\\U选项中手动删除重写规则的选项,它没有出现在wp\\U选项中。

非常感谢您能给我的任何帮助或建议:)

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

我已经找到了答案,我完全误解了标准帖子类型和自定义帖子类型之间的区别,以及它们的分类法。我发现了几篇非常有用的文章,下面的代码就是我最终得到的,还有一些我觉得有用的文章。我希望其他人觉得这很有用。

// Load Custom Post Type 
function add_directory() {
    $labels = array( 
    \'name\'               => __( \'Businesses\', \'text_domain\' ),
        \'singular_name\'      => __( \'Business\', \'text_domain\' ),
        \'add_new\'            => __( \'Add New Business\', \'${4:Name}\', \'text_domain\' ),
        \'add_new_item\'       => __( \'Add New Business\', \'text_domain}\' ),
        \'edit_item\'          => __( \'Edit Business\', \'text_domain\' ),
        \'new_item\'           => __( \'New Business\', \'text_domain\' ),
        \'view_item\'          => __( \'View Business\', \'text_domain\' ),
        \'search_items\'       => __( \'Search Businesses\', \'text_domain\' ),
        \'not_found\'          => __( \'No Businesses found\', \'text_domain\' ),
        \'not_found_in_trash\' => __( \'No Businesses found in Trash\', \'text_domain\' ),
        \'parent_item_colon\'  => __( \'Parent Business:\', \'text_domain\' ),
        \'menu_name\'          => __( \'Business Directory\', \'text_domain\' ),
    );
    $args = array( 
        \'labels\'              => $labels,
        \'hierarchical\'        => true,
        \'description\'         => \'description\',
        \'taxonomies\'          => array( \'biz-cat\' ),
        \'public\'              => true,
        \'show_ui\'             => true,
        \'show_in_menu\'        => true,
        \'menu_position\'       => 5,
        //\'menu_icon\'         => \'\',
        \'show_in_nav_menus\'   => true,
        \'publicly_queryable\'  => true,
        \'exclude_from_search\' => false,
        \'has_archive\'         => true,
        \'query_var\'           => true,
        \'can_export\'          => true,
        \'rewrite\'             => true,
        \'capability_type\'     => \'post\', 
        \'supports\'            => array( \'title\', \'editor\', \'author\', \'thumbnail\', \'custom-fields\', \'revisions\', \'post-formats\' ),
    );
    register_post_type( \'bizdirectory\', $args );
}
add_action( \'init\', \'add_directory\' );

// Create Custom Taxonomy
function directory_create_taxonomies() 
{
    register_taxonomy( \'biz-cat\', array( \'bizdirectory\' ), array(
        \'hierarchical\' => true,
        \'label\' => \'Business Categories\',
        \'singular_name\' => \'Business Category\',
        \'show_ui\' => true,
        \'query_var\' => true,
        \'rewrite\' => array( \'slug\' => \'biz-cat\' )
    ));
}
add_action( \'init\', \'directory_create_taxonomies\', 0 );

// \'bizdirectory\' is the registered post type name
function directory_columns($defaults) {
    // \'biz-cat\' is the registered taxonomy name
    $defaults[\'biz-cat\'] = \'Business Category\';
    return $defaults;
}
function directory_custom_column($column_name, $post_id) {
    $taxonomy = $column_name;
    $post_type = get_post_type($post_id);
    $terms = get_the_terms($post_id, $taxonomy);

    if ( !empty($terms) ) {
        foreach ( $terms as $term )
            $post_terms[] = "<a href=\'edit.php?post_type={$post_type}&{$taxonomy}={$term->slug}\'> " . esc_html(sanitize_term_field(\'name\', $term->name, $term->term_id, $taxonomy, \'edit\')) . "</a>";
        echo join( \', \', $post_terms );
    }
    else echo \'<i>Not assigned.</i>\';
}
add_filter( \'manage_project_posts_columns\', \'directory_columns\' );
add_action( \'manage_project_posts_custom_column\', \'directory_custom_column\', 10, 2 );

SO网友:Brad Dalton

您最好为帖子分离类别,并为CPT类别使用自定义分类法类型。

您需要将此行添加到注册cpt代码中

\'taxonomies\'   => array( \'bizdirectory-type\' ),
在代码中替换此行。

\'taxonomies\' => array( \'category\' ),
然后在函数中添加此代码。php

add_action( \'init\', \'wpsites_custom_taxonomy_types\' );
function wpsites_custom_taxonomy_types() {

register_taxonomy( \'bizdirectory-type\', \'bizdirectory\',
    array(
        \'labels\' => array(
            \'name\'          => _x( \'Types\', \'taxonomy general name\', \'wpsites\' ),
            \'add_new_item\'  => __( \'Add New Bizdirectory Type\', \'wpsites\' ),
            \'new_item_name\' => __( \'New Bizdirectory Type\', \'wpsites\' ),
        ),
        \'exclude_from_search\' => true,
        \'has_archive\'         => true,
        \'hierarchical\'        => true,
        \'rewrite\'             => array( \'slug\' => \'bizdirectory-type\', \'with_front\' => false ),
        \'show_ui\'             => true,
        \'show_tagcloud\'       => false,
    ));

}

结束

相关推荐

Wp_Dropdown_Categories-删除 ;

我需要删除的自动添加&nbsp; 在wp_dropdown_categories, 这导致我使用的jQuery multi-select下拉插件出现问题。示例值:<option class=\"level-1\" value=\"120\">&nbsp;&nbsp;&nbsp;Apples&nbsp;&nbsp;(125)</option>