20
votes

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 pageenter image description here

Would really appreciate any help on fixing this.

3
It doesn't look like you've shared the template code responsible for outputting the title of the page... - rnevius
@rnevius I added one of the templates that is rendered - Bazinga777
Have you tried simply using wp.title? - rnevius
@Bazinga777 could you provide us with (or emphasize) the snippet that holds the code for setting the title? - sitilge
@sitilge The wp.the_title is a proxy for the wordpress the_title method. It's weird that the contents of the pages are being shown properly but not the titles for the same. I am using the following theme framework github.com/zach-adams/sprig - Bazinga777

3 Answers

1
votes

Not sure if I got your question right but quoting a similar issue

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

So try changing

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

to

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

Also, see related:

0
votes

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:

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

to this:

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

If it works for the subheading it should work for the title too.

0
votes

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