Warm tip: This article is reproduced from serverfault.com, please click

wordpress-显示所有帖子的存档,但不显示 single.php

(wordpress - Show the archive of all the posts but not the single.php)

发布于 2020-12-28 13:21:20

有人可以建议我最好的行动方案。

我为一位专门从事安乐死的兽医创建了一个自定义 wordpress 主题,他想要一个简单的联系表样式的纪念墙,以添加客户缩略图和几行纪念文本。

所以我创建了一个 wordpress 目录 (single-memorial.php),它在 (archive-memorial.php) 上显示自定义帖子。到目前为止一切都很好...

但我只希望 ARCHIVE 索引和可见。不是single-memorial.php

在循环中,我没有将帖子磁贴包装在任何 the_permalinks 中。所以缩略图没有链接到单个帖子数据,基本上使单个帖子完全孤立,除非你当然猜到了slug并直接找到了它们。

然而,单个帖子是否可以编入索引,我不想要,我宁愿不需要不必要地设置 single-memorial.php 的样式 - 因为我不希望任何人看到它们。

信息如此之少,它们就不是相关页面。

那么我怎样才能保持发布数据公开,这样 ARCHIVE 就可以工作,但不显示 single-memorial.php 页面?

我正在考虑使用 single-memorial.php 的任何单个帖子的函数文件中的某种通用 301 重定向到 ARCHIVE 或 HOME?

我可以做一个 .htaccess 重定向,但我不确定如何定位正确的帖子,因为我不知道客户端创建的 URL,他添加了新的纪念馆。

非常欢迎建议,提前致谢。

存档页面

// Memorials Custom Post Type
function memorials_custom_post() {
    $args = array(
        'labels' => array(
            'name'             => 'Memorials',
            'singular_name'    => 'Memorial'
        ),
        'show_ui'              => true,
        'show_in_nav_menus'    => true,
        'has_archive'          => true,
        'supports'             => array(
                                  'title', 'editor', 'thumbnail', 'post-formats'),
        'description'          => 'Pet memorial catalogue',
        'hierarchical'         => true,
        'show_in_nav_menus'    => true,
        'exclude_from_search'  => true,
        'publicly_queryable'   => true,
        'menu_position'        => 23,
        'menu_icon'            => 'dashicons-format-quote'
    );
    register_post_type( 'memorial-wall', $args );
}
add_action( 'init', 'memorials_custom_post' );

// Redirect Memorial Single Custom Post
function redirect_single_memorial_post() {
    if ( is_singular( 'memorial' )) {
        wp_redirect( get_post_type_archive_link( 'memorial-wall' ), 301 );
        exit;
    }
}
add_action( 'template_redirect', 'redirect_single_memorial_post' );
Questioner
DanG
Viewed
0
amarinediary 2021-01-01 06:44:38

当你注册自定义帖子类型 (CPT) 时,你可以指定参数列表。

帖子类型可以支持任意数量的内置核心功能,例如元框、自定义字段、帖子缩略图、帖子状态、评论等。

register_post_type( string $post_type, array|string $args = array() )

$ args 描述
'上市' (bool) 帖子类型是否旨在通过管理界面或前端用户公开使用。虽然 $exclude_from_search、$publicly_queryable、$show_ui 和 $show_in_nav_menus 的默认设置是从 public 继承的,但每个设置都不依赖于这种关系并控制一个非常具体的意图。默认为false。
'exclude_from_search' (bool) 是否从前端搜索结果中排除此帖子类型的帖子。默认值是 $public 的相反值。
'public_queryable' (bool) 作为 parse_request() 的一部分,是否可以在前端对 post 类型执行查询。
'has_archive' (bool/string) 是否应该有 post 类型的档案,或者如果是字符串,则使用档案 slug。如果启用 $rewrite,将生成正确的重写规则。默认为false。
'show_ui' (bool) 是否生成并允许在管理员中管理此帖子类型的 UI。默认值为 $public。
'show_in_nav_menus' (bool/string) 在管理菜单中显示帖子类型的位置。要工作, $show_ui 必须为真。如果为 true,则帖子类型将显示在其自己的顶级菜单中。如果为 false,则不显示菜单。如果现有顶级菜单的字符串(例如'tools.php' 或'edit.php?post_type=page'),则帖子类型将作为其子菜单放置。默认值为 $show_ui 的值。

使用publicexclude_from_searchpublicly_queryableshow_uishow_in_nav_menushas_archive参数,我们可以建立你要找的行为。

$args = array(
  'public' => false, //Default is false, can be omitted
  'show_ui' => true, //Default inherited from public, has to be specified
  'show_in_nav_menus' => true, //Default inherited from public, has to be specified
  'exclude_from_search' => true, //Default is the opposite value of $public, can be omitted
  'publicly_queryable' => true, //Default inherited from public, has to be specified
  'has_archive' => true, //Default is false, has to be specified
  //...Your other arguments...
);

显示档案页面要求用户能够查询帖子类型。未经测试,但应该可以工作。


学到更多