• Resolved txie

    (@txie)


    Hi!
    I want my website to change an image depending on if it′s in a category, a page, etc.
    My code is:

    <?php
        if (is_front_page() ) {
          $cat = "home";
        } elseif (is_page(array("archives"))) {
          $cat = "archive";
        } elseif (is_page() || is_search()) {
          $cat = "page";
          } elseif (is_category() || is_single()) {
          $cat = get_the_category();
          $cat = $cat[0]->slug;
        }
      ?>
    <div id="wrapper" class="<?= $cat_title ?> <?php if (is_single()) echo 'post' ?>">
        <a href="https://k-i-t.com.ar/"><img src="https://k-i-t.com.ar/wp-content/themes/kit/images/kit-marca-azul-tienda-regalos.png" alt="" width="75" height="41" border="0" style="opacity:0;filter:alpha(opacity=0)"/></a></div>

    CSS:

    #wrapper.home  {
    	position:absolute;
    	width: 75px;
    	height:41px;
    	margin: 55px 0px 0px 0px;
    	background: url(images/kit-marca-azul-tienda-regalos.gif) #FFF;
    	}
    
    #wrapper.archive  {
    	position:absolute;
    	width: 75px;
    	height:41px;
    	margin: 55px 0px 0px 0px;
    	background: url(images/kit-marca-azul-tienda-regalos.gif) #FFF;
    	}
    
    #wrapper.page {
    	position:absolute;
    	width: 75px;
    	height:41px;
    	margin: 55px 0px 0px 0px;
    	background: url(images/kit-marca-verde-tienda-regalos.gif) #FFF;
    
    	}
    
    #wrapper.cuadernos  {
    	position:absolute;
    	width: 75px;
    	height:41px;
    	margin: 55px 0px 0px 0px;
    	background: url(images/kit-marca-azul-tienda-regalos.gif) #FFF;
    
    	}
    
    #wrapper.bienvenida {
    	position:absolute;
    	width: 75px;
    	height:41px;
    	margin: 55px 0px 0px 0px;
    	background: url(images/kit-marca-amarillo-tienda-regalos.gif) #FFF;
    
    	}
    
    #wrapper.regalo {
    	position:absolute;
    	width: 75px;
    	height:41px;
    	margin: 55px 0px 0px 0px;
    	background: url(images/kit-marca-naranja-tienda-regalos.gif) #FFF;
    
    	}
    
    #wrapper.cocina {
    	position:absolute;
    	width: 75px;
    	height:41px;
    	margin: 55px 0px 0px 0px;
    	background: url(images/kit-marca-rosa-tienda-regalos.gif) #FFF;
    
    	}

    If it′s home or a page works fine. But I can′t make it work if it′s category or single. It′s important that I can make the code work automatically, so I don′t have to type every category title or single title.

    Thanks!!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Have you examined the source of your category pages to see what class is assigned?

    A link to your site might help.

    Thread Starter txie

    (@txie)

    Yes, when I write everythimng manually it works, for example:

    <?php
        if (is_front_page() ) {
          $cat_title = "home";
        } elseif (is_page(array("archives"))) {
          $cat_title = "archive";
        } elseif (is_page() || is_search()) {
          $cat_title = "page";
        } elseif (is_category('1') || is_single())  {
          $cat_title = "cuadernos";
        } elseif (is_category('3'))  {
          $cat_title = "cocina";
        } elseif (is_category('4')) {
          $cat_title = "bienvenida";
        } elseif (is_category('5')) {
          $cat_title = "regalo";
        }
      ?>

    But I need it to be automatic.
    My website is: https://k-i-t.com.ar/

    You can see that the code works for pages and for the home:
    https://k-i-t.com.ar/
    https://k-i-t.com.ar/como-comprar-kit

    But not for categories:
    https://k-i-t.com.ar/categoria/cuadernos

    Thanks!

    Your class name on the De Cuadernos page is individuales, like this: <div class="individuales " id="wrapper">

    I suspect that the page belongs to more than one category, and you are just picking the first one. If there is only individuales as a second class, you could pick the following slug:

    if ($cat[0]->slug == 'individuales') {
       $cat = $cat[1]->slug;
    } else {
       $cat = $cat[0]->slug;
    }

    You should probably check that there is a result from get_the_category and provide a default value, also.

    Thread Starter txie

    (@txie)

    Oh… I see… Thanks for this!
    I made it work by adding the subcategories styles, thanks to you!

    You are welcome!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Change an image according to categories, pages, single or home’ is closed to new replies.