[英]Set custom pages instead of category/archive pages
我正在嘗試設置自定義頁面而不是 Wordpress 的默認類別/存檔頁面。
我已將以下腳本復制到主題 function.php 中,我提到該站點使用 The7 Theme。
function loadPageFirst() {
// get the actual category
$actualCategory = get_category( get_query_var('cat') );
// get the page with the same slug
$matchingPage = get_page_by_path( $actualCategory->slug );
// If no match, load the normal listing template and exit (edit if you are using a custom listing template, eg. category.php)
if (!$matchingPage) {
include( get_template_directory() . '/archive.php');
die();
}
// Make a new query with the page's ID and load the page template
query_posts( 'page_id=' . $matchingPage->ID );
include( get_template_directory() . '/page.php');
die();
}
add_filter( 'category_template', 'loadPageFirst' );
我從這里拿了它,這是 becergazda 的解決方案。
現在,在腳本之后,如果我創建一個與類別頁面具有相同 url 的頁面,它會自動替換它。
問題是腳本僅限於主(父)類別。
我想創建一個子頁面(假設 example.com/cars/european/german)自動替換相同的子類別頁面。
我的問題是如何修改腳本以包含類別子項。
試試這個:
function loadPageFirst() {
// get the actual category
$actualCategory = get_category( get_query_var('cat') );
// get the page with the same slug
$matchingPage = get_page_by_path( $actualCategory->slug );
// If no match, load the normal listing template and exit (edit if you are using a custom listing template, eg. category.php)
if (!$matchingPage) {
include( get_template_directory() . '/archive.php');
die();
}
// Make a new query with the page's ID and load the page template
global $post; $post->ID = $matchingPage->ID;
query_posts( 'page_id=' . $matchingPage->ID );
include( get_template_directory() . '/page.php');
die();
}
add_filter( 'category_template', 'loadPageFirst' );
您可以使用模板輕松地做到這一點。 使用子主題並創建一個名為 category-$slug.php 的模板,並將 $slug 替換為您要為其創建頁面的 slug。 你也可以只制作一個category.php。 有關更多選項,請檢查此模板層次結構。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.