• Resolved neverStopAL

    (@neverstopal)


    I have a filter in place to remove the parent category from my permalink structure. When I activate your plugin and give a secondary category, the permalink structure picks up the parent category again, but the permalink only gets the parent category if it is not in same parent. If the secondary cat is in the same parent cat, the permalink does not pull in the parent. Here’s the filter from the functions.php. Can you see something I should be setting differently here?

    add_filter( 'post_link', 'remove_parent_cats_from_link', 10, 3 );
    function remove_parent_cats_from_link( $permalink, $post, $leavename ) {
    
        $cats = get_the_category( $post->ID );
        if ( $cats ) {
            usort( $cats, '_usort_terms_by_ID' ); // order by ID
    
            if ( $parent = $cats[0]->parent ) {
                $parentcats = get_category_parents( $parent, false, '/', true );
                $permalink = str_replace( $parentcats, '', $permalink );
            }
        }
        return $permalink;
    }

    https://wordpress.org/plugins/wp-category-permalink/

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hi neverStopAL,

    The way a post in a “permalinked” subcategory works is that it keeps its parent category slug in the URL even if the parent category is not assigned to the post (see also the FAQ).

    Did you try to create a filter which removes the parent category slug from the URL?

    Keep in mind that WordPress keeps the category > subcategory permalink structure for categories regardless of your categories assignment to a post.

    For example, suppose you’ve “permalinked” a subcategory but didn’t assign its parent category to the post – you’ll see that the subcategory link (you can see it using the categories widget for example) keeps the category/subcategory URL structure.

    Thread Starter neverStopAL

    (@neverstopal)

    Yes, that is what I’m seeing. The filter, that is removing the parent category from post link, is not being ‘hit’ again after the plugin’s permalink button click makes the link bold, making it primary… Is there a way to “refresh” the categories (or force the post link filter to rerun) after the new Primary category is discovered?

    Hi neverStopAL,

    I’m not sure I totally understand your issue. Can you please supply an example?

    Thanks,
    Yaniv

    Thread Starter neverStopAL

    (@neverstopal)

    Hi Yaniv,
    Sorry so long to reply. I am unfortunately working behind a firewall here that won’t allow for you to see the code. But I’ll try to explain. I am trying to apply next/previous post links on a page in an infinite loop. So, when you always see 2 links. The solution worked fine (using get_adjacent_posts() & get_next/prev_posts() ) until the client decided to add Secondary Categories as a requirement. So now I have had 2 problems.

    First problem: The original problem was that I filter the permalink. For instance this is the structure: http://site.com/category/parent-category/sibling-category/post. With the filter the permlink becomes http://site.com/sibling-category/post. When I use the plugin to change the Primary category the permalink was getting the Parent category again like so: http://site.com/parent-category/sibling-category/post. I found a second filter in the functions.php file that was causing that. I’ve disabled that. One problem solved. 🙂

    Second/new problem The next/previous links loop now includes posts that have the existing post’s Primary category as a Secondary Category. When the user clicks a next/previous link to a post with a different Primary Category, the next/previous loop is now looping through a different set of articles. This is for me an unacceptable user experience. I need to query to find the Primary Category and exclude posts that have a different Primary Category. Have you seen a need for that in the past?

    Hi neverStopAL,

    I’m glad you’ve solved your first problem.

    As Iv’e written above, this requirement is risky because it only affects the selected post, and not necessarily all other links on the page such as categories. In this situation I would advise to rearrange your categories so that your parent category would actually be a parent category.

    Regarding your second problem, the previous and next links of a post should keep the “permalink” settings you’ve set for them. However, since you are altering the plugin / WP setting in a way that the plugin isn’t supposed to work for, I’m not sure I can currently help you with this problem.

    Best,
    Yaniv

    Thread Starter neverStopAL

    (@neverstopal)

    No worries. One of my teammates wrote a custom db query to accomplish it. Thanks again for all your help.

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Adds Parent Category back to permalink’ is closed to new replies.