下面是我根据自己一直在编写的一些代码编写的一些代码。我的原始代码工作正常,但我没有测试此代码,因为我没有看到您的代码。以下是您需要做的事情:
在帖子类型定义中,设置rewrite
参数为false
将以下代码放在您的帖子类型代码之后:
//Create Permalink for news posts
function wpse136458_create_permalink( $permalink, $post, $leavename, $sample ) {
$rewritecode = array(
\'%year%\',
\'%monthnum%\',
\'%day%\',
\'%hour%\',
\'%minute%\',
\'%second%\',
$leavename? \'\' : \'%postname%\',
\'%post_id%\',
\'%category%\',
\'%author%\',
$leavename? \'\' : \'%pagename%\',
\'%category-news%\'
//Add custom permalink tags here
);
if ( \'\' != $permalink && !in_array($post->post_status, array(\'draft\', \'pending\', \'auto-draft\')) ) {
$unixtime = strtotime($post->post_date);
$category = \'\';
if ( strpos($permalink, \'%category%\') !== false ) {
$cats = get_the_category($post->ID);
if ( $cats ) {
usort($cats, \'_usort_terms_by_ID\'); // order by ID
$category = $cats[0]->slug;
if ( $parent = $cats[0]->parent )
$category = get_category_parents($parent, false, \'/\', true) . $category;
}
// show default category in permalinks, without
// having to assign it explicitly
if ( empty($category) ) {
$default_category = get_category( get_option( \'default_category\' ) );
$category = is_wp_error( $default_category ) ? \'\' : $default_category->slug;
}
}
$author = \'\';
if ( strpos($permalink, \'%author%\') !== false ) {
$authordata = get_userdata($post->post_author);
$author = $authordata->user_nicename;
}
$date = explode(" ",date(\'Y m d H i s\', $unixtime));
$cat_news = \'\';
if ( strpos($permalink, \'%category-news%\') !== false ) {
$news_terms = wp_get_object_terms( $post->ID, \'category-news\' );
if( $news_terms ) {
$cat_news = $news_terms[0]->slug;
}
}
//Enter permalink manipulations here
$rewritereplace = array(
$date[0],
$date[1],
$date[2],
$date[3],
$date[4],
$date[5],
$post->post_name,
$post->ID,
$category,
$author,
$post->post_name,
$cat_news
//Add custom tag replacements here
);
$permalink = str_replace($rewritecode, $rewritereplace, $permalink);
}
return $permalink;
}
add_filter( \'post_type_link\', \'wpse136458_create_permalink\', 10, 4 );
//Helper functions
function struct_to_query( $struct, $post_type_slug ) {
global $wp_rewrite ;
$querycode = array_merge( $wp_rewrite->queryreplace, array( \'paged=\', \'feed=\' ) );
$tagcode = array_merge( $wp_rewrite->rewritecode, array( \'%paged%\', \'%feed%\' ) );
$num_toks = preg_match_all(\'/%.+?%/\', $struct, $toks);
$tokens = $toks[0];
$query_result = str_replace( $tagcode, $querycode, $tokens );
$query_string = \'\';
foreach( $query_result as $index => $value ) $query_string .= \'&\' . $value . \'$matches[\' . (string)( (int) $index + 1 ). \']\';
return \'index.php?post_type=\' . $post_type_slug . $query_string;
}
function struct_to_regex( $struct ) {
global $wp_rewrite ;
$regexcode = array_merge( $wp_rewrite->rewritereplace, array( \'([0-9]{1,})\', \'(feed|rdf|rss|rss2|atom)\' ) );
$tagcode = array_merge( $wp_rewrite->rewritecode, array( \'%paged%\', \'%feed%\' ) );
return rtrim( str_replace( $tagcode, $regexcode, $struct ), \'/\' ) . \'?/?$\';
}
function wpse136458_tax_regex( $taxonomy ) {
$terms = get_terms( $taxonomy, array( \'hide_empty\' => false ) );
$slugs = wp_list_pluck( $terms, \'slug\' );
return \'(\' . implode( \'|\', $slugs) . \')\';
}
//Creates the complex rewrite rules
function wpse136458_rewrite_api() {
add_rewrite_tag( \'%category-news%\', wpse136458_tax_regex( \'category-news\' ) );
$structs = array(
\'/news/%category-news%/%year/%monthnum%/%day%/page/%paged%/\',
\'/news/%category-news%/%year/%monthnum%/%day%/feed/%feed%/\',
\'/news/%category-news%/%year/%monthnum%/%day%/%feed%/\',
\'/news/%category-news%/%year/%monthnum%/%day%/\',
\'/news/%category-news%/%year/%monthnum%/page/%paged%/\',
\'/news/%category-news%/%year/%monthnum%/feed/%feed%/\',
\'/news/%category-news%/%year/%monthnum%/%feed%/\',
\'/news/%category-news%/%year/%monthnum%/\',
\'/news/%category-news%/%year/page/%paged%/\',
\'/news/%category-news%/%year/feed/%feed%/\',
\'/news/%category-news%/%year/%feed%/\',
\'/news/%category-news%/%year/\',
\'/news/%category-news%/page/%paged%/\',
\'/news/%category-news%/feed/%feed%/\',
\'/news/%category-news%/%feed%/\',
\'/news/%category-news%/%postname%/\',
\'/news/%category-news%/\',
\'/news/%year/%monthnum%/%day%/page/%paged%/\',
\'/news/%year/%monthnum%/%day%/feed/%feed%/\',
\'/news/%year/%monthnum%/%day%/%feed%/\',
\'/news/%year/%monthnum%/%day%/\',
\'/news/%year/%monthnum%/page/%paged%/\',
\'/news/%year/%monthnum%/feed/%feed%/\',
\'/news/%year/%monthnum%/%feed%/\',
\'/news/%year/%monthnum%/\',
\'/news/%year/page/%paged%/\',
\'/news/%year/feed/%feed%/\',
\'/news/%year/%feed%/\',
\'/news/%year/\',
\'/news/page/%paged%/\',
\'/news/feed/%feed%/\',
\'/news/%feed%/\',
\'/news/\',
);
foreach( $structs as $struct ) {
add_rewrite_rule(
struct_to_regex( $struct ),
struct_to_query( $struct, \'news\' ),
\'top\'
);
}
//This creates permalinks for news posts
add_permastruct( \'news\', \'news/%category-news%/%postname%/\', array( \'walk_dirs\' => false, \'endpoints\'=>false ) );
}
add_action( \'init\', \'wpse136458_rewrite_api\' );
//Flush rewrite rules when adding, removing or deleting terms
// Taxonomy created
add_action( \'created_category-news\', \'flush_rewrite_rules\' );
// Taxonomy edited
add_action( \'edited_category-news\', \'flush_rewrite_rules\' );
// Taxonomy Deleted
add_action( \'delete_category-news\', \'flush_rewrite_rules\' );
下班后我会发布一篇深入的解释。如果对你有用,请告诉我
编辑:好的,下面是基本情况:wpse136458_rewrite_api
这是工作原理的核心。它基本上是创建一个permalink结构数组,这些结构由遵循您指定的规则的重写标记组成,然后使用helper函数从这些结构创建重写规则struct_to_query
(创建查询字符串)和struct_to_regex
(创建regex)。我刚刚添加的函数的第一行为category-news
分类法和调用add_permastruct
创建WP用于特定news
职位。
wpse136458_create_permalink
是对post-type_link
接受该值的筛选器news
permalink并用正确的数据填充,如类别和postname