Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to render a single page wordpress template with twig

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.

like image 546
Bazinga777 Avatar asked Sep 24 '15 17:09

Bazinga777


1 Answers

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:

  • https://codex.wordpress.org/Function_Reference/single_post_title - use outside loop
  • https://codex.wordpress.org/Function_Reference/the_title - use inside loop
like image 170
sitilge Avatar answered Nov 01 '22 14:11

sitilge