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

时间:2019-07-11 17:02:32

标签: php wordpress

我似乎无法从文档中找出答案。

我有一个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-admin是什么意思。

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

2 个答案:

答案 0 :(得分:0)

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

您显然需要根据Template Hierarchy创建和归档CPT,并将其归档-news.php

答案 1 :(得分:0)

只需添加此代码即可检查帖子类型是否为新闻,然后渲染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/

中创建模板的方式

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