WordPress自定义帖子类型register_post_type删除单个视图但保留存档页面



我似乎无法从文档中弄清楚这一点。

我有一个wordpress网站,其中包含一个名为"新闻"的自定义帖子类型

有 21 个"帖子",其中包含我们刚刚在根级别永久链接/news/上显示的数据

但是,这21个帖子中的每一个都有自己的页面/子域,您可以单击"查看">

这会将您带到一个空白页面,因为我们从未构建过自定义页面来显示该数据......同样,我们只是在/news/上显示数据,并希望保留这些数据。

但是出于SEO目的,我们希望禁用这些页面的实际创建,每个页面都有自己的永久链接。

所以,基本上我希望"/news/the-custom-news-post-that-i-made-example"重定向到404页面......或者根本不重定向......它不应该存在。

任何帮助都非常感谢!

当我将register_post_type - public 设置为 false 或 publicly_queryable 设置为false 时,这并没有给我我想要的结果.. bc 然后我也看不到/news/了。

谢谢 -O

编辑。。

你的意思是编辑这段代码...不确定您的WP管理员是什么意思。

<?php
add_action('init', 'theyard_news_init');
add_action('init', 'create_taxonomy');
function theyard_news_init() {
$labels = array(
'name'               => __('News', 'theyard'),
'singular_name'      => __('News', 'theyard'),
'add_new'            => __('Add New', 'theyard'),
'add_new_item'       => __('Add New news', 'theyard'),
'edit_item'          => __('Edit News', 'theyard'),
'new_item'           => __('New News', 'theyard'),
'all_items'          => __('All News', 'theyard'),
'view_item'          => __('View News', 'theyard'),
'search_items'       => __('Search News', 'theyard'),
'not_found'          => __('No News found', 'theyard'),
'not_found_in_trash' => __('No News found in Trash', 'theyard'),
'parent_item_colon'  => '',
'menu_name'          => __('News', 'theyard')
);
$args   = array(
'labels'             => $labels,
'public'             => true,
'publicly_queryable' => true,
'show_ui'            => true,
'show_in_menu'       => true,
'query_var'          => false,
'capability_type'    => 'page',
'rewrite'            => true,
'has_archive'        => 'news',
'hierarchical'       => false,
'menu_position'      => 21,
'menu_icon'          => 'dashicons-media-text',
'supports'           => array('title', 'thumbnail', 'editor'),
'show_in_nav_menus'  => true,
'show_in_admin_bar'  => true,
'taxonomies'         => array()
);
register_post_type('news', $args);
}

https://wordpress.stackexchange.com/questions/128636/how-to-disable-the-single-view-for-a-custom-post-type

<?php
add_action( 'template_redirect', 'wpse_128636_redirect_post' );
function wpse_128636_redirect_post() {
$queried_post_type = get_query_var('post_type');
if ( is_single() && 'sample_post_type' ==  $queried_post_type ) {
wp_redirect( home_url(), 301 );
exit;
}
}
?>

您显然需要根据CPT的模板层次结构创建和存档新闻.php

只需添加此代码即可检查帖子类型是否为新闻,然后渲染404模板,否则呈现内容模板。

if ( is_single() && get_post_type() == 'news' ) {
get_404_template();
} else {
get_template_part( 'content', get_post_format() );
}

您可以从此链接获取有关页面在WordPress中如何工作的更多信息 - https://developer.wordpress.org/themes/template-files-section/post-template-files/

或者您可以创建一个模板 404,然后将其分配给您的自定义帖子类型,现在它将始终呈现 404 页面,这就是您在 WordPress 中创建模板的方式 https://www.cloudways.com/blog/creating-custom-page-template-in-wordpress/

您还可以为您的新闻帖子类型创建单一新闻.php页面,只需加载 404 页面引用 - https://developer.wordpress.org/themes/template-files-section/custom-post-type-template-files/

最新更新