【问题标题】:Wordpress Custom Post Type rewrite page and category slugWordpress 自定义帖子类型重写页面和类别 slug
【发布时间】:2017-08-26 05:43:56
【问题描述】:

我已经设置了自定义帖子类型

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'
    )
);

我有一个带有城市名称的页面设置,出于不同原因我需要将其保留为页面,但我希望将该城市中的社区存储为自定义帖子类型。我为要添加社区的每个城市设置了类别,但我希望他们将城市名称页面视为父级。

喜欢这个

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

我希望 cityname 是一个页面,communities 是一个存档页面,communityname 是一个页面。

到目前为止,我发现的每个解决方案都会产生 404 错误或与父页面发生冲突。

感谢任何帮助。谢谢!

【问题讨论】:

    标签: php wordpress url-rewriting custom-post-type


    【解决方案1】:

    这应该为你做,但首先有两件事你应该知道。

    1.) 我不推荐这种 URL 结构,因为它要求每个条目至少有一个类别。它总是会抓取数组中的第一个。您需要编写一个小插件让您的作者知道。这可能会奏效:

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

    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 );
    

    【讨论】:

    • 感谢您迄今为止的帮助。我使用上面的代码并在我的自定义主题以及二十六中对其进行了测试。它适用于存档和单页,但父页面显示为存档。其他页面(即联系页面等)现在也正在生成 404 错误。
    • 它似乎快到了...你有什么建议我可以试试吗?
    • 这解决了 404 问题,但我仍然认为结构可以设置得更简洁一些。也许是自定义分类法,而不是使用帖子的分类法。然后,您可以使用 ACF 来建立关系。请参阅下面的更新,因为它将向您展示如何以您认为合适的方式重写您的规则。 gist.github.com/simplethemes/c9813cafef799000b04439ffae4a5c50
    • 再次感谢。是的,我认为您是对的.. 设置自定义分类法我们可能是最好的选择。
    • 我注册了一个自定义分类法,从仪表板看,url 结构似乎很好。但是,当您单击链接查看社区时,它仍然会生成 404。gist.github.com/pagetopixel/d103632eb7a0cbed3aaff0c5f30d0535
    猜你喜欢
    • 1970-01-01
    • 2017-02-28
    • 1970-01-01
    • 2014-11-10
    • 2017-01-31
    • 1970-01-01
    • 2021-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多