• Resolved sefanjotwo

    (@sefanjotwo)


    Hi,

    I am working on some pages and I need to match the ID of the page to display some stuff. Maybe I am overcomplicating things but right now the code that I have does not work.

    //Keys
    if (get_the_ID() == 45424){
        echo do_shortcode('[product_categories ids="2063, 2066, 2070, 2097, 2098, 2099"]');
    }
    	
    //Gitaar
    if (get_the_ID() == 45263){
        echo do_shortcode('[product_categories orderby="include" ids="2105, 2106, 2107, 2108, 2109, 2111, 2112, 2113, 2114, 2115, 2116"]');
    }
        
    //Pro Audio    
    if (get_the_ID() == 45426){
        echo do_shortcode('[product_categories orderby="include" ids="1960, 1568, 1973, 1984, 1988, 1990"]');
    }
        
    //Studio & Recording    
    if (get_the_ID() == 45428){
        echo do_shortcode('[product_categories orderby="include" ids="1540, 1594, 1601, 1695, 1716, 1727"]');
    }
        
    //Accessoires
    if (get_the_ID() == 45430){
        echo do_shortcode('[product_categories orderby="include" ids="1532, 1534"]');
    }
        
    //Bekabeling    
    if (get_the_ID() == 45432){
        echo do_shortcode('[product_categories orderby="include" ids="1523, 1524, 3631, 4350, 1525, 1526, 1527, 3964"]');
    }
        
    //Case's & Koffers
    if (get_the_ID() == 45434){
        echo do_shortcode('[product_categories orderby="include" ids="1519, 1521"]');
    }
    
    //Rigging & Stands
    if (get_the_ID() == 45436){
        echo do_shortcode('[product_categories orderby="include" ids="4322, 3653"]');
    }
        
    //Licht & Effecten    
    if (get_the_ID() == 45438){
        echo do_shortcode('[product_categories orderby="include" ids="1508, 4321, 1510, 1511, 1512, 1513"]');
    }
        
    //Drums    
    if (get_the_ID() == 45440){
        echo do_shortcode('[product_categories orderby="include" ids="1503, 1505, 1506, 4099"]');
    }
        
    //Overige Snaarinstrumenten
    if (get_the_ID() == 45442){
        echo do_shortcode('[product_categories orderby="include" ids="1500, 1501, 2110, 2115"]');
    }

    Only the ‘Gitaar’ section is displaying correctly, all the others are not showing anything. I triple checked the numbers to see if the ID’s are correct but they are. Does anyone know why I am not seeing a thing?

    Also tried to add an else to display an error code but it did not came trough.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz

    (@bcworkz)

    Are you sure the various shortcode parameters are all correct? For example if you were to use Pro Audio parameters in the Gitaar section, would the appropriate Pro Audio content be output? Probably, but worth checking.

    The value from get_the_ID() will vary depending on what secondary queries or output loops on the page have executed. It’s only reliable within the primary loop. If these are all singular pages, you should be able to get the page’s ID with get_queried_object_id(). If this too fails to work, there has been some secondary query that has failed to reset itself when output had completed, or your code is used while a secondary query is active. In that case, call get_queried_object_id() early on, before any secondary queries. Save the ID in some unique variable for subsequent use.

    ETA: While a series of if(): statements is perfectly fine, you might consider using a switch/case structure instead. It’s IMO a more elegant approach and likely more efficient as well.
    https://www.php.net/manual/en/control-structures.switch.php

    • This reply was modified 3 years, 7 months ago by bcworkz.
    Thread Starter sefanjotwo

    (@sefanjotwo)

    Hi,

    Thanks for the reply.
    I’ve checked/switched the shortcode parameters but without succes.

    Also tried the get_queried_object_id() but it did not make an difference.

    At last I did swap my if(): with a switch/case structure. This looks like this:

    switch (get_the_ID()) {
        //Keys
        case 45424:
    	echo "Works";
            break;
        //Gitaar
        case 45263:
            echo "Works";
            break;
        //Pro Audio
        case 45426:
            echo "Works";
            break;
    }

    I tried with three page id’s and I am getting the same results as before, the Gitaar page is working and the other ones do not work. So my guess is that the ID is not correct which is strange because we’ve checked that multiple times. (Even my coworker did check if they are the same number)

    Also I’ve tried to use get_queried_object_id() instead of if():.

    Very strange..

    • This reply was modified 3 years, 7 months ago by bcworkz. Reason: code format fixed
    Moderator bcworkz

    (@bcworkz)

    It seems you’re getting the ID in the wrong context. For example, when a secondary query is active. Then you’d get that related ID instead of the one you’re looking for. Maybe by examining the ID you do get would help lead you to what context you’re in and help you get out of that context and into the correct one. If the returned ID alone isn’t meaningful, try var_dumping() the global $wp_query object right where you get the ID. You’ll then see every property that exists for the current query. In particular look at the query_vars and request properties. If the query is not for a single page, you certainly are in the wring context. (output will be more readable if you var_dump() within <pre> tags)

    In case you were not aware, WP reuses the same global $wp_query query object for every query made, the “context” I’m referring to. Even menu items reuse the same query object. If you were to get an ID when the menu was being generated, you’d get a menu item ID. Fortunately, the wp_nav_menu() function resets its query so this would rarely be an issue. However, it’s much more common for template code to fail to reset its query, or to simply cause your code to be inserted into the wrong context to start with.

    Thread Starter sefanjotwo

    (@sefanjotwo)

    Let me add some context, the purpose of the code is to display a product category page. We run a webshop, so when people click or go to the global categorie they first should go to this page. So they can click on the desired category.

    Instead of making multiple pages I thought it would be easier to store all the code in a template page and let WP/WooCommerce do the rest.

    So the code should check if the ID of the page (which I am getting from making the page.) is equal to one the ID’s in the code, if so then it should display all the categories that I want it to display.

    I’ve did a var_dump but did not see something wrong there. If you want I can sent you the given data.

    But why is the code only working for 1 category, even if that category is not in first place. Why do all the others not working?

    Sorry in advanced if I am misunderstanding you!

    Thread Starter sefanjotwo

    (@sefanjotwo)

    I have found the problem, no idea how I missed this but okay..

    Somehow I forgot to set the page templates to the right one, now every page is working as it should!

    Thanks for you help!

    Moderator bcworkz

    (@bcworkz)

    Woo hoo! I’m glad it’s solved. You’re welcome.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘get_the_ID()’ is closed to new replies.