【问题标题】:Show the archive of all the posts but not the single.php显示所有帖子的存档,但不显示 single.php
【发布时间】:2021-04-05 06:20:26
【问题描述】:

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

我为一位专门从事安乐死的兽医创建了一个自定义 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' );

【问题讨论】:

    标签: wordpress redirect archive


    【解决方案1】:

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

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

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

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


    了解详情

    【讨论】:

    • 太棒了,非常感谢。我现在走在正确的道路上,除了我无法工作的重定向之外,我已经解决了所有问题。
    • 很高兴我能帮上忙,我也让你投票赞成接受的答案。如果您无法弄清楚,您可以就重定向提出一个新问题。 @DanG
    • 您可以将带有条件语句的重定向粘贴到您的 header.php... 在任何情况下,您都应该打开一个新问题,因为您之前的问题已得到回答。
    • 在上面的编辑器中查看我的函数 php 代码。 PHP=single-memorial-wall.php |存档-纪念-wall.php。如果 'publicly_queryable' => false,则存档消失,因此必须为 true。但是,如果我输入单个帖子 slug .....localhost:8888/ttsgb/memorial-wall/molly ....我仍然会在单个帖子上结束。当我宁愿被重定向回:localhost:8888/ttsgb/memorial-wall
    • 如果您只设置has_archive => false 并使用自定义查询创建自定义页面呢?
    【解决方案2】:

    带有 301 重定向的 CPT 工作代码(如果单个帖子 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' );
    

    【讨论】:

      【解决方案3】:

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

      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;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-02
        • 1970-01-01
        • 2017-09-06
        • 2020-03-21
        • 2012-07-29
        • 1970-01-01
        相关资源
        最近更新 更多