当我添加自定义帖子类型的永久链接重写时,我的常规帖子永久链接将停止工作.不能同时工作 [英] When I add custom post type permalink rewrite, my regular post permalinks stop working. Can't get both to work at the same time

查看:72
本文介绍了当我添加自定义帖子类型的永久链接重写时,我的常规帖子永久链接将停止工作.不能同时工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

真的在为此苦苦挣扎,因此任何帮助将不胜感激.我的网站既有常规的帖子,又有一种称为文章"的自定义帖子类型.

我正在尝试使其正常工作,以便我的常规帖子将使用/%category%/postname%/永久链接结构(已在设置中设置).在我为我的文章帖子类型添加自定义重写之前,此方法工作正常.我希望文章遵循/%issue%/%postname%/结构.我可以通过以下方法使它正常工作:

add_filter( 'post_type_link', 'wpa_show_permalinks', 1, 2 );

function wpa_show_permalinks( $post_link, $id = 0 ){
    $post = get_post($id);
    if ( is_object( $post ) && $post->post_type == 'article' ){
        $terms = wp_get_object_terms( $post->ID, 'issue_tax' );
        if( $terms ){
            return str_replace( '%issue%' , $terms[0]->slug , $post_link );
        }
    }
    return $post_link;
}

我的帖子类型是这样注册的:

function article_post_type() {

$labels = array(
    'name'                => _x( 'Magazine Articles', 'Post Type General Name', 'text_domain' ),
    'singular_name'       => _x( 'Magazine Article', 'Post Type Singular Name', 'text_domain' ),
    'menu_name'           => __( 'Magazine Articles', 'text_domain' ),
    'name_admin_bar'      => __( 'Magazine Articles', 'text_domain' ),
    'parent_item_colon'   => __( 'Parent Article:', 'text_domain' ),
    'all_items'           => __( 'All Articles', 'text_domain' ),
    'add_new_item'        => __( 'Add New Article', 'text_domain' ),
    'add_new'             => __( 'Add New', 'text_domain' ),
    'new_item'            => __( 'New Article', 'text_domain' ),
    'edit_item'           => __( 'Edit Article', 'text_domain' ),
    'update_item'         => __( 'Update Article', 'text_domain' ),
    'view_item'           => __( 'View Article', 'text_domain' ),
    'search_items'        => __( 'Search Article', 'text_domain' ),
    'not_found'           => __( 'Not found', 'text_domain' ),
    'not_found_in_trash'  => __( 'Not found in Trash', 'text_domain' ),
);
$rewrite = array(
    'slug'                => '%issue%',
    'with_front'          => false,
    'pages'               => true,
    'feeds'               => true,
);
$args = array(
    'label'               => __( 'article', 'text_domain' ),
    'description'         => __( 'Magazine Articles and Features', 'text_domain' ),
    'labels'              => $labels,
    'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'trackbacks', 'revisions', 'custom-fields', 'page-attributes', ),
    'taxonomies'          => array( 'issue_tax', 'category', 'featured_media', 'tag' ),
    'hierarchical'        => false,
    'public'              => true,
    'show_ui'             => true,
    'show_in_menu'        => true,
    'menu_position'       => 5,
    'menu_icon'           => 'dashicons-welcome-write-blog',
    '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',
);
register_post_type( 'article', $args );

}


add_action( 'init', 'article_post_type', 0 );

我添加了这一点,重置了永久链接设置,并且文章永久链接按预期开始工作-但是-一旦我开始工作,我的常规帖子就会开始显示404.

为什么我不能同时使两个人同时工作?我在某处错过了一块吗?

感谢您的任何建议!

-艾琳


只是我问题的跟进-也许我真正在挣扎的是为什么post_type_filter函数不仅影响我指定的文章发布类型,还影响更多?

谢谢, 艾琳


好吧,还有一件超级奇怪的事情.如果我在自定义帖子链接的末尾传递一个查询参数,那么所有这些都可以工作,所以它可以工作:解决方案

来自上面的答案:
Replace 'slug' => '%issue%',替换为'slug' => 'article/%issue%',

这对我来说并不完全有效,因此我也不得不更改post_type_link过滤器.
我要做的就是更改以下内容:

return str_replace( '%issue%' , $terms[0]->slug , $post_link );

使用:

return str_replace( 'article/%issue%' , $terms[0]->slug , $post_link );

有点奇怪,我花了几个小时才弄清楚这一点.希望它对以后的人有所帮助!

really struggling with this one, so any help would be much appreciated. My site has both regular post posts, and a custom post type called "articles."

I'm trying to get it to work so that my regular posts will use the /%category%/postname%/ permalink structure, (which I have set up in settings). This is working fine, until I add a custom rewrite for my article post type. I'd like articles to follow a /%issue%/%postname%/ structure. I can get this working great with the following:

add_filter( 'post_type_link', 'wpa_show_permalinks', 1, 2 );

function wpa_show_permalinks( $post_link, $id = 0 ){
    $post = get_post($id);
    if ( is_object( $post ) && $post->post_type == 'article' ){
        $terms = wp_get_object_terms( $post->ID, 'issue_tax' );
        if( $terms ){
            return str_replace( '%issue%' , $terms[0]->slug , $post_link );
        }
    }
    return $post_link;
}

where my post type is registered like this:

function article_post_type() {

$labels = array(
    'name'                => _x( 'Magazine Articles', 'Post Type General Name', 'text_domain' ),
    'singular_name'       => _x( 'Magazine Article', 'Post Type Singular Name', 'text_domain' ),
    'menu_name'           => __( 'Magazine Articles', 'text_domain' ),
    'name_admin_bar'      => __( 'Magazine Articles', 'text_domain' ),
    'parent_item_colon'   => __( 'Parent Article:', 'text_domain' ),
    'all_items'           => __( 'All Articles', 'text_domain' ),
    'add_new_item'        => __( 'Add New Article', 'text_domain' ),
    'add_new'             => __( 'Add New', 'text_domain' ),
    'new_item'            => __( 'New Article', 'text_domain' ),
    'edit_item'           => __( 'Edit Article', 'text_domain' ),
    'update_item'         => __( 'Update Article', 'text_domain' ),
    'view_item'           => __( 'View Article', 'text_domain' ),
    'search_items'        => __( 'Search Article', 'text_domain' ),
    'not_found'           => __( 'Not found', 'text_domain' ),
    'not_found_in_trash'  => __( 'Not found in Trash', 'text_domain' ),
);
$rewrite = array(
    'slug'                => '%issue%',
    'with_front'          => false,
    'pages'               => true,
    'feeds'               => true,
);
$args = array(
    'label'               => __( 'article', 'text_domain' ),
    'description'         => __( 'Magazine Articles and Features', 'text_domain' ),
    'labels'              => $labels,
    'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'trackbacks', 'revisions', 'custom-fields', 'page-attributes', ),
    'taxonomies'          => array( 'issue_tax', 'category', 'featured_media', 'tag' ),
    'hierarchical'        => false,
    'public'              => true,
    'show_ui'             => true,
    'show_in_menu'        => true,
    'menu_position'       => 5,
    'menu_icon'           => 'dashicons-welcome-write-blog',
    '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',
);
register_post_type( 'article', $args );

}


add_action( 'init', 'article_post_type', 0 );

I add that, reset permalink settings, and the article permalinks start working as indended - BUT - as soon as I get that working, my regular posts start displaying a 404.

Why am I unable to get both to work at the same time? Am I missing a piece somewhere?

Thanks for any advice!

-erin


Just a follow up to my question - perhaps the thing that I'm really struggling with is why is the post_type_filter function affecting more than just the article post type that I have specified?

Thanks, Erin


Ok, one more super strange thing. This all works if I pass a query parameter at the end of my custom post links, so this works: http://www.mysitename.com/spring-2015/test-article-here/?post_type=article but this gives me a 404 http://www.mysitename.com/spring-2015/test-article-here/

Why would that be? I'm sorry for so many questions, just really trying to get to the bottom of this..!

Thanks again, Erin

解决方案

From the answer above:
Replace: Replace 'slug' => '%issue%', with 'slug' => 'article/%issue%',

That didn't work fully for me, so I had to change my post_type_link filter as well.
All I had to do was to change following:

return str_replace( '%issue%' , $terms[0]->slug , $post_link );

With:

return str_replace( 'article/%issue%' , $terms[0]->slug , $post_link );

It's a bit strange and I spent several hours figuring this out. Hope it helps someone in the future!

这篇关于当我添加自定义帖子类型的永久链接重写时,我的常规帖子永久链接将停止工作.不能同时工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆