我有一个网站,其中有自定义帖子类型(来自CoAuthors Plus的客座作者)。使用 一个插件,我设法使自定义类型“客座作者”的帖子可以通过 WordPress 旧版搜索进行搜索。
现在,作者可以正确显示在搜索结果中。尽管如此,它们链接到了错误的页面,
/?post_type=guest-author&p=2148
,这会导致 404。
我希望能够获取 URL、解释它并重定向到正确的页面(其形式为
/archives/author/name-surname/
。
我试图让它与重写 URL 一起工作,但我无法捕获数据并制定重写。
以下代码更改
guest-authors
的永久链接。它使用 CoAuthor 插件中的方法来输出客座作者链接。
至少现在你根据插件的意图有了正确的链接。
它们的形式如下:
{site_url}/author/{author_slug}
这是要包含在functions.php中的代码:
function adjust_permalink($permalink, $post){
$post_type = get_post_type($post);
if($post_type === 'guest-author'){
global $coauthors_plus;
$author = $coauthors_plus->get_coauthor_by('ID', $post->ID);
$permalink = get_author_posts_url( $author->ID, $author->user_nicename );
}
return $permalink;
}
add_filter('post_type_link','adjust_permalink',10,2);
现在,您应该能够在主题中为作者创建模板 php 文件:
author.php
我只是想说,我来这里是为了解决这个问题,并且截至 2023 年 11 月它仍然有效。