WordPress Development Asked by SolidTerre on December 4, 2021
I’m currently using the Avada Theme that is making use of custom post types being Portfolio Categories & Portfolio Items (which are part of one or more categories).
The theme is offering the solution to use a fixed slug “prefix” (as can be seen here). This solution translates into an url like this:
www.domain.com/fixed-prefix-here/portfolio-item-title
What I would like to attempt, is to set the fixed prefix dynamically, setting it to the category of that custom post.
F.e. the slug of the portfolio item “test”, which has as category “category-test” should be: www.domain.com/category-test/test
I’m fairly new to WordPress development, not to PHP. Any help is welcome.
Thank you !
EDIT:
Try #1 I set the “fixed slug” to a custom taxonomy %portfolio_prefix%
and replacing it in a function after saving that custom taxonomy (see code snippet). This works, the custom url’s are shown for single posts, and posts load correctly when opened. Unfortunately my single pages crashes when I open them for some reason. Any tips welcome.
add_action('init', 'add_custom_taxonomies', 0);
// The post_link hook allows us to translate tags for regular post objects
add_filter('post_link', 'portfolio_permalink', 10, 3);
// The post_type_link hook allows us to translate tags for custom post type objects
add_filter('post_type_link', 'portfolio_permalink', 10, 3);
function add_custom_taxonomies()
{
register_taxonomy('portfolio_prefix', 'post', array(
'hierarchical' => false,
'labels' => array(
'name' => _x('Portfolio prefix', 'taxonomy general name') ,
'singular_name' => _x('Portfolio prefix', 'taxonomy singular name') ,
'search_items' => __('Portfolio prefix search') ,
'all_items' => __('All Portfolio prefix') ,
'edit_item' => __('Edit Portfolio prefix') ,
'update_item' => __('Update Portfolio prefix') ,
'add_new_item' => __('Add New Portfolio prefix') ,
'new_item_name' => __('New Portfolio prefix Name') ,
'menu_name' => __('Portfolio prefixs') ,
) ,
'rewrite' => array(
'slug' => 'portfolio_prefix', // This controls the base slug that will display before each term
'with_front' => false, // Don't display the category base before "/locations/"
'hierarchical' => false
) ,
));
}
function portfolio_permalink($permalink, $post_id) {
// If the permalink does not contain the %portfolio_prefix% tag, then we don’t need to translate anything.
if (strpos($permalink, '%portfolio_prefix%') === FALSE) return $permalink;
// Get post
$post = get_post($post_id);
if (!$post) return $permalink;
// Get the portfolio_prefix terms related to the current post object.
$terms = wp_get_object_terms($post->ID, 'portfolio_category');
// Retrieve the slug value of the first portfolio_prefix custom taxonomy object linked to the current post.
if ( !is_wp_error($terms) && !empty($terms) && is_object($terms[0]) )
$taxonomy_slug = $terms[0]->slug;
// If no portfolio_prefix terms are retrieved, then replace our portfolio_prefix tag with the value "portfolio_prefix"
else
$taxonomy_slug = 'portfolio_category';
// Replace the %portfolio_prefix% tag with our custom taxonomy slug
return str_replace('%portfolio_prefix%', $taxonomy_slug, $permalink);
}
I think your solution is here Include Taxonomy slug in post url
Also, you can try to play with the permalink structure in Settings->Permalinks WordPress admin menu, but I'm not sure that it helps you.
Answered by Oleh on December 4, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP