A client of mine has ads on his website, which are managed by a CPT. The ads can be pulled in via a widget, hardcoded query and a shortcode. I have an issue that isn't consistent that I'm trying to debug. Sometimes ads show up below the footer and other times they don't (and they shouldn't). The website is responsive and has different ad units for all 3 breakpoints. The site first goes through CloudFlare and then is cached using W3TC. I'm wondering if anyone has seen this or may have a thought about why this is happening.
Thanks!
//[ad id=""]
function insert_ad( $atts ) {
$ad = shortcode_atts( array(
'id' => '',
'align' => ''
), $atts );
if($ad['align'] == '' || $ad['align'] == 'left'){
$align = 'alignLeft';
} elseif($ad['align'] == 'middle' || $ad['align'] == 'center'){
$align = 'alignCenter';
} elseif($ad['align'] == 'right'){
$align = 'alignRight';
}
global $post;
$args = array( 'post_type' => 'cbd_ads', 'p' => $ad['id'] );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
if(!wp_is_mobile() && get_post_status() == 'publish'){
return '<div class="'. $align .'">' . get_the_content() . '</div>';
} else {
return '';
}
endwhile; wp_reset_query();
}
add_shortcode( 'ad', 'insert_ad' );
wp_reset_postdata()
withWP_Query
,wp_reset_query()
is used withquery_posts
which you should never use