【问题标题】:Wordpress specific page url doesn't return templateWordpress 特定页面 url 不返回模板
【发布时间】:2018-03-07 12:03:46
【问题描述】:

我有一个 Wordpress 网站,我为每个页面创建了页面和模板文件。 当我打开任何 url 时,它会显示与该 url 关联的模板文件。 除了“博客” url,它似乎未被识别为有效页面并向我显示主页布局。如果我为其他内容重写页面 url 和页面名称并将 functions.php 中的语句调整为新页面的名称,它就可以工作。 好吧,我希望它也适用于“博客”。 可能是什么问题呢?欢迎任何想法。

if (isset($wp->query_vars["pagename"]) && $wp->query_vars["pagename"] == 'blog') {
    $templatefilename = 'layout-blog_layout.php';
    if (file_exists(TEMPLATEPATH . '/' . $templatefilename)) {
        $return_template = TEMPLATEPATH . '/layouts/' . $templatefilename;
    } else {
        $return_template = $plugindir . '/layouts/' . $templatefilename;
    }
    do_theme_redirect($return_template);
}

【问题讨论】:

  • 博客/文章页面是否设置为 WordPress 阅读设置中的静态页面?如果要检查已设置为博客/帖子页面的页面,请使用 $query->is_home()。
  • 首页的静态页面是首页,帖子的静态页面是博客页面。
  • 所以,显然我有一个名为“blog”的注册帖子类型。我需要为帖子显示,例如“/blog/name-of-the-post” 另一方面,我需要将“/blog” url 用作能够显示帖子列表的页面由高级自定义表单添加。唯一的问题是,“/blog” url 被视为 post_type 而不是页面。

标签: wordpress


【解决方案1】:

问题是我有一个 register_post_type 作为“博客”,所以它不能用作页面。没有为该 url 设置 query_vars 中的页面名称,因此无法将其识别为页面。相反,我更改了语句以查看 post_type=blog。

if (isset($wp->query_vars["post_type"]) && !isset($wp->query_vars["blog"]) && $wp->query_vars["post_type"] == 'blog') {
    $templatefilename = 'layout-blog_layout.php';
    if (file_exists(TEMPLATEPATH . '/' . $templatefilename)) {
        $return_template = TEMPLATEPATH . '/layouts/' . $templatefilename;
    } else {
        $return_template = $plugindir . '/layouts/' . $templatefilename;
    }
    do_theme_redirect($return_template);
}

【讨论】:

    猜你喜欢
    • 2017-05-23
    • 2017-08-31
    • 2016-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-21
    • 2018-10-04
    相关资源
    最近更新 更多