个性化阅读
专注于IT技术分析

如何用twig渲染单页WordPress模板

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

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

在此处输入图片说明

非常感谢你提供修复方面的帮助。


#1


不确定我是否正确回答了你的问题, 但引用了类似的问题

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

所以尝试改变

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

to

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

另外, 请参阅相关内容:

  • https://codex.wordpress.org/Function_Reference/single_post_title-使用外部循环
  • https://codex.wordpress.org/Function_Reference/the_title-在内部循环中使用

#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>

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


#3


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

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

赞(0)
未经允许不得转载:srcmini » 如何用twig渲染单页WordPress模板

评论 抢沙发

评论前必须登录!