我为自定义帖子类型创建父子关系.
通用:www.example.com/parent/parent_post
示例:www.example.com/projects/project-one
在上面的URL中,父级是自定义帖子类型,父级帖子是其单个帖子.我可以将父母的所有帖子和单个帖子分别显示为archive-parent.php和single-parent.php.
正如我之前提到的,我创建了父子关系,并将子帖子存储为'post_parent'作为父ID.
通用:www.example.com/child/parent_post/child_post
样本:www.example.com/project_article/project-one/first-article
对于特定的子帖子,URL将如上所述.
下面的代码是为了获得特定的子邮件.它工作正常.
function my_add_rewrite_rules() {
add_rewrite_tag('%child%', '([^/]+)', 'child=');
add_permastruct('child', 'child/%parent%/%child%', false);
add_rewrite_rule('^child/([^/]+)/([^/]+)/?','index.php?child=$matches[2]','top');
}
add_action( 'init', 'my_add_rewrite_rules' );
function my_permalinks($permalink, $post, $leavename) {
$post_id = $post->ID;
if($post->post_type != 'child' || empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft')))
return $permalink;
$parent = $post->post_parent;
$parent_post = get_post( $parent );
$permalink = str_replace('%parent%', $parent_post->post_name, $permalink);
return $permalink;
}
add_filter('post_type_link', 'my_permalinks', 10, 3);
Run Code Online (Sandbox Code Playgroud)
通用:www.example.com/child/parent_post
示例:www.example.com/project_article/project-one
现在我想要所有带有父帖的子帖,如上面的URL.
我是新手,请按指南.
假设parent作为父自定义帖子类型,child作为子自定义帖子类型,并希望您需要子帖子 URL,例如http://www.example.com/parent/parent-post/child/child-post而不是http://www.example.com/child/parent-post/child-post.
将您的功能更改my_add_rewrite_rules()为以下内容。
function my_add_rewrite_rules() {
add_rewrite_tag('%child%', '([^/]+)', 'child=');
add_permastruct('child', '/parent/%parent%/child/%child%', false);
add_rewrite_rule('^parent/([^/]+)/child/([^/]+)/?','index.php?child=$matches[2]','top');
}
add_action( 'init', 'my_add_rewrite_rules' );
Run Code Online (Sandbox Code Playgroud)
更新后,不要忘记通过“设置 > 永久链接”刷新永久链接。
| 归档时间: |
|
| 查看次数: |
175 次 |
| 最近记录: |