要在post permalink中使用所有标签,请尝试我的类似答案的变体How to use first tag in permalinks
add_rewrite_tag( $tag, $regex )
添加可在设置/永久链接中使用的新占位符。过滤器打开post_link
将占位符转换为有用的内容,这里是所有标记段塞的列表,由-
.
调整静态变量$default
和$placeholder
满足您的需求。
然后将代码作为插件安装并激活,转到设置/永久链接,并使用如下新占位符:
add_action( \'init\', array ( \'T5_All_Tags_Permalink\', \'init\' ) );
/**
* Adds \'%tag%\' as rewrite tag (placeholder) for permalinks.
*/
class T5_All_Tags_Permalink
{
/**
* What to use when there is no tag.
*
* @var string
*/
protected static $default = \'tag\';
/**
* Used in Settings/Permalinks
*
* @var string
*/
protected static $placeholder = \'%tags%\';
/**
* Add tag and register \'post_link\' filter.
*
* @wp-hook init
* @return void
*/
public static function init()
{
add_rewrite_tag( self::$placeholder, \'([^/]+)\' );
add_filter( \'post_link\', array( __CLASS__, \'filter_post_link\' ) , 10, 2 );
}
/**
* Parse post link and replace the placeholder.
*
* @wp-hook post_link
* @param string $link
* @param object $post
* @return string
*/
public static function filter_post_link( $link, $post )
{
static $cache = array (); // Don\'t repeat yourself.
if ( isset ( $cache[ $post->ID ] ) )
return $cache[ $post->ID ];
if ( FALSE === strpos( $link, self::$placeholder ) )
{
$cache[ $post->ID ] = $link;
return $link;
}
$tags = get_the_tags( $post->ID );
if ( ! $tags )
{
$cache[ $post->ID ] = str_replace( self::$placeholder, self::$default, $link );
return $cache[ $post->ID ];
}
$slugs = wp_list_pluck( $tags, \'slug\' );
$cache[ $post->ID ] = str_replace( self::$placeholder, join( \'-\', $slugs ), $link );
return $cache[ $post->ID ];
}
}