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

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

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

Can someone advise me on the best course of action please.

I've created a custom wordpress theme for a vet specialising in euthanasia, who wants a simple contact sheet style memorial wall to add clients thumbnail images with just a couple of lines of memorial text.

So i've created a wordpress catalogue, (single-memorial.php) which displays the custom posts on (archive-memorial.php). All good so far...

But I only want the ARCHIVE indexed and viewable. Not the single-memorial.php

In the loop I'm not wrapping the post tiles in any the_permalinks. So the thumbnails aren't linking to the single post data, essentially making the single posts complete orphans, unless of course you guessed the slug and directly found them.

However the single posts will be indexable won't they, which I DON'T want and I'd rather not need to style the single-memorial.php un-necessarily - as I don't want anybody to see them.

With such little information, they wouldn't be relevant pages.

So how can I keep the post data public, so the ARCHIVE will work, but not show the single-memorial.php pages?

I was thinking of some kind of generic 301 redirect in the functions file for any of the single posts using single-memorial.php, to the ARCHIVE or HOME??

I could do a .htaccess redirect, but I'm not sure how I'd target the correct posts, as I won't know the url's the client creates he adds new memorials.

Advice most welcome, thanks in advance.

The archive page

// 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

When you register your custom post type (CPT) you can specify a list of arguments.

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.

Using the public, exclude_from_search, publicly_queryable, show_ui, show_in_nav_menus and has_archive arguments, we can built the behaviour you're looking for.

$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...
);

Displaying an archive page require the user to be able query a post type. Untested but should be working.


Learn More