Wordpress 职位类型分级

Wordpress 职位类型分级,wordpress,custom-post-type,Wordpress,Custom Post Type,我有一个问题,升级到wordpress 4.0后,我的自定义帖子类型不再工作,下面是我注册自定义帖子类型的方法: function my_post_type() { $labels = array( 'name' => 'Staff' ); $args = array( 'labels' => $labels, 'public' =&

我有一个问题,升级到wordpress 4.0后,我的自定义帖子类型不再工作,下面是我注册自定义帖子类型的方法:

function my_post_type() {
    $labels = array(
        'name'               => 'Staff'
    );
    $args = array(
        'labels'              => $labels,
        'public'              => true,
        'exclude_from_search' => true,
        'publicly_queryable'  => true,
        'show_ui'             => true,
        'show_in_nav_menus'   => false,
        'show_in_menu'        => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 20,
        'menu_icon'           => null,
        'capability_type'     => 'post',
        'hierarchical'        => true,
        'supports'            => array('title','editor','thumbnail','custom-fields','page-attributes'),
        'has_archive'         => false,
        'rewrite'             => false,
        'query_var'           => true,
        'can_export'          => true
    );
    register_post_type('my_staff', $args);
}
add_action('init', 'my_post_type');

问题是这个帖子类型的permalink给出了404,如果我将'hierarchical'设置为false,一切都会正常,但问题是我需要'hierarchical'设置为true,有什么解决方案吗?

你需要做的就是在行中添加$post\u type=任何内容

$children = wp_list_pages..
看来

$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0$post_type=whatever");
这将帮助您解决同样的问题

我们为我们的网站中的产品、产品类别和产品供应商开发了自定义帖子类型,还开发了一个自定义重写规则,以便产品URL可以“漂亮”地包含类别名称

WordPress 4.0升级后,这些分层自定义帖子类型崩溃了。此时唯一的快速解决方案是发布更新,将Hierarchy设置为false,因为我们所有客户的产品都显示404页面未找到


WordPress4.0中一定发生了一些变化。我们仍在研究解决方案,试图找到让它们恢复原状的最佳方法。

我也遇到了同样的问题,我可以通过在register\u post\u类型参数中设置'hierarchy'=>false来解决这个问题

$args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true, 
    'query_var' => true,
    'has_archive' => true,
    'menu_icon' => '',
    'show_in_menu' => 'bcs-options',
    'rewrite' => array('slug' => 'stories'),
    'capability_type' => 'page',
    'hierarchical' => false,
    'menu_position' => null,
    'supports' => array('title','editor','thumbnail','excerpt','comments','revisions')
);

我仍然能够在我的代码中手动设置自定义post父项,一切都像以前一样正常工作。我不确定WordPress4.0为什么会破坏这一点,但我猜这与新版本如何处理Hierarchy设置为true的自定义帖子类型的永久链接有关。对Wordpress代码库有更深入了解的人可能能够详细说明这一点

我遇到了完全相同的问题,尽管我没有将其与Wordpress 4.0的更改联系起来。 我有几个自定义的帖子类型,其中只有少数返回404页。只有在看到您最初的问题后,我才意识到问题出在那些设置为“hierarchy”=>true的自定义帖子类型上。一旦我设置'hierarchy'=>false,我就可以查看“problem”自定义帖子了

然而,这也意味着,当hierarchy设置为true时,我丢失了一些URL/permalink结构。因此,“自定义帖子类型\父标题\帖子标题”变成了“自定义帖子类型\帖子标题”

最后,我设置了一个“post_type_link”过滤器,将父对象添加回链接中。 我也有一个修改后的链接结构重写规则设置

该解决方案的灵感来源于以下帖子:

虽然我没有在寄存器\ u post\ u类型的重写slug中使用“%”参数。 相反,在post_type_link函数中,我搜索slug并将其替换为原始slug+任何必需的前缀。我发现这样我可以在链接中保留原来的slug,它不会被“%”参数“污染”,这意味着链接仍然可以用于显示归档页面,而且“%”参数也不会显示在面包屑中

寄存器\u post\u类型中提取:

'rewrite' => array(
                        'slug' => 'dresses'// 'dresses/%designer%'
                  ),
add_filter("post_type_link","rewrite_dress",10,2);

function rewrite_dress($link, $post) {
    if(get_post_type($post) === "dress") {
        $designer = get_post($post->post_parent);
        $designer = $designer->post_name . "/";
        if(!(check_not_empty($designer))) {
         $designer = "";
        }
        //$link     = str_replace("%designer%",$designer,$link);
        $link     = str_replace("dresses/","dresses/" . $designer,$link);
    }
    return $link;

    /*
    originally the slug had a reference in it e.g. %designer%
    we could then do a string replace on it to insert the designer
    but, if you used the breadcrumbs to view the root e.g. /dresses
    then it would actually appear as /dresses/%designer% which isn't wanted.
    */

}
function rewrite_dress_rules(){
    global $wp_rewrite;
    /* /dresses/designer/dress */
    $q = "dresses/(.*)/(.*)";
    $q = "dresses/([^/]*)/([^/]*)/?";
    add_rewrite_rule($q,'index.php?post_type=dress&designer=$matches[1]&dress=$matches[2]','top');
    // $wp_rewrite->flush_rules(); // !!!
}

add_action("init", 'rewrite_dress_rules' );
后类型链接过滤器:

'rewrite' => array(
                        'slug' => 'dresses'// 'dresses/%designer%'
                  ),
add_filter("post_type_link","rewrite_dress",10,2);

function rewrite_dress($link, $post) {
    if(get_post_type($post) === "dress") {
        $designer = get_post($post->post_parent);
        $designer = $designer->post_name . "/";
        if(!(check_not_empty($designer))) {
         $designer = "";
        }
        //$link     = str_replace("%designer%",$designer,$link);
        $link     = str_replace("dresses/","dresses/" . $designer,$link);
    }
    return $link;

    /*
    originally the slug had a reference in it e.g. %designer%
    we could then do a string replace on it to insert the designer
    but, if you used the breadcrumbs to view the root e.g. /dresses
    then it would actually appear as /dresses/%designer% which isn't wanted.
    */

}
function rewrite_dress_rules(){
    global $wp_rewrite;
    /* /dresses/designer/dress */
    $q = "dresses/(.*)/(.*)";
    $q = "dresses/([^/]*)/([^/]*)/?";
    add_rewrite_rule($q,'index.php?post_type=dress&designer=$matches[1]&dress=$matches[2]','top');
    // $wp_rewrite->flush_rules(); // !!!
}

add_action("init", 'rewrite_dress_rules' );
重写规则:

'rewrite' => array(
                        'slug' => 'dresses'// 'dresses/%designer%'
                  ),
add_filter("post_type_link","rewrite_dress",10,2);

function rewrite_dress($link, $post) {
    if(get_post_type($post) === "dress") {
        $designer = get_post($post->post_parent);
        $designer = $designer->post_name . "/";
        if(!(check_not_empty($designer))) {
         $designer = "";
        }
        //$link     = str_replace("%designer%",$designer,$link);
        $link     = str_replace("dresses/","dresses/" . $designer,$link);
    }
    return $link;

    /*
    originally the slug had a reference in it e.g. %designer%
    we could then do a string replace on it to insert the designer
    but, if you used the breadcrumbs to view the root e.g. /dresses
    then it would actually appear as /dresses/%designer% which isn't wanted.
    */

}
function rewrite_dress_rules(){
    global $wp_rewrite;
    /* /dresses/designer/dress */
    $q = "dresses/(.*)/(.*)";
    $q = "dresses/([^/]*)/([^/]*)/?";
    add_rewrite_rule($q,'index.php?post_type=dress&designer=$matches[1]&dress=$matches[2]','top');
    // $wp_rewrite->flush_rules(); // !!!
}

add_action("init", 'rewrite_dress_rules' );
这对我来说很有用。至少目前是这样。
我必须密切关注Wordpress未来的更新,看看它们是否有进一步的影响。

今天早些时候升级到WP 4.0后,我遇到了同样的问题——升级后,我的一个分层自定义帖子类型的单个帖子的视图显示为404

我对WP核心进行了一些挖掘,以了解可能导致问题的变更,并发现:

恢复到以前的代码修复了404问题。我不知道这是不是我自己的CPT实现很差的一个迹象,或者这是一个无法预见的错误。也就是说,如果其他有这个问题的人想尝试这个解决方案,这是一个相对简单的改变

从以下内容替换/wp includes/query.php中的第2558行:

if ( ! $ptype_obj->hierarchical ) {
为此:

if ( ! $ptype_obj->hierarchical || strpos($q[ $ptype_obj->query_var ], '/') === false ) { 

我发现了同样的事情。想知道这是否应该被归类为bug?