【问题标题】:how to render a single page wordpress template with twig如何使用 twig 渲染单页 wordpress 模板
【发布时间】:2015-12-22 09:56:39
【问题描述】:

我一直在尝试使用 Twig 呈现单页 wordpress 模板,但到目前为止一切都失败了。

{% extends 'layouts/base.twig' %}

{% block content %} 
    {% for page in pages() %}{{ set_up_page(page) }}                                
        {% include 'content/content-' ~ page.post_name ~ '.twig' %}
    {% endfor %}
{% endblock %}

其中一个模板是什么样子的:

<section id="about" {{ wp.post_class }}>
    <div class="container">
        <div class="row">
            <div class="col-lg-12 text-center">
                <h2 class="section-heading">{{ wp.the_title }}</h2>
                <h3 class="section-subheading text-muted">{{ wp.get_post_meta(wp.get_the_ID() , 'st_page_subtitle', true)  }}</h3> <!-- To be Changed to subtext for title  -->
            </div>
        </div>
        <div class="row">
            <div class="col-lg-12">
             {{ wp.the_content }}
            </div>
        </div>
    </div>

对应的功能:

        $wpgetpages = new Twig_SimpleFunction("pages", function() { 

                $currentID = get_the_ID();

                $menu_order = wp_get_nav_menu_items('header');

                $menu_items = array();

                foreach($menu_order as $item) {

                    $menu_items[] = $item->ID;
                }

                $args = array('post_type' => 'page',
                              'status' => 'publish',                                  
                              'exclude'=> $currentID,
                              'orderby' => 'menu_order',                                  
                              'order' => 'ASC'
                           );

                $pages = get_posts($args);

                return $pages;

        });     

        $wpsetpages = new Twig_SimpleFunction("set_up_page", function($arg) {   

                setup_postdata($arg);                       

        });

        self::$twig_environment->addFunction($wpposts);
        self::$twig_environment->addFunction($get_theme_options);
        self::$twig_environment->addFunction($wppostdata);          
        self::$twig_environment->addFunction($wpgetpages);
        self::$twig_environment->addFunction($wpsetpages);  

这会调出模板,但会将模板中的页面标题设置为主页的标题

非常感谢任何有关解决此问题的帮助。

【问题讨论】:

  • 你好像没有分享负责输出页面标题的模板代码...
  • @rnevius 我添加了一个被渲染的模板
  • 你试过简单地使用wp.title吗?
  • @Bazinga777 能否提供(或强调)保存标题设置代码的 sn-p?
  • @sitilge wp.the_title 是 wordpress the_title 方法的代理。页面内容正确显示,但标题却没有显示,这很奇怪。我正在使用以下主题框架github.com/zach-adams/sprig

标签: php wordpress twig wordpress-theming


【解决方案1】:

不确定我的问题是否正确,但引用 a similar issue

在我的单个帖子页面上,我使用的是 the_title 而不是 single_post_title。

所以尝试改变

<h2 class="section-heading">{{ wp.the_title }}</h2>

<h2 class="section-heading">{{ wp.single_post_title }}</h2>

另见相关:

【讨论】:

    【解决方案2】:

    你的问题有点不清楚。但是,如果您的主要问题是在此单个页面中呈现的所有单个页面都有标题“HOME”,那么应该这样做。

    尝试改变这个:

    <h2 class="section-heading">{{ wp.the_title }}</h2>
    

    到这里:

    <h2 class="section-heading">{{ wp.get_post_meta(wp.get_the_ID() , 'title', true) }}</h2>
    

    如果它适用于副标题,它也应该适用于标题。

    【讨论】:

    • 这正是我的问题,但我已经完成了 get_the_ID() 并且它返回当前页面的 ID,即主页。这让我很困惑。
    • 您使用的是什么 WP 版本?我会尝试在我的开发框中重新创建错误。
    • 我在 4.3.1,这是主题本身的链接dropbox.com/s/6wxxkee7k6v8h40/sprig.zip?dl=0
    【解决方案3】:

    如果您尝试在 WordPress 主题中使用 Twig,我强烈建议您安装一个名为 Timber 的插件。它为您处理了许多复杂的 WordPress 特定集成。他们有一个入门主题,您可以查看以更好地了解如何将您的主题组合在一起:https://github.com/timber/starter-theme

    他们也有全面的文档,你可以在这里找到:https://github.com/jarednova/timber/wiki

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多