首页 > 解决方案 > 带有自定义分类的自定义帖子类型的单页不起作用

问题描述

我使用自定义分类创建了自定义帖子类型“推荐”,除了单页外,它运行良好,当我单击永久链接时,它显示“未找到”。

这是我的自定义帖子类型/分类法的代码:

function testimonials_custom_post_type()
{
$labels = array(
    'name' => _x('Testimonials', 'Post type general name'),
    'singular_name' => _x('Testimonial', 'Post type singular name'),
    'add_new' => _x('Add new Testimonial', 'Grower'),
    'add_new_item' => __('Add new Testimonial'),
    'edit_item' => __('Edit Testimonial'),
    'new_item' => __('New Testimonial'),
    'all_items' => __('All Testimonials'),
    'view_item' => __('View Testimonial'),
    'search_items' => __('Search Testimonials'),
    'not_found' => __('No testimonials found'),
    'not_found_in_trash' => __('No testimonials found in Trash'),
    'parent_item_colon' => '',
);

$args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => false,
    'query_var' => false,
    'rewrite' => true,
    'capability_type' => 'post',
    'has_archive' => false,
    'hierarchical' => false,
    'exclude_from_search' => false,
    'supports' => array('title', 'editor', 'thumbnail' ),
    'menu_position' => 6,
    'menu_icon'   => 'dashicons-id',
    'taxonomies' => array( 'subjects' ),
);

register_post_type('testimonials', $args);
}

add_action('init', 'testimonials_custom_post_type');

    /*register custom taxonomies for testimonials*/

add_action( 'init', 'create_testimonials_taxonomies', 0 );
function create_testimonials_taxonomies() {
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
    'name'              => _x( 'Subjects', 'subjects', 'textdomain' ),
    'singular_name'     => _x( 'Subject', 'subject', 'textdomain' ),
    'search_items'      => __( 'Search Subject', 'textdomain' ),
    'all_items'         => __( 'All Subjects', 'textdomain' ),
    'edit_item'         => __( 'Edit Subject', 'textdomain' ),
    'update_item'       => __( 'Update Subject', 'textdomain' ),
    'add_new_item'      => __( 'Add new Subject', 'textdomain' ),
    'new_item_name'     => __( 'New Category Subject', 'textdomain' ),
    'menu_name'         => __( 'Subject', 'textdomain' ),
);

$args = array(
    'hierarchical'      => true,
    'labels'            => $labels,
    'show_ui'           => true,
    'show_admin_column' => false,
    'query_var'         => true,
    'rewrite'           => array( 'slug' => 'subjects' ),
);

register_taxonomy( 'subjects', array( 'testimonials' ), $args );
   }
   /*register custom taxonomies for testimonials*/

我尝试使用flush_rewrite_rules( false ); 并尝试在 wp 管理面板中更新永久链接它仍然无法正常工作

对于自定义帖子类型“推荐”,我在我的子主题中创建了 single-testimonials.php

非常感谢您的帮助!

标签: wordpresscustom-post-typepermalinks

解决方案


为了修复我的错误,我必须将“publicly_queryable”设置为“true”,它现在可以工作了))


推荐阅读