0

I've been playing around with solution to be able to show a different header banner on different categories, and hopefully later on, be able to extend that query to the post level. I've found a few options but they don't seem to be working as I hoped they would.

Here's an example:

              <?php if (in_category('5')) { ?>


<!-- Test_tags_Alex/lifestyle -->
<script type="text/javascript">
  var ord = window.ord || Math.floor(Math.random() * 1e16);
  document.write('<script type="text/javascript" src="http://ad.doubleclick.net/N5081/adj/Test_tags_Alex/lifestyle;sz=728x90;ord=' + ord + '?"><\/script>');
</script>
<noscript>
<a href="http://ad.doubleclick.net/N5081/jump/Test_tags_Alex/lifestyle;sz=728x90;ord=[timestamp]?">
<img src="http://ad.doubleclick.net/N5081/ad/Test_tags_Alex/lifestyle;sz=728x90;ord=[timestamp]?" width="728" height="90" />
</a>
</noscript>

<?php } elseif (in_category('7')) { ?>


<!-- Test_tags_Alex/music -->
<script type="text/javascript">
  var ord = window.ord || Math.floor(Math.random() * 1e16);
  document.write('<script type="text/javascript" src="http://ad.doubleclick.net/N5081/adj/Test_tags_Alex/music;sz=728x90;ord=' + ord + '?"><\/script>');
</script>
<noscript>
<a href="http://ad.doubleclick.net/N5081/jump/Test_tags_Alex/music;sz=728x90;ord=[timestamp]?">
<img src="http://ad.doubleclick.net/N5081/ad/Test_tags_Alex/music;sz=728x90;ord=[timestamp]?" width="728" height="90" />
</a>
</noscript>


<?php } else { ?>


<!-- Test_tags_Alex -->
<script type="text/javascript">
  var ord = window.ord || Math.floor(Math.random() * 1e16);
  document.write('<script type="text/javascript" src="http://ad.doubleclick.net/N5081/adj/Test_tags_Alex;sz=728x90;ord=' + ord + '?"><\/script>');
</script>
<noscript>
<a href="http://ad.doubleclick.net/N5081/jump/Test_tags_Alex;sz=728x90;ord=[timestamp]?">
<img src="http://ad.doubleclick.net/N5081/ad/Test_tags_Alex;sz=728x90;ord=[timestamp]?" width="728" height="90" />
</a>
</noscript>


<?php } ?>

All I get is all 3 banners showing at the same time no matter what category I'm in. I've tried both in_category and is_category but no change :(

Here's a live demo: http://spacenun.com/

Any clues?

Thanks!!

0

You have attempted to paste your PHP into a text widget. and the tags have been transformed.

<div class="textwidget">          <!--?php if (is_category('5')) { ?-->

You can't execute PHP from a text widget by default, and I would not recommend trying to do so (it should be possible). Instead, it is trivial to build your own widget to do this.

Based on that other answer:

class PHP_Widget_wpse_135949 extends WP_Widget {
  function __construct() {
    $opts = array(
      'description' => 'Display Some ads'
    );
    parent::WP_Widget(
      'my-ad-content',
      'Some PHP',
      $opts
    );
  }
  function widget($args,$instance) {
    // Your PHP goes here
    // 
    echo 'PHP generated content';
  }
}
function register_my_widgets() {
  register_widget('PHP_Widget_wpse_135949');
}
add_action('widgets_init','register_my_widgets');
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.