首页 > 解决方案 > 如何构建 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

标签: phpwordpresscustom-post-type

解决方案


这就是我解决的方法:

我创建了一个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。


推荐阅读