I’ve been trying to render a single page wordpress template with Twig, but so far everything has failed.
{% extends 'layouts/base.twig' %}
{% block content %}
{% for page in pages() %}{{ set_up_page(page) }}
{% include 'content/content-' ~ page.post_name ~ '.twig' %}
{% endfor %}
{% endblock %}
What one of the templates looks like :
<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>
The corresponding functions :
$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);
This brings out the templates but it sets the page title from the template as the title of the home page
Would really appreciate any help on fixing this.
Not sure if I got your question right but quoting a similar issue
So try changing
to
Also, see related:
You question is a little bit unclear. But if your main problem is that all single pages rendered in this single page have the heading “HOME” this should do it.
Try changing this:
to this:
If it works for the subheading it should work for the title too.
If you’re trying to use Twig inside a WordPress theme I would highly recommend installing a plugin called Timber. It takes care of a lot of the complicated WordPress-specific integration for you. They have a starter theme you can check out to get a better idea of how to put your theme together: https://github.com/timber/starter-theme
They also have comprehensive documentation, which you can find here: https://github.com/jarednova/timber/wiki