Okay
  Public Ticket #3161297
Salient Ajax Issue for Hierarchical Categories
Closed

Comments

  • noo-studio started the conversation

    Hello. I have been using your theme for a while, and I wanted to ask is it normal that when I use hierarchical categories, Ajax result in the following issue: 

    When displayed directly: the posts are showing all the hierarchical url right (first 8 posts). But once filter on top or load more is activated, the Ajax return the post link only with parent category, not with the child. And since my post structure often gave hierarchical category, then it results to coming to 404 (or home page redirect in my case). I try to solve it, but it seems everywhere when I use the post module, once I enable load more or filter on top, it always result in the same mistake. 

    As you see in attached screen, once I load page, already visible post have a right url. But once I click any of the filter (or load more), the url of the pages change and omit the second part of the slug (fall,winter,etc). Same post in module, 2 url…

    Attached files:  22B3E90D-C84E-493A-A1BF-3BFE304D7C08.jpeg
      68EB7624-1B6D-43CF-BEAF-58D50CFEFE16.png
      BAB9FE43-D18D-41F5-A271-1967858CE08F.jpeg
      E4CFFF51-7956-42A3-ACF5-9F6DA7BCE9B8.png

  •  997
    ThemeNectar replied

    Hey noo-studio,

    So far I haven't been able to replicate that issue locally when using nested categories + the Post grid with ajax loaded items. Are you using /%category%/%postname%/ for your permalink structure? It appears that you have many other categories assigned to your items other than the main levels shown in the URL so I'm wondering if you have a more complex permalink structure implemented through the child theme/.htaccess?

    Kind regards

  • noo-studio replied

    Yes, the permalink is category/postname. to create a post I am using a primary category from yoast seo to set up the post permalink, but that post also connects to color category and season category. Since in ajax — the primary category from Yoast is not implemented (I assume) then the ajax loaded post permalinks are just parent-category/postname. 

    So if the post as in my website have the following categories associated with it: 

    Color Palette > Fall*Primary Category

    Color Palette > Color > Beige

    Pantone > Pantone Number

    ... and I select "Fall" as Yoast Primary Category, then the post first loaded have the right url and there is no error: 

    kidspattern.com/color-palette/fall/postname

    ... but the post loaded with load more or ajax filter on the top get the following url:

    kidspattern.com/color-palette/postname. 

    The Child of Category from URL is not coming through, only the first category alphabetically. 


    I hope that explanation is clear. 

    I was wondering if that issue is connected to the URL structure that comes from setting up the Yoast Seo Primary Category as a Child — and Ajax not being able to determine that class. I know that Yoast have its own hey in php, I have tried to edit it, but I am not as advanced. 

    $args['meta_key'] = '_yoast_wpseo_primary_category';

    The same thing is happening with the Slider on the Home Page: it reads the first category (alphabetically), so if the post is in Autumn, and that is my Yoast Primary Category, the slider instead of displaying that, is showing first one: Black. 

    The reason that I used categories instead of tags, was that in your theme the categories pages with thumbnails and filter and modules to display, allow me to build the custom pages for color, season, etc nicely visually and enable to sort them for different people looking for information. I couldn't do that effectively with the tags. But I diverge a little. 

  •  997
    ThemeNectar replied

    Hey noo-studio,

    Thanks for explaining the setup in further detail. Upon digging into the Yoast Primary category code, I found the same issue documented on their GitHub: https://github.com/Yoast/wordpress-seo/issues/11717

    It appears their method for registering hooks is limited to the initial page load and won't occur at all for subsequent AJAX requests. Therefore, the filtering they do for the category will never execute https://github.com/Yoast/wordpress-seo/blob/trunk/src/integrations/primary-category.php

    A workaround for that would be to take their category filter logic and apply it via a standard filter from your child which will also run for AJAX requests. Here's a snippet you can add to your child functions.php file:

    function salient_child_yoast_get_primary_cat($post ) {
      $primary_term = new WPSEO_Primary_Term( 'category', $post->ID );
      return $primary_term->get_primary_term();
    };
    function salient_child_post_link_cat_filter($category, $categories = null, $post = null) {
        $post = \get_post( $post );
            if ( $post === null ) {
                return $category;
            }
            $primary_category = salient_child_yoast_get_primary_cat( $post );
            if ( $primary_category !== false && $primary_category !== $category->cat_ID ) {
                $category = \get_category( $primary_category );
            }
            return $category;
    }
    add_filter( 'post_link_category', 'salient_child_post_link_cat_filter', 10, 3 );
    
  • noo-studio replied

    The filter is working like a dream now. Thank you so much. 

    The solution addresses the Ajax, but the main slider still have the one category displayed, which is the same as the alphabetic order. Is there a way it also addresses that? 

    Thank you so much for a quick response, your theme is amazing and I really think it is so flexible, I am able to customise it so much, and the styling is so clean even for the complex data. 

    So if you can let me know how I can also have it working for the sliders, where the category is displayed (homepage slider will show the primary category as well), then it would be great. As you see the post part have still black: instead of fall (primary category) displayed in the slider. 

    In the meantime, thank you again! Best theme ever. 

    Attached files:  Screenshot 2022-11-09 at 14.42.12.jpg

  •  997
    ThemeNectar replied

    Glad to hear, noo-studiosmile.png

    Unfortunately adding the Yoast-specific primary category into the recent posts element would require modifying the element template file: wp-content/plugins/salient-core/includes/vc_templates/recent_posts.php You could do so via a child theme using this method: https://themenectar.com/docs/salient/overriding-salient-wpbakery-element-template-files-through-a-child-theme/

    And then implement a new function to query for the primary category such as https://whiteleydesigns.com/code/get-primary-term-if-yoast-seo-is-being-used/

    The line you would need to implement is around 704 in the recent_posts.php file.

    Kind regards