Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write the wordpress function "is_front_page()" in twig Timber code?

My goal is to simply output something only on the index page of my wordpress using Twig code. I have set a static page called Home.

I have tried this in my base.twig:

{% if is_front_page %}
 Homepage content
{% endif %}

But this doesn't do anything and I just find can't easily find it for some reason.

Any help is appreciated! Thanks in advance

like image 879
Noob17 Avatar asked Nov 28 '22 09:11

Noob17


2 Answers

Timber comes with the fn (also has alias of function) that let's you execute external PHP functions. So something like this would work:

{% if fn('is_front_page') %}
  Homepage content
{% endif %}
like image 152
Anonymous Avatar answered Dec 05 '22 22:12

Anonymous


I like to keep special functions outside of my twig templates. In Timber you can define your own context where you can set your own variables.

Create a file called front-page.php and add:

<?php

$context = Timber::get_context();

// Set a home page variable
$context['is_front_page'] = 'true';

Timber::render(array('home.twig'), $context);

then you can use is_front_page as a variable just like you wanted:

{% if is_front_page %}
 Homepage content
{% endif %}
like image 27
Jason Rambeck Avatar answered Dec 06 '22 00:12

Jason Rambeck