• Resolved Kevin Tunis

    (@ktunis)


    I’ve categorized my blog articles using tags: ‘business’ and ‘personal’. I’d like to structure the URLs to reflect these tags in combination with specific topics. For instance, for an article under the ‘Growth and Strategy’ topic, I want the URL to be: /business/growth-and-strategy/article-title for business-related articles, and /personal/growth-and-strategy/article-title for personal ones. How can I achieve this?”

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Maciej Bis

    (@mbis)

    Hi Kevin,

    You will need to use a custom code snippet to make the articles’ permalinks conditional because the plugin allows to specify one permastructure (permalink format) for all posts through the plugin interface.

    If you use the inbuilt tags (post_tag taxonomy), the code snippet should look as follow. I am not exactly certain if this would work out-of-box in your specific case, but it should not be difficult to adjust it:

    function pm_filter_post_permastructure($permastructure, $post) {
    	// Filter only 'Post' permalinks
    	if(empty($post->post_type) || $post->post_type !== 'post') {
    		return $permastructure;
    	}
    	
    	// A. Articles tagged 'business' 
    	if(has_term(array('business'), 'post_tag', $post)) {
    		$permastructure = 'business/growth-and-strategy/%postname%';
    	}
    	// B. Articles tagged 'personal'
    	else if(has_term(array('personal'), 'post_tag', $post)) {
    		$permastructure = 'personal/growth-and-strategy/%postname%';
    	}
    	
    	return $permastructure;
    }
    add_filter('permalink_manager_filter_permastructure', 'pm_filter_post_permastructure', 10, 2);

    The snippet, it is supposed to dynamically overwrite the permalink settings (“Permastructures”) you have set (if certain condition is met) via the interface.

    Thread Starter Kevin Tunis

    (@ktunis)

    I have made some adjustments to the code it acts like it is not making any changes at all. Are there any settings in the plugin I need to set?

    function pm_filter_post_permastructure($permastructure, $post) {
    	// Filter only 'Post' permalinks
    	if(empty($post->post_type) || $post->post_type !== 'post') {
    		return $permastructure;
    	}
    	
    	// A. Articles tagged 'business' 
    	if(has_term(array('business'), 'post_tag', $post)) {
    		$permastructure = 'business/%category%/%postname%';
    	}
    	// B. Articles tagged 'personal'
    	else if(has_term(array('personal'), 'post_tag', $post)) {
    		$permastructure = 'personal/%category%/%postname%';
    	}
    	
    	return $permastructure;
    }
    add_filter('permalink_manager_filter_permastructure', 'pm_filter_post_permastructure', 10, 2);
    Plugin Author Maciej Bis

    (@mbis)

    All modifications to permalink formats (Permastructures) are only applied to new items (posts) by default. To set it for existing post items, please use the “Regenerate/reset” function:

    https://permalinkmanager.pro/docs/basics/how-to-regenerate-the-custom-permalinks/#1-how-does-the-regenerate-reset-tool-work

    Thread Starter Kevin Tunis

    (@ktunis)

    Ok, I got that to work, thanks. Now when I am creating a archive page or clicking on the /business/ this does not show all the post with that tag. It does the same for categories. How do I show that?

    Plugin Author Maciej Bis

    (@mbis)

    The free version allows to change only the permalinks of individual posts. If you want to edit the post tag archives as well, you will need the Pro version.

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

The topic ‘Tags’ is closed to new replies.