I am trying to add a class to the body in a WordPress theme so that when there are no posts on a page it will not show the search bar. I have in my functions.php
right now this to look for one certain page, and if there are no posts.
<?php
add_filter( 'body_class', 'custom_class' );
function custom_class( $classes ) {
if(strpos($_SERVER['REQUEST_URI'], 'agent') !== false ){
$classes[] = 'noSearchBar';
}
if(!have_posts() ){
$classes[] = 'noSearchBar';
}
return $classes;
}
?>
Which works for most of the pages, but on some pages it is using a different template so instead of calling the posts with have_posts
like this:
<?php
global $wp_query;
if ( have_posts() ) :
while ( have_posts() ) : the_post();
get_template_part('template-parts/property-for-listing');
endwhile;
wp_reset_postdata();
else:
?>
<h4><?php esc_html_e('Sorry No Results Found', 'houzez') ?></h4>
<?php
endif;
?>
Its is bringing it up like this:
<?php
global $wp_query, $paged;
if(!$fave_prop_no){
$posts_per_page = 9;
} else {
$posts_per_page = $fave_prop_no;
}
$latest_listing_args = array(
'post_type' => 'property',
'posts_per_page' => $posts_per_page,
'paged' => $paged,
'post_status' => 'publish'
);
$latest_listing_args = apply_filters( 'houzez_property_filter', $latest_listing_args );
$latest_listing_args = houzez_prop_sort ( $latest_listing_args );
$wp_query = new WP_Query( $latest_listing_args );
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) : $wp_query->the_post();
get_template_part('template-parts/property-for-listing');
endwhile;
else:
get_template_part('template-parts/property', 'none');
endif;
?>
Which is making my function not work. There is only 1 line of code in the template-parts/property-none
and its just saying the same thing the other files are. So I'm not sure why the other template would not add the body class.