1

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!

2
  • You have a question which is almost 4 years old, and you have never taken the time to leave any kind of feedback. If an answer helped to solve your issue, accept that answer by clicking on the checkmark next to it, or post your own solution and accept it Commented Feb 12, 2015 at 11:06
  • Apologies I thought the site chose for me. I have fixed this. Commented Feb 12, 2015 at 19:06

1 Answer 1

1

Add global $post; on top of your postads.php file. The conditional queries are depending on this.

<?php
  global $post;

  // your add codes

?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.