WordPress无法在single.php页面上显示自定义帖子类型

问题描述:

我正在尝试创建一个名为products的WordPress自定义帖子类型。在我的主题function.php文件中,我编写了代码(请参阅下面的代码)。在WordPress管理员中,我可以看到一个名为Products的新菜单。我甚至创建了一个新产品,写下它的标题和内容并保存。WordPress无法在single.php页面上显示自定义帖子类型

到目前为止,一切都很好。我甚至可以在首页看到新的“产品”文章。

但是,当我点击它时,WordPress无法找到它并显示404 not found错误页面。但是对于一个WordPress正常的post类型,它不会像那样发生,只有发生这种情况时才会使用自定义帖子类型。我在谷歌搜索了很多,并遵循他们的指示,但他们都没有解决我的问题。

enter image description here

请帮助。下面是我的代码:

<?php 
 
add_action('init', 'product_register'); 
 
function product_register() { 
 
\t $labels = array(
 
\t \t 'name'    => _x('Products', 'post type general name', 'your-plugin-textdomain'), 
 
\t \t 'singular_name'  => _x('Product', 'post type singular name', 'your-plugin-textdomain'), 
 
\t \t 'menu_name'   => _x('Products', 'admin menu', 'your-plugin-textdomain'), 
 
\t \t 'name_admin_bar'  => _x('Product', 'add new on admin bar', 'your-plugin-textdomain'), 
 
\t \t 'add_new'   => _x('Add New', 'product', 'your-plugin-textdomain'), 
 
\t \t 'add_new_item'  => __('Add New Product', 'your-plugin-textdomain'), 
 
\t \t 'new_item'   => __('New Product', 'your-plugin-textdomain'), 
 
\t \t 'edit_item'   => __('Edit Product', 'your-plugin-textdomain'), 
 
\t \t 'view_item'   => __('View Product', 'your-plugin-textdomain'), 
 
\t \t 'all_items'   => __('All Products', 'your-plugin-textdomain'), 
 
\t \t 'search_items'  => __('Search Products', 'your-plugin-textdomain'), 
 
\t \t 'parent_item_colon' => __('Parent Products:', 'your-plugin-textdomain'), 
 
\t \t 'not_found'   => __('No products found.', 'your-plugin-textdomain'), 
 
\t \t 'not_found_in_trash' => __('No products found in Trash.', 'your-plugin-textdomain') 
 
\t); 
 

 
\t $args = array(
 
\t \t 'labels'    => $labels, 
 
\t \t 'public'    => true, 
 
\t \t 'publicly_queryable' => true, 
 
\t \t 'show_ui'   => true, 
 
\t \t 'show_in_menu'  => true, 
 
\t \t 'query_var'   => true, 
 
\t \t 'rewrite'   => array('slug' => 'product'), 
 
\t \t 'capability_type' => 'post', 
 
\t \t 'has_archive'  => true, 
 
\t \t 'hierarchical'  => false, 
 
\t \t 'menu_position'  => null, 
 
\t \t 'supports'   => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments') 
 
\t); 
 
    
 
    register_post_type('product' , $args); 
 
\t flush_rewrite_rules(); 
 
} 
 
add_filter('pre_get_posts', 'my_get_posts'); 
 
function my_get_posts($query) { 
 
\t if ((is_home() && $query->is_main_query()) || is_feed()) 
 
\t $query->set('post_type', array('post', 'product')); 
 

 
\t return $query; 
 
} 
 
?>

我的代码主要是基于帮助下this link

+0

您是否已经在模板目录中生成文件'single-product.php'? – 2014-12-13 04:23:02

+0

删除'flush_rewrite_rules();',这是一个非常昂贵的函数,只能在主题激活或插件激活时运行一次。在删除之后,手动刷新您的固定链接 – 2014-12-13 04:23:33

+0

此外,此行if((is_home()&& $ query-> is_main_query())|| is_feed())'if(($ query-> is_home )&& $ query-> is_main_query())|| $ query-> is_feed())' – 2014-12-13 04:24:27

谢谢大家。现在我的自定义帖子类型正在工作。

我的错误是,我使用的条件,包括被称为products.php自定义职位类型的源代码文件到functions.php文件:

if (is_admin()) { 
     include_once('products.php'); 
} 

随后,一个偶然的机会,我删除了条件,只是用:

include_once('products.php'); 

现在它工作正常。感谢大家的提示和帮助。他们对我非常有帮助。