邮政类型分层

时间:2014-09-09 08:40:18

标签: wordpress custom-post-type

我遇到问题,升级到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');

问题是这个帖子类型的固定链接给出了404,如果我设置了#hierarchical;如果错误,一切正常,但事情就是我需要“等级化”。设置为true,对此有什么解决方案吗?

5 个答案:

答案 0 :(得分:3)

我在今天早些时候升级到WP 4.0后遇到了同样的问题 - 升级后,我的一个分层自定义帖子类型的单个帖子的观看次数显示为404。

我做了一些深入研究WP核心,看看可能导致问题的变化是什么,并发现了这个:

https://core.trac.wordpress.org/changeset/28803

恢复到之前的代码为我修复了404问题。无法判断这是否表明我自己的CPT实施不佳或者Core中的这一变化出现了无法预料的错误。也就是说,如果有这个问题的其他人想尝试这个解决方案,这是一个相对简单的改变。

将/wp-includes/query.php中的第2558行替换为:

if ( ! $ptype_obj->hierarchical ) {

到此:

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

答案 1 :(得分:1)

我有同样的问题,我可以通过设置'hierarchical'=>来解决这个问题。 register_post_type参数中为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')
);

我仍然能够在我的代码中手动设置自定义帖子,所有内容都像以前一样再次运行。不知道为什么Wordpress 4.0破坏了东西,但我的猜测是它与新版本如何处理自定义帖子类型的永久链接有关,其中层次结构设置为true。对Wordpress代码库有更深入了解的人可能会详细说明这一点。

答案 2 :(得分:0)

您需要做的就是在行

中添加$ post_type = whatever
$children = wp_list_pages..

所以它看起来像

$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0$post_type=whatever");

这将有助于您http://codex.wordpress.org/Function_Reference/wp_list_pages#List_members_of_a_custom_post_type

答案 3 :(得分:0)

这里也有同样的问题。

我们在WordPress Shopping Cart plugin中为产品,产品类别和产品供应商开发了自定义帖子类型以及自定义重写规则,以便产品网址可以与其中的类别名称“相当”。

WordPress 4.0升级后,这些分层自定义帖子类型破裂。目前唯一的快速解决方案是发布更新,将层次结构设置为false,因为我们所有客户的产品都显示404 Page Not Found。

WordPress 4.0中一定有些变化。我们仍在研究解决方案,试图找到让它们恢复原状的最佳方法。

答案 4 :(得分:0)

我遇到了完全相同的问题,但我并没有轻易将其与Wordpress 4.0的更改联系起来。 我有几个自定义帖子类型,其中只有少数返回404页面。只有在看到您的原始问题后,才意识到问题出在那些设置为“等级”的自定义帖子类型上。 =>真正。一旦我设置了等级' =>是的,我可以看到"问题"自定义帖子。

然而,这也意味着我失去了一些在层次结构设置为true时我所拥有的URL /永久链接结构。所以,"自定义后期类型\ parent-title \ post-title"成为"定制后期类型\后标题"。

我最终设置了" post_type_link"过滤以将父项添加回链接。 我还为这个修订过的链接结构设置了重写规则。

该解决方案的灵感来自这篇文章:https://wordpress.stackexchange.com/questions/136786/parent-cpt-child-custom-post-type-url-permalink-relationship

虽然我没有使用"%" register_post_type中重写slug中的参数。 相反,在post_type_link函数中,我搜索slug并将其替换为原始slug +任何所需的前缀。我找到了这种方式,我可以保持链接中的原始slug,这是不被污染的'与"%"参数,表示该链接对于显示存档页面以及"%"参数不会出现在面包屑中。

摘自 register_post_type:

'rewrite' => array(
                        'slug' => 'dresses'// 'dresses/%designer%'
                  ),

post_type_link过滤器:

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更新,看看它们会有进一步的影响。

相关问题