为什么会这样?对于这么多问题,我感到很抱歉,只是想尽一切办法..!再次感谢,艾琳解决方案来自上面的答案:将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!-erinJust 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, ErinOk, 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! 这篇关于当我添加自定义帖子类型的永久链接重写时,我的常规帖子永久链接将停止工作.不能同时工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-24 21:47