wordpress - 显示所有帖子的存档,但不显示 single.php
问题描述
有人可以建议我最好的行动方案吗?
我为一位专门从事安乐死的兽医创建了一个自定义 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' );
解决方案
当您注册自定义帖子类型 (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 继承的,但每个都不依赖这种关系并控制一个非常具体的意图。默认假。 |
'exclude_from_search' | (bool) 是否从前端搜索结果中排除该帖子类型的帖子。默认值是 $public 的相反值。 |
'publicly_queryable' | (bool) 作为 parse_request() 的一部分,是否可以在前端对 post 类型执行查询。 |
'has_archive' | (bool/string) 是否应该有 post 类型的档案,或者如果是字符串,则使用档案 slug。如果启用了 $rewrite,将生成正确的重写规则。默认假。 |
'show_ui' | (bool) 是否生成并允许在管理员中管理此帖子类型的 UI。默认值为 $public。 |
'show_in_nav_menus' | (布尔/字符串)在管理菜单中显示帖子类型的位置。要工作,$show_ui 必须为真。如果为 true,则帖子类型将显示在其自己的顶级菜单中。如果为 false,则不显示菜单。如果是现有顶级菜单的字符串(例如'tools.php' 或'edit.php?post_type=page'),则帖子类型将作为其子菜单放置。默认值为 $show_ui。 |
使用public
, exclude_from_search
, publicly_queryable
, show_ui
,show_in_nav_menus
和has_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...
);
显示存档页面需要用户能够查询帖子类型。未经测试,但应该可以工作。
学到更多
- 了解更多关于
register_post_type
@https ://developer.wordpress.org/reference/functions/register_post_type/
推荐阅读
- android - 如何使用改造从 REST API 读取 pdf 文件?
- c# - 需要使用c#从prometheus获取数据
- html - HTML:如何删除 div 之间的空格?
- haskell - 冲突教程示例中“纯”关键字的用途是什么?
- azure-data-explorer - 使用一个查询的输出结果作为表名
- kubernetes - Kubernetes - 将负载均衡的公共 IP 作为环境变量传递到 Pod
- tomcat - Tomcat:如何以编程方式在 GlobalNamingResources 中定义数据源资源并将其链接到应用程序
- android - 改变方向后虚线下划线位置改变
- reactjs - Webpack React - CSS 加载器问题
- html - 使用 Spring MVC 的 JSP 中的下拉列表问题