1

I am currently working on a project where we're designing a custom frontpage template for a static front page.

Within the template are three different custom WP_Queryies with relating loops. Within one of the custom loops, I use template-parts chosen by which post type the current post is. In these template parts (meaning within a custom loop), I would like to check if the current page (active page, meaning "frontpage" where the loop lives, not the current active element in the custom loop) is frontpage or search results page to display an icon for identifying different post types.

If my understanding of the descriptions of is_search and is_front_page is correct, these functions checks current element in loop, hence will they do me no good in this particular example.

An illustration is provided to assist the description above.

Illustration: Checking if active page is frontpage or search in custom loops using site-wide template parts

Does anyone have a good idea on how to check if the active page is the front page or the search results page in the custom loop?

  • You can do and save into a variable the check at the beginning of the template_frontpage.php file. Then you can do the check inside the loop. For example: Before all the loops $isFrontPageOrSearch = is_search() || is_front_page() and then inside loop if(isFrontPageOrSearch) – Laxmana Apr 10 '17 at 9:40
  • I like the idea, but the loop doesn't seem to reach the variable (not returning anything when echoed within the custom loop). I tried making the variable global, first in header to preserve scope, later in tpl_frontpage but with same result. Any thoughts on why this would occur? – Marius Akerbæk Apr 10 '17 at 11:15
  • At first I would put an integer or a string value at the variable and print it inside the custom loop. Not need for a global variable. That's because echoing false will print an empty string (nothing visible) and echoing true will print 1. Better to use var_dump($variable) that will print bool(false) if the variable is false. – Laxmana Apr 10 '17 at 11:20
  • Tried both, unfortunately (both assigning the variable an actual integer based on an if-test, and using the boolean values). But as Stig-Ruben points out below, and a current google on get_template_part's scope indicates, we don't get to pass variables in to get_template_part. Thank you for the idea, anyhow :) – Marius Akerbæk Apr 10 '17 at 11:50
  • Sorry I didn't understood that you want to pass a variable to another template / file. You want to do the check inside the template file ?? – Laxmana Apr 10 '17 at 11:58
0

Marius, I have been in this situation before as well. Haven't found a good solution though. Answer provided by Laxmana won't work as the $isFrontPageOrSearch wont be accessible inside the template-part file. Although using the approach provided by Laxmana, you could be using PHP include(locate_template('your-template-name.php'));, instead of using get_template_part function. Then the variable would be accessible.

  • Cheers, Stig-Ruben. I was about to reach the same conclusion based on a google on get_template_part's scope, good to know it works! Then I'll implement this set instead. – Marius Akerbæk Apr 10 '17 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.