简体   繁体   English

WordPress的自定义帖子类型重写页面和类别标签

[英]Wordpress Custom Post Type rewrite page and category slug

I have set up a custom post type 我已经设置了自定义帖子类型

register_post_type( 'Communities',
    array(
        'labels' => array(
        'name' => __( 'Communities' ),
        'singular_name' => __( 'Community' ),
        'not_found' => __( 'No Communities Found' )
        ),
        'public' => true,
        'rewrite' => array( 'slug' => '/%category%/communities', 'with_front' => false),
        'has_archive' => true,
        'hierarchical'  => true,
        'taxonomies'  => array( 'category' ),
        'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'custom-fields' ),
        'menu_icon' => 'dashicons-admin-home'
    )
);

I have a pages setup with a city name that I need to remain as a page for different reasons, but I want communities in that city to be stored as a custom post type. 我有一个带有城市名称的页面设置,出于各种原因,我需要将其保留为页面,但我希望将该城市中的社区存储为自定义帖子类型。 I have setup categories for each city that I am adding communities to but I would like them to treat the cityname page as the parent. 我为要添加社区的每个城市设置了类别,但是我希望他们将城市名称页面视为父级。

like this 像这样

www.sitename.com/cityname/communities/communityname/

I would like cityname to be a page, communities to be an archive page and the communityname to be a single page. 我希望cityname是一个页面,community是一个存档页面,而communityname是一个页面。

Every solution I have found thus far generates 404 errors or conflicts with the parent page. 到目前为止,我发现的每个解决方案都会产生404错误或与父页面冲突。

Any help is appreciated. 任何帮助表示赞赏。 Thanks! 谢谢!

This should do it for you, but first there are 2 things you should know. 这应该为您做,但是首先您应该知道两件事。

1.) I wouldn't recommend this URL structure as it mandates that every entry have at least ONE category. 1.)我不建议使用此URL结构,因为它要求每个条目至少具有一个类别。 It will always grab the first in the array. 它将始终抢占阵列中的第一个。 You'll need to write a small plugin to let your authors know. 您需要编写一个小插件,以告知您的作者。 This may do the trick: 这可能会达到目的:

https://srd.wordpress.org/plugins/require-post-category/ https://srd.wordpress.org/plugins/require-post-category/

2.) Before you can view a post, you'll need to visit Settings > permalinks after you add the code so it will force a rewrite flush. 2.)在查看帖子之前,添加代码后,您需要访问设置>永久链接,以强制进行重写刷新。


/**
 * Register Community Custom Post Type
 */
function community_post_type() {

    $labels = array(
        'name'                  => 'Communities',
        'singular_name'         => 'Community',
        'menu_name'             => 'Communities',
        'name_admin_bar'        => 'Communities',
        'archives'              => 'Community Archives',
        'attributes'            => 'Community Attributes',
        'parent_item_colon'     => 'Parent Community:',
        'all_items'             => 'All Communities',
        'add_new_item'          => 'Add New Community',
        'add_new'               => 'Add New',
        'new_item'              => 'New Community',
        'edit_item'             => 'Edit Community',
        'update_item'           => 'Update Community',
        'view_item'             => 'View Community',
        'view_items'            => 'View Communities',
        'search_items'          => 'Search Communities',
        'not_found'             => 'Community Not found',
        'not_found_in_trash'    => 'Not found in Trash',
        'featured_image'        => 'Featured Image',
        'set_featured_image'    => 'Set featured image',
        'remove_featured_image' => 'Remove featured image',
        'use_featured_image'    => 'Use as featured image',
        'insert_into_item'      => 'Insert into item',
        'uploaded_to_this_item' => 'Uploaded to this Community',
        'items_list'            => 'Communities list',
        'items_list_navigation' => 'Communities list navigation',
        'filter_items_list'     => 'Filter Communities list',
    );
    $rewrite = array(
        'slug'                  => '/%category%/communities',
        'with_front'            => true,
        'pages'                 => true,
        'feeds'                 => true,
    );
    $args = array(
        'label'                 => 'Community',
        'labels'                => $labels,
        'supports'              => array( 'title', 'editor', 'excerpt', 'thumbnail', 'custom-fields', ),
        'taxonomies'            => array( 'category' ),
        'hierarchical'          => true,
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'menu_position'         => 5,
        'menu_icon'             => 'dashicons-universal-access',
        'show_in_admin_bar'     => true,
        'show_in_nav_menus'     => true,
        'can_export'            => true,
        'has_archive'           => true,
        'exclude_from_search'   => false,
        'publicly_queryable'    => true,
        'rewrite'               => $rewrite,
        'capability_type'       => 'page',
        'show_in_rest'          => true,
    );
    register_post_type( 'communities', $args );

}
add_action( 'init', 'community_post_type', 0 );

/**
 * Add category slug to community post links
 * @param  $post_link
 * @param  $id
 */

function my_commmunity_post_link( $post_link, $id = 0 ){
    $post = get_post($id);
    if ( is_object( $post ) && $post->post_type == 'communities' ){
        $terms = wp_get_object_terms( $post->ID, 'category' );
        if( !empty($terms) ){
            return str_replace( '%category%' , $terms[0]->slug , $post_link );
        }
    }
    return $post_link;
}
add_filter( 'post_type_link', 'my_commmunity_post_link', 1, 3 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM