我目前使用的是Avada主题,该主题利用了作为投资组合类别的自定义帖子类型;投资组合项目(属于一个或多个类别的一部分)。
该主题提供了使用固定slug“前缀”的解决方案(如图所示here). 此解决方案转换为如下url:www.domain.com/fixed-prefix-here/portfolio-item-title
我想尝试的是动态设置固定前缀,将其设置为该自定义帖子的类别。
F、 e.投资组合项目“测试”的段塞,其类别为“类别测试”,应为:www.domain.com/category-test/test
我对WordPress开发相当陌生,而不是PHP。欢迎任何帮助。非常感谢。
EDIT:
Try #1 我将“固定slug”设置为自定义分类法
%portfolio_prefix%
并在保存自定义分类法后在函数中替换它(请参见代码段)。这样就可以了,单个帖子会显示自定义url,打开后帖子会正确加载。不幸的是,当我出于某种原因打开它们时,我的单页崩溃了。欢迎提供任何提示。
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);
}