我如何使用从 post 中的自定义分类法中选择的年份自定义永久链接,然后在 wordpress 中使用 post tile

How I customize permalink with year selected from custom taxnomy in post then post tile in wordpress

我想在标题为 post 的自定义永久链接中添加 post select 的自定义分类法的“年份” 例如。假设在我的 post 中有 year 的自定义分类法。所以我可以 select 年特定 post 。我有 select 年 2018 那么我的永久链接应该是 http ://mysite/2018/post-title 我有 select 年 2019 那么我的永久链接应该是 http ://mysite/2019/post-title

意味着固定链接应该取决于 select 年 post。

我查看了 google 并进行了研发,但没有找到与此相关的内容。如果有人对此有想法,请帮助我

我得到了答案

在 functions.php 文件中添加以下代码

add_filter('post_link', 'postcustom_permalink', 10, 3);
add_filter('post_type_link', 'postcustom_permalink', 10, 3);
 
function postcustom_permalink($permalink, $post_id, $leavename) {
    if (strpos($permalink, '%winneryear%') === FALSE) return $permalink;
     
        // Get post
        $post = get_post($post_id);
        if (!$post) return $permalink;
 
        // Get taxonomy terms
        $terms = wp_get_object_terms($post->ID, 'winneryear');   
        if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;
        else $taxonomy_slug = 'no-year';
 
    return str_replace('%winneryear%', $taxonomy_slug, $permalink);
} 

然后在后端的自定义永久链接中添加 %winneryear% 之后。