【问题标题】:Wordpress custom post type: parent child relationshipWordpress自定义帖子类型:父子关系
【发布时间】:2014-10-18 09:34:42
【问题描述】:

如何在不使用 wordpress 中的任何插件的情况下保持自定义帖子类型中的父子关系?

例如: 当您访问本网站时

http://www.volunteeringnepal.com/

您可以找到志愿者影响菜单。在这个菜单下,我制作了volunteers_impact 分类法,通过使用称为 gdcpt 自定义帖子类型的插件对志愿者的影响进行分类。但是我想在不使用任何插件的情况下做到这一点,最重要的是我想保持父子关系。

在这个网站,当你点击

www.volunteeringnepal.com/volunteers_impact/volunteering-impact-2013

您只会发现 2013 年的志愿者影响。分类名称是voluteers_impact,但我想保留分类名称volunteer-impact,这对搜索引擎优化有好处。

在提供自定义帖子类型和分类名称时,此插件建议仅使用下划线连接两个单词。

假设当你点击单个页面时

http://www.volunteeringnepal.com/volunteer-impact/women-empowerment-2013/

您将看到帖子名称 (volunteer-impact) 和分类名称 (volunteers_impact) 的区别。我如何对其进行分类以使 url 结构相同。例如:

volunteeringnepal.com/volunteer-impact/volunteering-impact-2013

分类和

volunteeringnepal.com/volunteer-impact/women-empowerment-2013

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    使用连字符 (-) 来分隔自定义分类法和自定义帖子类型中的单词是一种不好的做法。您不应该在分类/自定义帖子类型名称中使用任何特殊字符、数字或大写字母,因为它们在以后命名自定义模板时非常麻烦。这些破坏了模板层次结构,任何定制的模板都将被忽略。你可以自己去尝试一下。只能使用下划线 (_)。

    我总是尝试在分类和帖子类型名称中使用单个单词,因为它们在此类情况下不太容易失败。使用起来也更清洁。

    为了保持你所追求的 URL 结构,你应该创建一个自定义的重写规则来为你处理它

    编辑

    这是为您的主题添加自定义分类和自定义帖子类型的代码。只需将其添加到您的functions.php 中。去玩弄代码

    function event_post_example() {
    
    register_post_type( 'event_type',
        array(
        'labels' => array(
            'name' => __('Events Posts', 'baretheme'),
            'singular_name' => __('Event Post', 'baretheme'),
            'all_items' => __('All Event Posts', 'baretheme'),
            'add_new' => __('Add New Event Post', 'baretheme'),
            'add_new_item' => __('Add New Event Type', 'baretheme'),
            'edit' => __( 'Edit', 'baretheme' ),
            'edit_item' => __('Edit Post Types', 'baretheme'),
            'new_item' => __('New Post Type', 'baretheme'),
            'view_item' => __('View Post Type', 'baretheme'),
            'search_items' => __('Search Post Type', 'baretheme'),
            'not_found' =>  __('Nothing found in the Database.', 'baretheme'),
            'not_found_in_trash' => __('Nothing found in Trash', 'baretheme'),
            'parent_item_colon' => ''
        ), /* end of arrays */
        'description' => __( 'This is the example event post type', 'baretheme' ), /* Custom Type Description */
        'public' => true,
        'publicly_queryable' => true,
        'postids_from_search' => false,
        'show_ui' => true,
        'query_var' => true,
        'menu_position' => 9,
        'menu_icon' => get_stylesheet_directory_uri() . '/library/images/custom-post-icon.png',
        'rewrite'   => array( 'slug' => 'event_type', 'with_front' => false ),
        'has_archive' => 'event_type',
        'capability_type' => 'post',
        'hierarchical' => false,
    
        'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'sticky')
        ) /* end of options */
    ); /* end of register post type */
    
    register_taxonomy( 'event_cat','event_type',
        array(
            'labels' => array(
            'name' => __( 'Event Categories', 'baretheme' ),
            'singular_name' => __( 'Event Category', 'baretheme' ),
            'search_items' =>  __( 'Search Event Categories', 'baretheme' ),
            'all_items' => __( 'All Event Categories', 'baretheme' ),
            'parent_item' => __( 'Parent Event Category', 'baretheme' ),
            'parent_item_colon' => __( 'Parent Event Category:', 'baretheme' ),
            'edit_item' => __( 'Edit Event Category', 'baretheme' ),
            'update_item' => __( 'Update Event Category', 'baretheme' ),
            'add_new_item' => __( 'Add New Event Category', 'baretheme' ),
            'new_item_name' => __( 'New Event Category Name', 'baretheme' )
        ),
        'hierarchical' => true,
        'show_admin_column' => true,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'event-slug' ),
        )
    );
    
    register_taxonomy_for_object_type( 'event_cat', 'event_type' );
    
    }
    
    add_action( 'init', 'event_post_example');
    

    【讨论】:

    • 感谢您的回答,但对于 SEO,我必须使用多个单词来自定义帖子类型和分类。很难与单个关键字竞争,谷歌建议使用连字符 (-) 分隔单词。谷歌将下划线视为单词连接符。您能否在不使用任何插件的情况下帮助我制作自定义帖子类型和分类法。我曾尝试按照本网站smashingmagazine.com/2012/11/08/… 的指示进行自定义分类,但同时对两个词进行分类。它无法显示分类。
    • 我刚到公司。现在再次从我的手机发布。如果我回家,我会在今天下午发布代码:-)
    • 我可以使用连字符 (-) 来分隔帖子类型名称和分类名称中的单词吗?当我用连字符 (-) 分隔帖子类型名称时,会导致分页问题。
    • 是baretheme是主题名吗?所以我想写什么,如果我的主题名称是“vintheme”。我应该用vintheme替换baretheme吗?
    • 这就是我所说的使用连字符。连字符在名称中使用时会在 wordpress 中引起很多问题。它们永远不应用于分类和帖子类型名称。你无法改变这一点。要么您选择 SEO 和连字符并遇到所有这些问题,要么您坚持 wordpress 标准,使用下划线并在 SEO 上受苦。不幸的是,你不能同时拥有两者
    猜你喜欢
    • 2013-10-28
    • 1970-01-01
    • 2019-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 2016-04-27
    相关资源
    最近更新 更多