2014-12-13 37 views
0

我正在嘗試創建一個名爲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

回答

0

謝謝大家。現在我的自定義帖子類型正在工作。

我的錯誤是,我使用的條件,包括被稱爲products.php自定義職位類型的源代碼文件到functions.php文件:

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

隨後,一個偶然的機會,我刪除了條件,只是用:

include_once('products.php'); 

現在它工作正常。感謝大家的提示和幫助。他們對我非常有幫助。

相關問題