0

Main Issue: I can't use Wordpress conditional checks (is_page(), is_front_page() and is_page_template()) inside the main loop of my search.php file.

I know the Codex states that is_page() and other conditional checks don't work in the loop. So trying to figure out work arounds.

Objective: I'm using Advanced Custom Fields for all content entered on the homepage and some other templates, not using the default page editor at all. When someone searches, I want each returned item to have the featured image (post thumbnail), title, permalink and descriptive text that has been entered into a ACF field since I can't use the_content() or the_excerpt().

So inside the loop for my search.php, I'm doing some checks to see what post type/page the returned item is, or what template is being used so I can retrieve the appropriate content.

I was able to use 'page' == get_post_type() as a alternative to where I would normally use is_page(). I just can't figure out how to check if its the front page or if it's a page using a specific template. As I mentioned is_front_page() and is_page_template() do NOT work inside the main loop.

I also cannot use a page's name to check, as the page name for home (static front page) could change once the I turn it over to my client and they start creating real content. And obviously the template can be used on multiple pages which I won't know the names of until after the client creates them. I'm looking for a better long term solution that can handle any page created by the client in the future.

While any help is greatly appreciated, all I ask is please do not recommend a plugin for this, I want to accomplish this programmatically.

Thanks in advance!

1 Answer 1

0

Ok, so I still can't figure out the best way to check if the returned post is the static front page (without getting and comparing the url). However, I figured out how to check if a returned item is using a specific page template using get_page_template_slug(). I didn't know that function existed before running into this issue. Luckily my front page is using a custom page template, so don't currently need a work around for is_front_page().

So where I would usually use is_page_template('templates/template-name.php') I was able to check using the following:

$id = get_the_ID();
$template_slug = get_page_template_slug( $id );

if( $template_slug === 'templates/template-name.php' ) :
    do stuff
endif;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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