我一直在尝试用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);
非常感谢有关修复此问题的任何帮助。
答案 0 :(得分: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>
另见相关:
答案 1 :(得分:0)
你的问题有点不清楚。但是,如果您的主要问题是在这个单页中呈现的所有单个页面都标题为“HOME”,则应该这样做。
尝试更改此内容:
<h2 class="section-heading">{{ wp.the_title }}</h2>
到此:
<h2 class="section-heading">{{ wp.get_post_meta(wp.get_the_ID() , 'title', true) }}</h2>
如果它适用于副标题,它也适用于标题。
答案 2 :(得分:0)
如果您尝试在WordPress主题中使用Twig,我强烈建议您安装一个名为Timber的插件。它为您处理了许多复杂的WordPress特定集成。他们有一个入门主题,您可以查看以更好地了解如何将您的主题放在一起:https://github.com/timber/starter-theme
他们还提供了全面的文档,您可以在此处找到:https://github.com/jarednova/timber/wiki