php - 如何使用 twig 呈现单页 wordpress 模板

标签 php wordpress twig wordpress-theming

我一直在尝试使用 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);  

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

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

最佳答案

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

On my single post page, I was using the_title instead of single_post_title.

所以尝试改变

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

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

另见相关:

关于php - 如何使用 twig 呈现单页 wordpress 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32767410/

相关文章:

javascript - AMP 脚本警告验证器

PHP 函数重写?

html - CSS 中的 wordpress 3.3 相对路径

Symfony 在单个语句中分配多个变量

php - 防止 symfony 序列化程序到某些属性

php - OpenID 安全性,对此的任何更改

php - 具有不同配置的重复 WP 主题

wordpress - 如何将 URL 参数传递给 Wordpress 页面中的 iFrame?

symfony - 如何根据 Symfony 中登录的用户设置 Twig 默认日期和数字格式?

javascript - 如何禁用对 "twig"的评论?