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

Show the archive of all the posts but not the single.php

有人可以告诉我最好的行动方案吗?

我为专门从事安乐死的兽医创建了一个自定义 wordpress 主题,他想要一个简单的联系方式 sheet 风格的纪念墙来添加客户缩略图,只有几行纪念文字。

所以我创建了一个 wordpress 目录 (single-memorial.php),它在 (archive-memorial.php) 上显示自定义 posts。目前一切顺利...

但我只想将 ARCHIVE 编入索引并使其可见。不是单-memorial.php

在循环中,我没有将 post 块包裹在任何 the_permalinks 中。所以缩略图没有链接到单个 post 数据,基本上使单个 post 成为完全的孤儿,除非你当然猜到了 slug 并直接找到了它们。

然而,单个 posts 将是可索引的,不是吗,我不想要,我宁愿不需要不必要地设置单个 memorial.php 的样式 - 因为我不想让任何人看到它们。

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

那么我怎样才能保留 post 数据 public,这样 ARCHIVE 就可以工作,但不会显示单个 memorial.php 页面?

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

我可以进行 .htaccess 重定向,但我不确定如何定位正确的 post,因为我不知道客户端创建的 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' );

注册自定义 post 类型 (CPT) 时,您可以指定参数列表。

Post types can support any number of built-in core features such as meta boxes, custom fields, post thumbnails, post statuses, comments, and more.

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

$args Description
'public' (bool) Whether a post type is intended for use publicly either via the admin interface or by front-end users. While the default settings of $exclude_from_search, $publicly_queryable, $show_ui, and $show_in_nav_menus are inherited from public, each does not rely on this relationship and controls a very specific intention. Default false.
'exclude_from_search' (bool) Whether to exclude posts with this post type from front end search results. Default is the opposite value of $public.
'publicly_queryable' (bool) Whether queries can be performed on the front end for the post type as part of parse_request().
'has_archive' (bool/string) Whether there should be post type archives, or if a string, the archive slug to use. Will generate the proper rewrite rules if $rewrite is enabled. Default false.
'show_ui' (bool) Whether to generate and allow a UI for managing this post type in the admin. Default is value of $public.
'show_in_nav_menus' (bool/string) Where to show the post type in the admin menu. To work, $show_ui must be true. If true, the post type is shown in its own top level menu. If false, no menu is shown. If a string of an existing top level menu (eg. 'tools.php' or 'edit.php?post_type=page'), the post type will be placed as a sub-menu of that. Default is value of $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...
);

显示存档页面要求用户能够查询 post 类型。未经测试但应该有效。


了解更多

您可以将单个页面重定向到存档

add_action( 'template_redirect', 'prefix_redirect_single_cpt' );
// Redirect Memorial CPT
function prefix_redirect_single_cpt() {
    if ( is_singular( 'memorial' )) {
        wp_redirect( get_post_type_archive_link( 'memorial' ), 301 );
        exit;
    }
}

带有 301 重定向的 CPT 工作代码(如果在浏览器中正确输入了单个 post slug),重定向到 CPT 存档。

function custom_post_type() {
    $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', 'custom_post_type' );

// Redirect Single Post Content To Custom Archive
function redirect_to_custom_archive() {
    if( is_singular( 'memorial-wall' ) ) {
        wp_redirect( home_url( '/memorial-wall/' ), 301 );
        exit();
    }
}
add_action( 'template_redirect', 'redirect_to_custom_archive' );