当我添加自定义帖子类型永久链接重写时,我的常规帖子永久链接会停止工作。无法让两者同时工作

问题描述 投票:3回答:1

真的很挣这个,所以任何帮助都会非常感激。我的网站有常规帖子帖子和一个名为“文章”的自定义帖子类型。

我试图让它工作,以便我的常规帖子将使用/%category%/ postname%/ permalink结构(我在设置中设置)。这工作正常,直到我为我的文章帖子类型添加自定义重写。我希望文章遵循/%问题%/%postname%/ structure。我可以通过以下方式使这项工作变得更好:

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。

为什么我无法让两者同时工作?我在某个地方错过了一块吗?

谢谢你的建议!

-erin


只是跟进我的问题 - 也许我真正在努力解决的问题是为什么post_type_filter函数影响的不仅仅是我指定的文章帖子类型?

谢谢,艾琳


好的,还有一个超级奇怪的事情。如果我在自定义帖子链接的末尾传递一个查询参数,这一切都有效,所以这有效:http://www.mysitename.com/spring-2015/test-article-here/?post_type=article但这给了我一个404 http://www.mysitename.com/spring-2015/test-article-here/

那为什么会这样?我很抱歉这么多问题,只是真的想要深究这个......!

艾琳,再次感谢

wordpress custom-post-type permalinks custom-taxonomy
1个回答
0
投票

'slug' => '%issue%',替换'slug' => 'article/%issue%',

它肯定会有效,我已经测试过了。请注意,“文章”是帖子类型名称。

在进行上述更改后,还可以保存永久链接。

© www.soinside.com 2019 - 2024. All rights reserved.