可以通过在post_link
滤器此筛选器允许您更改最终的post链接。
由于您正在permalink中显示类别,因此我假设您的permalink结构包含%category%
在里面。
下面的代码将帮助您以想要的方式获取链接。
add_filter( \'post_link\', \'wdm_change_category_permalink_structure\', 10, 3 );
function wdm_change_category_permalink_structure( $post_link, $post, $leavename ) {
$array_of_cats_to_be_replaced = array( \'europe\', \'usa\' ); //array of category slugs. Add more slugs here
$get_permalink_structure = get_option( \'permalink_structure\' );
if ( strpos( $get_permalink_structure, \'%category%\' ) !== false ) {
$cats = get_the_category( $post->ID ); //get categories assocaited with the post
if ( $cats ) {
usort( $cats, \'_usort_terms_by_ID\' ); // order by ID
$category_object = get_term( $cats[0], \'category\' ); //Take first category
$category = $category_object->slug; //Get slug of category
if ( in_array( $category, $array_of_cats_to_be_replaced ) ) { //If category we want is in the array
$rewritecode = array(
\'%year%\',
\'%monthnum%\',
\'%day%\',
\'%hour%\',
\'%minute%\',
\'%second%\',
$leavename ? \'\' : \'%postname%\',
\'%post_id%\',
\'%category%\',
\'%author%\',
$leavename ? \'\' : \'%pagename%\',
);
$rewritereplace = array(
$date[0],
$date[1],
$date[2],
$date[3],
$date[4],
$date[5],
$post->post_name,
$post->ID,
\'news/view\', //Replace category with news/view
$author,
$post->post_name,
);
$post_link = home_url( str_replace( $rewritecode, $rewritereplace, $get_permalink_structure ) );
$post_link = user_trailingslashit( $post_link, \'single\' );
}
}
}
return $post_link;
}
希望有帮助:)