問題描述
乾草,我正在玩 WordPress 3.0 和兩個新功能,自定義帖子類型和菜單編輯器。
我已經開始創建一個名為 「products」 的新的帖子類型,就像你猜到的,這個列表的產品。
我使用沼澤標準模板,我正在創建一個新的菜單來替換頂級導航。據我看到,我只能添加一些”products”,我不能添加一個”archive” 的產品。我想要做的是添加一個菜單鏈接去一個列出所有產品的頁面。
有什麼想法怎麼辦?
最佳解決方案
@dotty 你可以看到這個 trac 門票:應該有自定義帖子類型的索引頁面,所以顯然需要在 WordPress 核心尚未解決。
@John P Bloch 和 @Chris_O 都給你很好的選擇; 我要給你一個第三。
“Products” 頁
首先為您的自定義帖子類型創建一個頁面,並將其稱為”Products” 。這將給它以下 URL:
“Products List” 短碼
接下來創建一個可以嵌入到”Products” 頁面中的 Shortcode 。在我的例子中,我稱之為 [product-list]。以下是使用它的屏幕截圖:
請注意,這樣的短代碼將是添加大量可選功能的一個很好的候選者,並使其能夠處理許多不同的後期類型,但為了清晰起見,我幾乎都是硬編碼的一切。你當然可以用它作為你自己的短碼的起點:
<?php
add_shortcode('product-list', 'my_product_list');
function my_product_list($args) {
$save_post = $GLOBALS['post']; // Save state so you can restore later
$post_type = 'product';
$template_file = get_stylesheet_directory() . "/post-{$post_type}.php";
if (!file_exists($template_file)) {
return "<p>Missing template [$template_file].</p>";
} else {
global $post;
$q = new WP_Query("showposts=10&post_type={$post_type}&orderby=title&order=ASC");
$rows = array();
$rows[] = '<div class="post-list ' . $post_type . '-post-list">';
global $post_list_data;
$post_list_data = array();
$post_list_data['post_count'] = $post_count = count($q->posts);
foreach ($q->posts as $post) {
$q->the_post();
ob_start();
include($template_file);
$rows[] = ob_get_clean();
}
$rows[] = '</div>';
$GLOBALS['post'] = $save_post;
return implode("n",$rows);
}
}
post-product.php 主題模板文件
接下來,您將需要創建一個僅顯示一個產品的主題模板文件。實現短代碼的功能命名為模板文件 post-product.php,這裏是一個很好的起點:
<?php
/**
* post-product.php - File to display only one product within a list of products.
*/
?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h2 class="entry-title"><?php the_title(); ?></h2>
<div class="entry-content">
<?php the_content(); ?>
</div>
</div>
添加菜單選項
最後,您需要添加菜單選項。這是非常簡單的,你可以從這個屏幕截圖中看到 (以下假設您之前對 WordPress 3.0 菜單什麼也沒做,而且您使用的是支持 WordPress 3.0 菜單的主題,例如 Twenty Ten):
-
在管理菜單中選擇菜單選項。
-
單擊”+” 添加一個新菜單。
-
輸入你的菜單名稱,無論你喜歡什麼。
-
點擊”Create Menu” 按鈕 (屏幕截圖顯示”Save Menu”,但添加時它將是”Create Menu” 。)
-
選擇您的新菜單作為您的”Primary Navigation” 。
-
選擇您的”Products” 頁面。
-
點擊 「添加到菜單」
-
點擊”Save Menu”
最後,輸出
以下是一個基本的產品列表:
次佳解決方案
這不是 WordPress 的本機支持。但是,您可以將其添加到您的 functions.php 文件中,並且它將工作:
function give_me_a_list_of_products(){
add_rewrite_rule( 'products/?$', 'index.php?post_type=products', 'top' );
}
add_action( 'init', 'give_me_a_list_of_products' );
這將給你 example.com/products/作為產品列表。從那裏,您只需添加一個自定義鏈接到您的菜單。
但是,如果您希望使用 Feed 進行真正歸檔 (按月,年等),則需要更詳細的代碼。如果你的’products’ 是一個 non-hierarchical 的帖子類型 (好像應該是),你可以使用我的插件:
http://www.wordpress.org/extend/plugins/custom-post-permalinks/
這給您額外的領域來定製您的永久鏈接 (像您可以使用博客帖子),並將為您提供根據類別,作者,月,年,職位類別等自定義固定鏈接的能力。
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。


