Support » Developing with WordPress » child of parent categories retrieved on mobile, not retrieved on tablet/desktop

  • Resolved hufferja

    (@hufferja)


    I am managing a clients website which is updated to WordPress 6.0 and I have noticed that a secondary menu in mobile is loading properly grabbing the child of parent category for the details menu item, specs menu item and tech specs menu item. But if you go to tablet or larger display it is retrieving all the categories and creating list items for each. The code is written the same for both menus. Both arrays are written to grab the child of the parents ID. I am not sure why it’s working for mobile, but being ignored when going to larger displays and showing all categories. Would like some help with this as my current troubleshooting has gotten no results.

    below is the code being used for both menu’s to get the children of parent and create a list item for each child.

    <?php
    
          	$categories  = get_categories('child_of=12&orderby=title&order=ASC');
    	  foreach($categories as $category) {
    	$args = array( 'post_type' => 'post', 'category_name' => $category->name);
    	$num = $category->count;
    	$url = get_category_link($category->term_id);
    
    	       echo '<li>';
    	       echo "<a class='checkboxcat' id='category-$category->term_id' href='$url' title='category-$category->term_id'>";
      	       echo $category->cat_name;
    	       echo "&nbsp;(<span>" . $num . "</span>)</a>";
    	       echo '</li>';
    	}
    	wp_reset_postdata();  ?>

    The page I need help with: [log in to see the link]

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

    (@bcworkz)

    PHP running server side has no idea what sort of device it’s sending output to. It just sends it out. There are actually both menu versions on the page, CSS manages which is visible via media queries.

    I’m unable to verify, but I’m almost certain that different template code is generating each list. It cannot be completely identical since each menu container has different HTML class attributes.

    That said, there is another recent forum topic where the OP had a similar issue. The first category query worked as intended, but the second did not. Two other forum members tried identical code on their own sites and got proper results for both. The conclusion is that there’s something wrong with the OP’s server that’s preventing proper results. The root cause hasn’t been determined, but it’s certainly not the PHP code. I’m guessing it is outdated PHP or mySQL versions. This may not be at all related to your issue, but it does sound remarkably similar.

    Thread Starter hufferja

    (@hufferja)

    After going to my host to contact support this message comes up. “We are experiencing temporary issues with MySQL service “pdx1-mysql-a7-6″. Our operations team is actively working on restoring services.” PHP version is 7.4 and hasn’t changed. So this mySQL problem could be the causing this issue?

    Moderator bcworkz

    (@bcworkz)

    Could be, but it’s hard to say for sure. MySQL server issues usually manifest by erratic or no results. Your category queries seem to be consistent, even if not what you wanted.

    Would you be able to provide the full code responsible for both mobile and desktop versions of the secondary menus? I imagine it’s somewhat lengthy, so please post to pastebin.com, gist.github.com, or similar public sites intended for sharing code. Provide the resulting link here.

    It probably doesn’t matter, but in the code you posted, the $args assignment and wp_reset_postdata() don’t have anything to do with category code. Perhaps it’s related to other code you had left out? Just an observation, doesn’t affect category code either way.

    Thread Starter hufferja

    (@hufferja)

    I wasn’t the original developer of this code, we took it over in order to build the responsive side of the original website. I also wasn’t the developer that built the responsive side just manage it now.
    Below are the links to the pastebins of the code for each templated menu.

    Secondary Menu tablet/desktop
    https://pastebin.com/bKXcjbWr

    Secondary Menu Mobile
    https://pastebin.com/EddJ3ATj

    Moderator bcworkz

    (@bcworkz)

    Thanks for posting those. I thought there might be something in there that could indirectly alter the category query. I used those as a basis for testing on my site, after removing unrelated code and altering child_of IDs to match those on my site. My final conclusion is there is a bug within WP core itself. It has to do with object caching. WP sees a query repeated and instead of re-running the query, it goes to its object cache. Except the cache has all the terms listed, not only children of a particular term.

    Somebody else had just filed a Trac bug report minutes before I came to this conclusion. You can add yourself as a watch over there to be notified of new replies to this ticket. The OP offers a suggestion on how to correct the cache content to get correct repeat results. You can instead do your own form of caching. I believe all of this code, both mobile and desktop, is within the same scope, with mobile code first.

    In mobile, alter $categories for each parent category (assignment and in subsequent foreach) so each is unique for that parent. For example $details, $specs, $tech. Then on the desktop template, comment out the get_categories() assignment and change the related foreach to use the same unique variable from the mobile menu. Thus the initial list from mobile is reused for desktop without making another query. This will only work if the reused list variables are within the same scope. If not, you’ll need to declare them as globals.

    Thread Starter hufferja

    (@hufferja)

    changing the variable to be unique in the mobile version editing the desktop template commenting out the get_categories() assignment and changing the related foreach loop in the desktop menu worked. Both menu’s are working properly now. I thought that might have something to do with it since it was reusing the same variable over and over again. And I originally tried that but didn’t comment out the get_categories() assignment in the desktop menu. Thanks for the help. I really appreciate you helping me with this.

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.