0

Hi,

I'm working on a wordpress website where every product page has its own top banner that appears on the page using php. This works fine on the products but when I try to have a different banner on the category page it shows the banner that was the first product banner I added to the category see below ID 78.

Can anyone help?

Here is my code for the three products in the same category, this works fine. (please note I'm a PHP newbe)

<?php
    if(get_the_ID()==78) {
        echo do_shortcode('[smartslider2 slider="11"]');
        }
?>

<?php
    if(get_the_ID()==82) {
        echo do_shortcode('[smartslider2 slider="12"]');
        }
?>

<?php
    if(get_the_ID()==85) {
        echo do_shortcode('[smartslider2 slider="13"]');
        }
?>

This is the code for the category banner

<?php
if(is_category(10)) {
  echo do_shortcode('[smartslider2 slider="14"]');
    }
?>

Any ideas what the problem is?

4
Contributors
5
Replies
29
Views
2 Years
Discussion Span
Last Post by bubbafunk1
0

I'd put those strings into an array, more efficient doing a simple array lookup than a bunch of IF statements.

echo do_shortcode(codes[get_the_ID()]);

My guess is that you're getting that banner because the ID function is returning 78. Unless you have an error somewhere in your do_shortcode function.

0

Does the is_category(10) function return true? Test it this way:

if(is_category(10)) {
    echo do_shortcode('[smartslider2 slider="14"]');
} else {
    echo "Error detecting the category page";
}

Now, if it returns the above error message post the is_category() function code.

Edited by broj1

0

Thank you both for getting back to me so quickly.

I dont think there's any error in the short code as it works for all 28 banners on the individual product pages, it just fails to work on the main category pages. I would not know where to start with using arrays. There are 28 different products all with different banners and there are 5 categoeries all with different banners. At the moment I have 33 separate bits of code for each product banner, which is ugly I know but i dont know of any other way to do it.

I tried the test and there it showed "Error detecting the category page". So I tried this code:-

<?php
    if(is_product_category()=="powders") {
        echo do_shortcode('[smartslider2 slider="14"]');
    }
?>

which worked but it still showed the banner from this product ID78 above it, so there were two banners on a page. I dont understand why its even showing as it should only show on page with an id of 78 , surely.

<?php
    if(get_the_ID()==78) {
        echo do_shortcode('[smartslider2 slider="11"]');
        }
?>

What I think is needed is a way to stop ID78 from working when the category code is working. Any ideas how to do that?

1

I think you should have the category check first, but add that there is no ID set. In the else you can check the IDs.

if (is_product_category() == "powders" and get_the_ID() == -1) // or whatever it returns for a category
{
    echo do_shortcode('[smartslider2 slider="14"]');
}
else
{
    switch (get_the_ID()) 
    {
        case 78:
            echo do_shortcode('[smartslider2 slider="11"]');
            break;

        case 82:
            echo do_shortcode('[smartslider2 slider="12"]');
            break;
    }
}

If you want the array as suggested by Phaelax, do something like this:

$map = array();
$map[78] = 11;
$map[82] = 12;

$id = get_the_ID();
if (isset($map[$id]))
{
    echo do_shortcode("[smartslider2 slider=\"{$id}\"]");
}
else
{
    // ID not mapped
}

Edited by pritaeas

0

Hi, I have tried showing the banners by, get_the_ID, is_product_category and is_product. They all work in varying degrees but the main problem i seem to be getting with all category pages now is the correct category banner with show on the page along with the banner for the first product in that category. If i change the second category product to the first one, i will get the banner for that product displaying on the same page as the main category. My code is correct as it works for the product pages its just for some reason the first product banner seems to want to display on its category page as well. Is there a way of making the product banner code just apply to that product and not overspill onto the category page?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.