WordPress.org

Ready to get started?Download WordPress

Forums

How to display different logos according to post category (10 posts)

  1. andreadirocco
    Member
    Posted 1 year ago #

    Dear community

    I am trying to have the headers of my wp blog display a different logo according to a post category. I managed to write the code that allows each category page to display its own logo, but all my posts are always displaying the fallback logo instead of the category one.

    Link to the blog: http://bit.ly/Xa5dkN

    I have many categories on this blog and this is the code I wrote to have the different logos displayed on the category pages:

    <?php
    if ( is_home() ) :
      get_header('home');
    elseif ( is_category('ambiente') ) :
      get_header('ambiente');
    elseif ( is_category('green-news') ) :
      get_header('ambiente');
    elseif ( is_category('clima') ) :
      get_header('ambiente');
    elseif ( is_category('natura') ) :
      get_header('ambiente');
    elseif ( is_category('energie-rinnovabili') ) :
      get_header('ambiente');
    elseif ( is_category('vivere-a-basso-impatto') ) :
      get_header('ambiente');
    elseif ( is_category('inquinamento') ) :
      get_header('ambiente');
    elseif ( is_category('animali') ) :
      get_header('animali');
    elseif ( is_category('cani') ) :
      get_header('animali');
    elseif ( is_category('gatti') ) :
      get_header('animali');
    elseif ( is_category('sport') ) :
      get_header('sport');
    elseif ( is_category('calcio') ) :
      get_header('sport');
    elseif ( is_category('formula-uno') ) :
      get_header('sport');
    elseif ( is_category('salute') ) :
      get_header('salute');
    elseif ( is_category('alimentazione') ) :
      get_header('salute');
    elseif ( is_category('psicologia') ) :
      get_header('salute');
    elseif ( is_category('fitness') ) :
      get_header('salute');
    elseif ( is_category('sessualita') ) :
      get_header('salute');
    elseif ( is_category('medicina') ) :
      get_header('salute');
    elseif ( is_category('attualita') ) :
      get_header('attualita');
    elseif ( is_category('rassegna-stampa') ) :
      get_header('attualita');
    elseif ( is_category('cronaca') ) :
      get_header('attualita');
    elseif ( is_category('politica') ) :
      get_header('attualita');
    elseif ( is_category('scienza-e-tecnologia') ) :
      get_header('attualita');
    elseif ( is_category('videogiochi') ) :
      get_header('videogiochi');
    else :
      get_header('home');
    endif;
    ?>

    What is the right code to make my posts display the right logo according to its respective category?

    Thank you very much for your help

  2. andrewmills
    Member
    Posted 1 year ago #

    I take it your theme has correctly named header files for each category? (i.e. header-home.php, header-ambiente.php, etc.)

  3. andreadirocco
    Member
    Posted 1 year ago #

    I created those Andrew. And now the category pages correctly display their respective logos. Yet post pages of those categories display only the fallback ( else ) version.

    I need every post to display the logo of it's respective category as well. I hope it's clear.

    Thank you

  4. alchymyth
    Forum Moderator
    Posted 1 year ago #

    for posts, try to use:

    is_single() && in_category()

    and add it with a logic OR to the if's;

    example:

    elseif ( is_category('animali') || is_single() && in_category('animali') ) :
      get_header('animali');
  5. andreadirocco
    Member
    Posted 1 year ago #

    Hi alchymyth thank you. I tried it but it didnt work.

    Let me add the whole code with the link to a post in the animali category.

    <?php
    if ( is_home() ) :
      get_header('home');
    elseif ( is_category('ambiente') ) :
      get_header('ambiente');
    elseif ( is_category('green-news') ) :
      get_header('ambiente');
    elseif ( is_category('clima') ) :
      get_header('ambiente');
    elseif ( is_category('natura') ) :
      get_header('ambiente');
    elseif ( is_category('energie-rinnovabili') ) :
      get_header('ambiente');
    elseif ( is_category('vivere-a-basso-impatto') ) :
      get_header('ambiente');
    elseif ( is_category('inquinamento') ) :
      get_header('ambiente');
    elseif ( is_category('animali') ) :
      get_header('animali');
    elseif ( is_category('cani') ) :
      get_header('animali');
    elseif ( is_category('gatti') ) :
      get_header('animali');
    elseif ( is_category('animali') || is_single() && in_category('animali') ) :
      get_header('animali');
    elseif ( is_category('sport') ) :
      get_header('sport');
    elseif ( is_category('calcio') ) :
      get_header('sport');
    elseif ( is_category('formula-uno') ) :
      get_header('sport');
    
    elseif ( is_category('salute') ) :
      get_header('salute');
    elseif ( is_category('alimentazione') ) :
      get_header('salute');
    elseif ( is_category('psicologia') ) :
      get_header('salute');
    elseif ( is_category('fitness') ) :
      get_header('salute');
    elseif ( is_category('sessualita') ) :
      get_header('salute');
    elseif ( is_category('medicina') ) :
      get_header('salute');
    elseif ( is_category('attualita') ) :
      get_header('attualita');
    elseif ( is_category('rassegna-stampa') ) :
      get_header('attualita');
    elseif ( is_category('cronaca') ) :
      get_header('attualita');
    elseif ( is_category('politica') ) :
      get_header('attualita');
    elseif ( is_category('scienza-e-tecnologia') ) :
      get_header('attualita');
    elseif ( is_category('videogiochi') ) :
      get_header('videogiochi');
    else :
      get_header('home');
    endif;
    ?>

    link: http://bit.ly/XBBSz5

    Thank you for your support

    Andrea

  6. andreadirocco
    Member
    Posted 1 year ago #

    Oh actually I just had an idea that fixed the issue. I love it when this happens. This is the actual line the worked in my case:

    elseif ( is_single() && in_category('cani') ) :
      get_header('animali');

    Now I will duplicate and edit this line for each category of the blog.

    Thanks a lot!

  7. andreadirocco
    Member
    Posted 1 year ago #

    New problem came up related to the original issue.
    Im using Dynamic Widgets to display different widgets according to the category of the blog. Since I edited the header.php file the dynamic widget stopped functioning.

    Any takes on this one? Your help would be much appreciated.

    Thank you

  8. alchymyth
    Forum Moderator
    Posted 1 year ago #

    Since I edited the header.php file the dynamic widget stopped functioning.

    what is the effect?
    i.e. what exactly is different from what you intended?
    which widget is showing or not showing in which category or post?

    do all your header files contain the came code (apart from the logo section) ?

  9. andreadirocco
    Member
    Posted 1 year ago #

    Basically the plugin Dyanmic Widget is not functioning properly anymore.
    If I select show on checked and then say I want Picture with Cat on the category dedicated to Cats, the image doesnt show. The plugin is not working anymore, while before it did.

    I dont know what to do.... maybe I should try another plugin to get different sidebars. Any reccos?

    Thank you

  10. andrewmills
    Member
    Posted 1 year ago #

    Without seeing the before and after of your header.php, I can only offer a guess-- but since plugins frequently need hooks in the header to successfully do things, I'd speculate that when you edited the header, you might have accidentally disabled a hook.

    Any chance we might be able to see your header.php file?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags