最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

permalinks - Force wordpress to display page with same url structure as categories

programmeradmin1浏览0评论

I'm struggling with Wordpress url structure...

  • My page for posts is domain/resources
  • My category url structure is domain/resources/[cat-slug] (I changed the category base in the WP permalink structure)
  • My post url structure is domain/[cat-slug]/[article-slug]

So far, so good.

I want to create sub-pages under my /resources/ page, but it does not works : it create 404 response.
I think WP is looking for a category when I request /resources/url-of-my-child-page, and it cannot display my page

One solution could be to use something like that :

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

as it's explained here but it's not very neat IMO...

Do you have any idea of how to display a page if it exist instead of a category ?

发布评论

评论列表(0)

  1. 暂无评论