自定义帖子类型单页显示索引页


Wordpress: Custom post type single page shows index page instead

我已经尝试过让我的第一个wordpress主题,我有一些麻烦与我的博客单页。[我的博客页][1]

这是一个自定义的帖子类型,我创建了名为blog_post。奇怪的是,这似乎只发生在我的网站的子层上,例如{url}/blog_post/这里的页面看起来就像我的索引页。

但是在我的工作页面上,我的单页没有任何问题,他们的永久链接没有{url}/work_post/附加到他们。[我的工作页][2]

我用functions.php文件创建了我的单个页面的特殊样式

define(SINGLE_PATH, TEMPLATEPATH . '/single');
/**
* Filter the single_template with our custom function
*/
add_filter('single_template', 'my_single_template');
/**
* Single template function which will choose our template
*/
function my_single_template($single) {
global $wp_query, $post;
/**
* Checks for single template by category
* Check by category slug and ID
*/
foreach((array)get_the_category() as $cat) :
if(file_exists(SINGLE_PATH . '/single-cat-' . $cat->slug . '.php'))
return SINGLE_PATH . '/single-cat-' . $cat->slug . '.php';
elseif(file_exists(SINGLE_PATH . '/single-cat-' . $cat->term_id . '.php'))
return SINGLE_PATH . '/single-cat-' . $cat->term_id . '.php';
/*                                                                                                                              <-------- if no single-cat page is found the return the single.php file 
*/
elseif(file_exists(SINGLE_PATH . '/single.php'))
return SINGLE_PATH . '/single.php';
endforeach;
}

我希望有人能理解我的混乱:D

我通过简单地删除functions.php中的添加,然后为我的工作页面制作自定义帖子,找到了自己的答案。

,然后我为每个我想要的鼻涕虫制作一个{鼻涕虫}.php页面大家都很开心

您需要在自定义帖子类型中添加"public => true"。还可以创建单个{custom-post-type}.php文件

代码:

$args = array(
        'labels'            => $labels,
        'public'            => true,
        'show_ui'           => true,
        'show_in_menu'      => true,
        'capability_type'   => 'post',
        'heirachical'       => false,
        'menu_position'     => 26,
        'has_archive'       => true,
        'menu_icon'         => 'dashicons-products',
        'supports'          => array( 'title', 'editor','thumbnail' ),
        'query_var'         => true,
        'capability_type'   => 'post',
        'rewrite'           => true,
    ); 

示例:假设您已经创建了自定义帖子类型,例如"product",那么您需要在主题目录中创建single-product.php文件。

注意:保存您的永久链接设置

对于我来说,将'publicly_queryable' => true添加到注册post_type的参数中是有效的。我最初必须设置set为false