您可以通过插件向permalinks添加自定义slug,并在自定义permalinks中使用它;别忘了更新永久链接。更新规则很重要。以下源代码是一个exmaple,用于字符串的post格式%postformat%
, 但不是完美的代码。最好是装在钩子上plugins_loaded
并没有创建新的类,但足以现在测试和使用它。
请检查此项,当前未测试。我写这个已经很久了。
<?php
/**
* Plugin Name: Post Format Permalink
* Description: Allow to use Post Format as Permalink; inlcude %postformat% in custom permalink
*/
class Fb_Add_Post_Format_Permalink {
function __construct() {
add_filter( \'pre_post_link\', array( $this, \'generate_permalink\' ), 10, 2 );
add_filter( \'post_rewrite_rules\', array( $this, \'rewrite_rules\' ) );
add_filter( \'generate_rewrite_rules\', array( $this, \'generate_rewrite_rules\' ) );
}
function generate_permalink( $permalink, $post ) {
global $standard_slug;
if ( FALSE === strpos( $permalink, \'%postformat%\' ) )
return $permalink;
if ( ! is_object( $post ) )
$post = get_post( $post_id );
$postformat = get_post_format( $post->ID );
if ( empty( $postformat ) )
$postformat = ! empty($standard_slug) ? $standard_slug : \'standard\';
return str_replace( \'%postformat%\', $postformat, $permalink );
}
function generate_rewrite_rules( $wp_rewrite ) {
global $clean_post_rewrites;
$wp_rewrite->rules = $wp_rewrite->rules + $clean_post_rewrites;
}
function rewrite_rules( $post_rewrite ) {
global $clean_post_rewrites, $wp_rewrite, $standard_slug;
$wp_rewrite->use_verbose_page_rules = TRUE;
$post_format_slugs = implode( \'|\', get_post_format_slugs() );
if ( ! empty($standard_slug) )
$post_format_slugs = preg_replace( \'|standard|\', $standard_slug, $post_format_slugs, 1 );
while ( list($k, $v) = each( $post_rewrite ) ) {
$new_k = preg_replace( \'|%postformat%|\', \'(\' . $post_format_slugs . \')\', $k, 1 );
$clean_post_rewrites[$new_k] = $v;
}
return $post_rewrite;
}
}
$post_format = new Fb_Add_Post_Format_Permalink();