Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 设置自定义页面而不是类别/归档页面_Php_Wordpress_Categories_Custom Pages - Fatal编程技术网

Php 设置自定义页面而不是类别/归档页面

Php 设置自定义页面而不是类别/归档页面,php,wordpress,categories,custom-pages,Php,Wordpress,Categories,Custom Pages,我正在尝试设置自定义页面,而不是Wordpress的默认类别/归档页面 我已经将以下脚本复制到theme function.php中,我提到该站点使用7主题 function loadPageFirst() { // get the actual category $actualCategory = get_category( get_query_var('cat') ); // get the page with the same slug $matchingPage = get_page_b

我正在尝试设置自定义页面,而不是Wordpress的默认类别/归档页面

我已经将以下脚本复制到theme function.php中,我提到该站点使用7主题

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' );
我从bencergazda那里拿到的,这是bencergazda的解决方案

现在,在脚本之后,如果我创建了一个页面,它与类别页面具有相同的url,它会自动替换它

问题是脚本仅限于主(父)类别

我想创建一个子页面(比如example.com/cars/european/german),自动替换相同的子类别页面


我的问题是如何修改脚本以包含类别子项。

您可以通过使用模板轻松做到这一点。使用一个子主题,创建一个名为category-$slug.php的模板,并将$slug替换为要创建页面的slug。您也可以创建category.php。有关更多选项,请选中此项。

尝试使用以下选项:

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' );

谢谢你的回答,我意识到我不清楚,我已经编辑了这个问题。我的想法是,我想创建一个子页面,并让它自动用相同的url替换子类别页面。非常感谢。