Brian O'Neill
Brian O'Neill

Reputation: 5185

WordPress: How can I check if a search result is a post of a page?

In my search archive view, some of the results will be posts, and some will be pages. I want to give posts and pages different visual treatments in the template.

I've tried using if (is_post()) and if (is_page()) with no luck. How can I accomplish this?

Upvotes: 0

Views: 930

Answers (1)

Atlas_Gondal
Atlas_Gondal

Reputation: 2552

The is_page() depends on how the request maps to the main query variables, so you could instead check the post type within the loop:

the_title();

$post_type = get_post_type();

if( 'post' === $post_type ) {

    // it is post, and style it like the way you need

} elseif ( 'page' === $post_type ) {

   // it is page

}

Upvotes: 2

Related Questions