【问题标题】:How to build a Wordpress archive template to show an author's multiple post_types in single template?如何构建 Wordpress 存档模板以在单个模板中显示作者的多个 post_types?
【发布时间】:2021-06-18 07:08:47
【问题描述】:

在我的 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&amp;post_type[]=poem。

上面的 URL 正在重定向回author.php 页面。如果我只使用以下任何一种帖子类型,则 URL 将保留在 archive.php。

<a href="' . $author_page_link . '?post_type=poem">All</a>

如何在archive.php 中通过选择性自定义 post_type 显示单个作者的所有帖子?

【问题讨论】:

    标签: php wordpress custom-post-type


    【解决方案1】:

    我就是这样解决的:

    我创建了一个自定义页面模板 author-all-posts.php 和页面 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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-16
      • 1970-01-01
      • 2017-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多