WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Need to add sponsor banner before posts on Category archive page SOS (21 posts)

  1. DeveWeb
    Member
    Posted 2 years ago #

    hi guys, i need help, i trie ti Added this code in my theme in fuctions.php

    function sponsor_banner($post) {
      static $already_run = false;
      if (is_category('uncategorized')) {
        if ( !$already_run) {
    ?>
    <img src="/path/to/banner.jpg">
    <?php
          $already_run = true;
        }
      }
    }
    add_action('the_post', 'sponsor_banner');

    but when I try to add a new o edit some category, I do not see any changes or add something extra to the sponsor code, I use the Arras theme, wodpress 3.2.1 spanish version, Arras will be the theme difficult to add this code?.. Please guys i need help to solvent this.! SOS

  2. Mike Little
    Member
    Posted 2 years ago #

    I've just tried this on a test site with twentyten and it works fine:

    Before the first post in category 'uncategorised', it outputs an image. That's it. It doesn't do anything else.

    Are you perhaps expecting something else?

  3. DeveWeb
    Member
    Posted 2 years ago #

    thanks a lot Mike for you answer but i dont see this image, i trie to add this code in twintyten theme for test the code but i dont see any image, in what line number in specific put the code? can you explain me step by step Please..!! :(

  4. Mike Little
    Member
    Posted 2 years ago #

    Just to clarify further, this code will display the banner image before the first post on the category archive page for the specififed category.

    So you won't see it on your home page, or on a single post page. Only on the archive page for the specific category.

    With that knowledge, the code just needs to be copied into functions.php. At the end will do, just before the closing ?> tag (if there is one - it's ok if there isn't).

    You need to change the name of the category 'your-category-here' and the path to the banner image '/path/to/your-banner-here.jpg' and presumably make it into a link to the sponsor or wherever.

    function sponsor_banner($post) {
      static $already_run = false;
      if (is_category('your-category-here')) {
        if ( !$already_run) {
    ?>
    <img src="/path/to/your-banner-here.jpg">
    <?php
          $already_run = true;
        }
      }
    }
    add_action('the_post', 'sponsor_banner');
  5. DeveWeb
    Member
    Posted 2 years ago #

    ready i solvent the code

    function sponsor_banner($post) {
      static $already_run = false;
      if (is_category('sociedad')) {
        if ( !$already_run) {
    ?>
    <a href="http://www.domainsponsor.com/" target="_blank"><img src="/wordpress/wp-content/publicidad/usa.jpg" /></a>
    <?php
          $already_run = true;
        }
      }
    }
    add_action('the_post', 'sponsor_banner');

    thanksssss a lot Mike you are the Best.!! :)

    but when I try to duplicate the code to add a another sponsor to another category gives me an error. how to duplicate the code and I do not throw error?

    // Sponsor 1
    function sponsor_banner($post) {
      static $already_run = false;
      if (is_category('ciencia')) {
        if ( !$already_run) {
    ?>
    <a href="http://www.domain.com/" target="_blank"><img src="/wp-content/publicidad/usa.jpg" /></a>
    <?php
          $already_run = true;
        }
      }
    }
    add_action('the_post', 'sponsor_banner');
    
    // Sponsor 2
    function sponsor_banner($post) {
      static $already_run = false;
      if (is_category('comunidad')) {
        if ( !$already_run) {
    ?>
    <a href="http://www.domain.com/" target="_blank"><img src="/wp-content/publicidad/usa.jpg" /></a>
    <?php
          $already_run = true;
        }
      }
    }
    add_action('the_post', 'sponsor_banner');
  6. Mike Little
    Member
    Posted 2 years ago #

    Try this:

    function sponsor_banner($post) {
      static $already_run = false;
      if ( !$already_run) {
        if (is_category('sociedad')) {
    ?>
    <a href="http://www.domainsponsor.com/" target="_blank"><img src="/wordpress/wp-content/publicidad/usa.jpg" /></a>
    <?php
          $already_run = true;
        } else if (is_category('category2')) {
    ?>
    <a href="http://www.sponsor2.com/" target="_blank"><img src="/wordpress/wp-content/publicidad/image2.jpg" /></a>
    <?php
          $already_run = true;
        }
      }
    }
    add_action('the_post', 'sponsor_banner');
  7. DeveWeb
    Member
    Posted 2 years ago #

    Excellent Mike yeap work perfecttttt.!!! Really Dude thanks a lot you safe my life, you are the best.!!

  8. DeveWeb
    Member
    Posted 2 years ago #

    Mike another question. to use that code but appear after every post, at the bottom, which would have to change the code? thanks my Friend.!!

  9. Mike Little
    Member
    Posted 2 years ago #

    something like this:

    function sponsor_banner_after($post) {
      static $already_run = false;
      if ( !$already_run) {
        if (is_category('category-1')) {
    ?>
    <a href="http://www.sponsor1.com/" target="_blank"><img src="/path/to/image1.jpg" /></a>
    <?php
          $already_run = true;
        } else if (is_category('category-2')) {
    ?>
    <a href="http://www.sponsor2.com/" target="_blank"><img src="/path/to/image2.jpg" /></a>
    <?php
          $already_run = true;
        }
      }
    }
    add_action('loop_end', 'sponsor_banner_after');
  10. DeveWeb
    Member
    Posted 2 years ago #

    wooowww Mike you are the best, work perfect... to complete the website of news, I just need to place a sponsor in the header, it's getting harder .. I might suggest?
    thanks a lot for you big help Bro.!!

    you have any email to send you later the link the my website for you get me your opinion.?

  11. Mike Little
    Member
    Posted 2 years ago #

    There is contact form on my site here http://zed1.com/contact-zed1

  12. DeveWeb
    Member
    Posted 2 years ago #

    okis thanks a lot Mike.. i will contact you about your site.!!

  13. DeveWeb
    Member
    Posted 2 years ago #

  14. ilanm47
    Member
    Posted 2 years ago #

    Hi,

    Saw your thread and I wonder what is wrong with my piece of code. I'm trying to give different header to each category by pasting this in the header.php.

    [please mark your code, following the forum guidelines for posting code]

    <?php if (is_category( 'concepts' )) \{ ?>
    <img src="http://www.portfolio.clickmarketing.co.il/wp-content/uploads/header_concSmall.jpg" width="940" alt="<?php bloginfo('name'); ?> header image" />
    <?php \} elseif (is_category( 'tools' )) \{ ?>
    <img src="http://www.portfolio.clickmarketing.co.il/wp-content/uploads/header_toolsSmall.jpg" width="940" alt="<?php bloginfo('name'); ?> header image" />
    <?php \} ?>

    Please assist.

    Thanks
    Ilan

  15. Mike Little
    Member
    Posted 2 years ago #

    If you really have the backslash before the curly brace "\{", then the code won't work, you should just have curly brace "{".

    If you correct that, then code should work to output an image on the category archive page for category concepts and category tools.

  16. ilanm47
    Member
    Posted 2 years ago #

    Thanks but I don't see any change, Does it appears only on archived posts? I would like it to appear on any post.

    Is it enough just to delete backslash before the question mark or does it appears in other places? <?php if (is_category( 'concepts' )) { ?>

  17. Mike Little
    Member
    Posted 2 years ago #

    The code you showed only displays the image on the specified category archive pages, e.g. http://example.com/category/tools/

    If you want to display a specific header for a single post in a specific category try something like this:

    <?php
    if ( is_single() ) {
      if ( is_object_in_term( $post->ID, 'category', 'category-1' ) ) { ?>
        <img src="http://example.com/wp-content/uploads/category-1-header.jpg" width="940" alt="<?php esc_attr( bloginfo( 'name' ) ); ?> header image" />
    <? } else if ( is_object_in_term( $post->ID, 'category', 'category-2' ) ) { ?>
        <img src="http://example.com/wp-content/uploads/category-2-header.jpg" width="940" alt="<?php esc_attr( bloginfo( 'name' ) ); ?> header image" />
    <? }
    }
    ?>
  18. ilanm47
    Member
    Posted 2 years ago #

    Hi Milke, I have too many holes in my PHP. it shows the same header on all categories. I'm not sure what are the variables (is ID is where I put the post number?) is category-1 is the slug? which line should I duplicate if I have 4 categories (the whole piece or just the first if)?

    Thanks Ilan

  19. Mike Little
    Member
    Posted 2 years ago #

    Hopefully this is a little clearer (and with 4 options):
    Change the 'category-slug-1', etc to your specific category slug.

    Mike

    <?php
    if ( is_single() ) {
      if ( is_object_in_term( $post->ID, 'category', 'category-slug-1' ) ) { ?>
        <img src="http://example.com/wp-content/uploads/category-1-header.jpg" width="940" alt="<?php esc_attr( bloginfo( 'name' ) ); ?> header image" />
    <? } else if ( is_object_in_term( $post->ID, 'category', 'category-slug-2' ) ) { ?>
        <img src="http://example.com/wp-content/uploads/category-2-header.jpg" width="940" alt="<?php esc_attr( bloginfo( 'name' ) ); ?> header image" />
    <? } else if ( is_object_in_term( $post->ID, 'category', 'category-slug-3' ) ) { ?>
        <img src="http://example.com/wp-content/uploads/category-3-header.jpg" width="940" alt="<?php esc_attr( bloginfo( 'name' ) ); ?> header image" />
    <? } else if ( is_object_in_term( $post->ID, 'category', 'category-slug-4' ) ) { ?>
        <img src="http://example.com/wp-content/uploads/category-4-header.jpg" width="940" alt="<?php esc_attr( bloginfo( 'name' ) ); ?> header image" />
    <? }
    }
    ?>
  20. DeveWeb
    Member
    Posted 2 years ago #

    Mike is definitely the best, mike I have solved many things, but I still have some things for which I could not open the web site, by facor need help on this, sorry for posti it here ... this is the post that I open i need help please http://wordpress.org/support/topic/removing-comments-from-node-based-posts-and-change-for-category-sos-please?replies=1#post-2460493
    thanks in advanced..

  21. DeveWeb
    Member
    Posted 2 years ago #

    hey Mike I have a question, there is a similar code to change the font color of the individual menu category??? for example, home#000, category2#111 category3#222 that each category font has a different color?

    Really you are the best..
    thanks in advanced my friend. =)

Topic Closed

This topic has been closed to new replies.

About this Topic