要為指定ID的分類使用不同的搜索模板,其實很簡單,通過以下方法,就可以實現為wordpress指定ID的分類添加不同的搜索結果頁模板,具體操作步驟如下:
1、在你的WordPress主題目錄下,找到search.php文件。如果沒有這個文件,你可以創建一個。
2、在search.php文件中添加以下代碼:
<?php
$current_category = get_queried_object();
$wodepress_category_id = 666; // Replace 666 with the category ID you want to specify
if ($current_category->term_id == $wodepress_category_id) {
// wodepress.com If the current classification ID matches the specified ID, use a custom search template
get_template_part('template-parts/custom-search-template');
} else {
// If the current classification ID does not match the specified ID, use the default search template
get_template_part('template-parts/default-search-template');
}
?>
3、在你的WordPress主題目錄下的template-parts文件夾中,創建兩個新文件:custom-search-template.php和default-search-template.php。
4、在custom-search-template.php文件中,編寫你想要為指定分類ID使用的自定義搜索模板代碼。
5、在default-search-template.php文件中,編寫你想要為其他分類ID使用的默認搜索模板代碼。
6、保存所有更改,然后在WordPress后臺查看搜索結果頁面,看看是否按照預期顯示了不同的搜索模板。
注意:請確保將666替換為你要指定的分類ID。