There are two types of pages I am interested in for WordPress: pages and posts.
At the top of my posts template, I have some code as following to display varying banner ads:
<?php if ( ! is_single() && ! is_search() && ! is_category() && ! is_tag() && my_wp_is_mobile() ) {?>
ad code
<?php } ?>
<?php if ( is_single() && my_wp_is_mobile() ) {?>
ad code
<?php } ?>
<?php if ( ! is_single() && ! is_search() && ! is_category() && ! is_tag() && ! my_wp_is_mobile() ) {?>
ad code
<?php } ?>
<?php if ( is_single() && ! my_wp_is_mobile() ) {?>
ad code
<?php } ?>
At the top of my pages template, I have some code as following to display varying banner ads:
<?php if ( is_page(1302) && ! my_wp_is_mobile() ) {?>
ad code
<?php } ?>
<?php if ( is_page(3651) && ! my_wp_is_mobile() ) {?>
ad code
<?php } ?>
<?php if ( is_page(602) && ! my_wp_is_mobile() ) {?>
ad code
<?php } ?>
<?php if ( ! is_front_page() && ! is_page(array(602,3651,1302)) && ! my_wp_is_mobile() ) {?>
ad code
<?php } ?>
<?php if ( is_front_page() && ! my_wp_is_mobile() ) {?>
ad code
<?php } ?>
<?php if ( is_page(1302) && my_wp_is_mobile() ) {?>
ad code
<?php } ?>
<?php if ( is_page(3651) && my_wp_is_mobile() ) {?>
ad code
<?php } ?>
<?php if ( is_page(602) && my_wp_is_mobile() ) {?>
ad code
<?php } ?>
<?php if ( ! is_front_page() && ! is_page(array(602,3651,1302)) && my_wp_is_mobile() ) {?>
ad code
<?php } ?>
All the “ad codes” are different. I would like to move the banner ad to a different place in my theme and have been trying something like:
<?php
if ( is_single() ) {
get_template_part( 'postads' );
}
?>
OR
<?php
if ( is_single() ) {
require("postads.php");
}
?>
With postads.php being a file in my child theme and containing the relevant banner code and conditions as above.
I’ve been trying both php include and get_template_part to no avail – it simply won’t show up (site loads, no ad code).
Does anyone have any ideas on how to neatly optimise this code? I am sure there is a better way!
Add
global $post;
on top of your postads.php file. The conditional queries are depending on this.