简体   繁体   English

Wordpress - 自定义帖子类型和自定义永久链接结构错误

[英]Wordpress - Custom Post Type and Custom Permalink Structures Error

I have 3 custom post types setup and I have created a custom permalink structure for each. 我有3个自定义帖子类型设置,我为每个创建了自定义永久链接结构。 The problem I am having is that the third custom post type I have setup with the custom permalink, is being used for the previous 2. 我遇到的问题是我使用自定义永久链接设置的第三个自定义帖子类型正用于前两个。

Example: 例:

  • Classified 分类
  • Employment 雇用
  • Sponsorship 赞助

The above are the post types and I have the custom permalink structure set to (for each): /post_type/post_id/ 以上是帖子类型,我将自定义永久链接结构设置为(对于每个):/ post_type / post_id /

Now, The above order are the order they are set in and sponsorship takes over all of them. 现在,上面的顺序是他们设置的顺序,赞助接管所有这些顺序。 So for instance: 例如:

/classified/100/
/employment/101/
/sponsorship/102/

The above all use the permalink: 以上都使用永久链接:

/sponsorship/100/
/sponsorship/101/
/sponsorship/102/

What am I doing wrong to get this error happening? 发生此错误的错误是什么? Is it a permalink structure error? 它是永久链接结构错误吗? below is my code I am using to create the custom permalink structures for each. 下面是我用于为每个创建自定义永久链接结构的代码。 The only difference is that %c_id% is changed to %e_id% and %s_id% for classifieds, employment and sponsorship. 唯一的区别是%c_id%更改为分类,就业和赞助的%e_id%和%s_id%。 And also any reference to 'classifieds' is changed to employment and sponsorship respectively. 此外,对“分类广告”的任何提及都分别改为就业和赞助。

add_action('init', 'classifieds_rewrite');
function classifieds_rewrite() {
    global $wp_rewrite;

    $queryarg = 'post_type=classifieds&p=';
    $wp_rewrite->add_rewrite_tag('%c_id%', '([^/]+)', $queryarg);
    $wp_rewrite->add_permastruct('classifieds', '/classifieds/%c_id%/', false);
}

add_filter('post_type_link', 'classifieds_permalink', 1, 3);
function classifieds_permalink($post_link, $id = 0) {
    global $wp_rewrite;
    $post = &get_post($id);
    if ( is_wp_error( $post ) )
        return $post;
    $newlink = $wp_rewrite->get_extra_permastruct('classifieds');
    $newlink = str_replace("%c_id%", $post->ID, $newlink);
    $newlink = home_url(user_trailingslashit($newlink));
    return $newlink;
}

Thanks for the help! 谢谢您的帮助! :) :)

If I understand correctly, you registered 3 different post types and you are trying to rewrite each post type so their slug precedes the post id. 如果我理解正确,你注册了3种不同的帖子类型,并且你试图重写每个帖子类型,所以他们的slug在post id之前。 To start with just the slug of each post type, it doesn't seem like you are issuing the rewrite when you register each post type, is this correct? 从每个帖子类型的slug开始,当你注册每个帖子类型时,你似乎没有发出重写,这是正确的吗? If not, you can take care of this part by adding the following with the other arguments when registering each post type. 如果没有,您可以通过在注册每个帖子类型时添加以下内容和其他参数来处理此部分。

'rewrite' => array( 'slug' => 'classified', 'with_front' => true )

Additional reference with extensive permalink rewrites here: http://shibashake.com/wordpress-theme/wordpress-permalink-add 其他参考资料包括广泛的永久链接重写: http//shibashake.com/wordpress-theme/wordpress-permalink-add

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

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