我正在尝试使用类别为我的网站提供结构。我将在我的网站上创建名为英格兰、威尔士、苏格兰和爱尔兰的部分。这些将是类别。我的帖子将使用自定义帖子类型-视频、指南、活动、俱乐部等。
因此,如果我使用“视频”CPT创建一个名为“南方麻烦”的帖子,将其附加到“英格兰”类别,我希望slug格式为:
category-name/custom-post-type-name/post-name
例如mysite。com/英格兰/视频/南方的麻烦我还希望以下永久链接也能像这样工作-
mysite.com/england/ - 显示该类别中的所有帖子,不考虑CPT
mysite.com/video/ - 显示所有视频CPT帖子,不考虑类别
按照我的示例,我创建了视频CPT,如下所示:
add_action( \'init\', \'register_cpt_video\' );
function register_cpt_video() {
$labels = array(
\'name\' => _x( \'videos\', \'video\' ),
some code...
);
$args = array(
some code...
\'taxonomies\' => array( \'category\' ),
some code...
\'has_archive\' => true,
\'rewrite\' => array(
\'slug\' => \'video\',
\'with_front\' => false,
\'feeds\' => true,
\'pages\' => true
),
\'capability_type\' => \'post\'
);
register_post_type( \'video\', $args );
}
这几乎满足了我的需要。我在以下位置获得正确的列表:
mysite.com/england (即类别存档页)
以及
mysite.com/video (即CPT存档页)
不幸的是,当我点击实际帖子时,slug显示为
mysite.com/video/trouble-down-south
我想
mysite.com/england/video/trouble-down-south
我尝试使用以下重写段塞
\'slug\' => \'%category%/video\',
这不起作用。
%category% 直接插入到url中,而不转换为实际的类别名称。
我在这个网站上搜索了一个解决方案,但没有找到任何解决方案。大多数相关的答案似乎都是在CPT slug上附加分类法。
i、 e类mysite.com/video/england/trouble-down-south
他们似乎是通过在分类法重写slug前面显式地加上CPT名称来实现这一点的,该slug利用了重写规则的级联特性(加上一些我不懂的其他魔法)
无论如何,我没有发现任何东西能满足我的要求category-name/custom-post-type-name/post-name, 加上我需要的归档页。
如何实现我想要的永久链接结构?
最合适的回答,由SO网友:fuxia 整理而成
你必须过滤\'post_type_link\'
创建适当的永久链接。看见this answer 对于类似的示例。
以下代码起作用:
add_action( \'init\', array ( \'WPSE_63343\', \'init\' ) );
class WPSE_63343
{
protected $post_type = \'video\';
public static function init()
{
new self;
}
public function __construct()
{
$args = array (
\'public\' => TRUE,
\'rewrite\' => array(
\'slug\' => \'[^/]+/\' . $this->post_type .\'/([^/]+)/?$\'
),
\'has_archive\' => TRUE,
\'supports\' => array( \'title\', \'editor\' ),
\'taxonomies\' => array( \'category\' ),
\'labels\' => array (
\'name\' => \'Video\',
\'singular_name\' => \'Video\'
)
);
register_post_type( $this->post_type, $args );
add_rewrite_rule(
\'[^/]+/\' . $this->post_type .\'/([^/]+)/?$\',
\'index.php?post_type=\' . $this->post_type . \'&pagename=$matches[1]\',
\'top\'
);
// Inject our custom structure.
add_filter( \'post_type_link\', array ( $this, \'fix_permalink\' ), 1, 2 );
# uncomment for debugging
# add_action( \'wp_footer\', array ( $this, \'debug\' ) );
}
/**
* Print debug data into the 404 footer.
*
* @wp-hook wp_footer
* @since 2012.09.04
* @return void
*/
public function debug()
{
if ( ! is_404() )
{
return;
}
global $wp_rewrite, $wp_query;
print \'<pre>\' . htmlspecialchars( print_r( $wp_rewrite, TRUE ) ) . \'</pre>\';
print \'<pre>\' . htmlspecialchars( print_r( $wp_query, TRUE ) ) . \'</pre>\';
}
/**
* Filter permalink construction.
*
* @wp-hook post_type_link
* @param string $post_link default link.
* @param int $id Post ID
* @return string
*/
public function fix_permalink( $post_link, $id = 0 )
{
$post = &get_post( $id );
if ( is_wp_error($post) || $post->post_type != $this->post_type )
{
return $post_link;
}
// preview
empty ( $post->slug )
and ! empty ( $post->post_title )
and $post->slug = sanitize_title_with_dashes( $post->post_title );
$cats = get_the_category( $post->ID );
if ( ! is_array( $cats ) or ! isset ( $cats[0]->slug ) )
{
return $post_link;
}
return home_url(
user_trailingslashit( $cats[0]->slug . \'/\' . $this->post_type . \'/\' . $post->slug )
);
}
}
不要忘记访问永久链接设置一次,以刷新存储的重写规则。
SO网友:Ashish Sharma
add_action( \'init\', \'register_my_types\' );
function register_my_types() {
register_post_type( \'recipes\',
array(
\'labels\' => array(
\'name\' => __( \'Recipes\' ),
\'singular_name\' => __( \'Recipee\' )
),
\'public\' => true,
\'has_archive\' => true,
)
);
register_taxonomy( \'country\', array( \'recipes\' ), array(
\'hierarchical\' => true,
\'label\' => \'Country\'
)
);
}
// Add our custom permastructures for custom taxonomy and post
add_action( \'wp_loaded\', \'add_clinic_permastructure\' );
function add_clinic_permastructure() {
global $wp_rewrite;
add_permastruct( \'country\', \'recipes/%country%\', false );
add_permastruct( \'recipes\', \'country/%country%/recipes/%recipes%\', false );
}
// Make sure that all links on the site, include the related texonomy terms
add_filter( \'post_type_link\', \'recipe_permalinks\', 10, 2 );
function recipe_permalinks( $permalink, $post ) {
if ( $post->post_type !== \'recipes\' )
return $permalink;
$terms = get_the_terms( $post->ID, \'country\' );
if ( ! $terms )
return str_replace( \'%country%/\', \'\', $permalink );
//$post_terms = array();
foreach ( $terms as $term )
$post_terms = $term->slug;
print_r($permalink);
return str_replace( \'%country%\', $post_terms , $permalink );
}
// Make sure that all term links include their parents in the permalinks
add_filter( \'term_link\', \'add_term_parents_to_permalinks\', 10, 2 );
function add_term_parents_to_permalinks( $permalink, $term ) {
$term_parents = get_term_parents( $term );
foreach ( $term_parents as $term_parent )
$permlink = str_replace( $term->slug, $term_parent->slug . \',\' . $term->slug, $permalink );
return $permlink;
}
// Helper function to get all parents of a term
function get_term_parents( $term, &$parents = array() ) {
$parent = get_term( $term->parent, $term->taxonomy );
if ( is_wp_error( $parent ) )
return $parents;
$parents[] = $parent;
if ( $parent->parent )
get_term_parents( $parent, $parents );
return $parents;
}
above code paste in theme function.php file and change the permalink option /%category/%postname%/ %