php - 如何构建 Wordpress 存档模板以在单个模板中显示作者的多个 post_types?
问题描述
在我的 WordPress v5.7 中,目前我有两个自定义 post_type:歌曲和诗歌。
根据网站设计的要求,我有一个自定义author.php
模板,其中包含作者简介和来自两个 post_type 的最新 10 个帖子,没有分页。这是 pastebin ( https://pastebin.com/kCrYebcD ) 的作者模板。
如果作者有超过 10 个来自两个 post_type 的帖子,我想在archive.php
带有分页的模板中显示来自两个 post_type 的所有帖子。这是 pastebin ( https://pastebin.com/twkWn5Bc ) 的存档模板。
在author.php
我有这个 URL 重定向到所有帖子存档页面:
<?php
$author_page_link = esc_url(get_author_posts_url(false, ID->user_nicename));
echo '<a href="' . $author_page_link . '?post_type[]=song&post_type[]=poem">All</a>';
?>
这是 URL 的构造方式:http://www.local.site/author/one/?post_type[]=song&post_type[]=poem
.
上面的 URL 正在重定向回author.php
页面。如果我只使用以下任何一种帖子类型,则 URL 将保留在archive.php
.
<a href="' . $author_page_link . '?post_type=poem">All</a>
如何从选择性自定义 post_type 中显示单个作者的所有帖子archive.php
?
解决方案
这就是我解决的方法:
我创建了一个author-all-posts.php
带有 page slug的自定义页面模板all
。我使用了如下的自定义 URL 参数:
http://www.local.site/all/?author_id=1
在我的自定义页面模板中,我有以下代码:
// get the author ID
$authorID = $_GET['author_id'];
在下方WP_Query
获取来自多个 post_types 的所有帖子:
$all_args = array(
'author' => $authorID,
'post_type' => array('song', 'poem'),
'posts_per_page' => -1
);
query_posts($all_args);
接下来我正在努力add_rewrite_rule
消除丑陋的 URL。
推荐阅读
- lisp - GCL中的条件如何处理?
- c# - 如何使用表达式选择组中的第一行
- python - 在“导入的模块 ete3”中找不到参考“NCBITaxonomy”
- c# - NodeJS解密程序到C#
- bash - sed Mac 终端删除以一个单词开头但不以另一个单词结尾的行
- apache-kafka - 如何使用 SMT 选择 CDC JSON 的内部字段作为 Kafka 中的记录键?
- sharepoint - 如何在 SharePoint 设计器 2013 中使用 jQuery 更改日期时弹出窗口
- c# - 在哪里使用 RSAPKCS1KeyExchangeFormatter 和 RSACryptoServiceProvider?
- reactjs - React Material表如何更改列中的选择[复选框]位置?
- c# - C# 在 XmlSerializer 中使用结构而不创建单独的 xml 节点