• Greetings, everyone. This is my first time posting in the forums. Hope you can help. I’m not at all versed in coding. Here’s what I’m looking to do.

    I have two sidebars on my blog (the left and the right). I’ve set up some posts with a custom field called “noads” (No ads). If a post contains the noads field with a value of 1, I want to display a different right sidebar, one without the ads. Otherwise, I just want the regular sidebar to display.

    Reason: there are a few posts that need to be excluded from showing ads, by request of the ad network.

    My blog URL is https://secondhandkarl.com/

    Thanks, everyone. I’m new to WordPress so I’m trying to learn. Any help will be appreciated.

    Karl

Viewing 4 replies - 1 through 4 (of 4 total)
  • Here is another method:

    Create a No Ads category and assign only the non-ads posts to it. Then use the following in single.php (the No Ads category has an ID of 44 in this example):

    <?php $post = $wp_query->post; ?>
    <?php if (in_category(44)) { ?>
    <?php include (TEMPLATEPATH . '/sidebar2.php'); ?>
    <?php } else { ?>
    <?php include (TEMPLATEPATH . '/sidebar.php'); ?>
    <?php } ?>
    Thread Starter secondhandkarl

    (@secondhandkarl)

    Thank you so much! It works beautifully.

    I am trying to do something similar, except I want only certain posts to display a particular ad. I have only a sidebar.php file. Am I to understand that I should create a new template file called sidebar2.php, place the ad in there, and use the code above (with the correct ID)? Or create as many sidebar#.php files as I need to if I end up having other ads that go only with a particular page?

    Actually, I just tried it and it worked, so no need to answer. Awesome.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom Field used to display different sidebar’ is closed to new replies.