如何更改帖子永久链接的标题加自定义字段值

问题描述 投票:0回答:1

我正在创建流媒体网站,我在帖子页面内有自定义帖子类型(观看)和自定义字段(eps_num)。 当我创建新帖子(标题 = Super Natural)时,帖子永久链接如下所示 www.website.com/watch/Super-Natural/ , (eps_num = 01)

如果我必须创建具有相同标题的新帖子, ( 永久链接 = /watch/Super-Natural-1/ ) ,(eps_num = 05)。

我想将帖子永久链接更改为(/watch/Super-Natural-05/),这意味着在永久链接中获取标题+自定义归档(eps_num)值。

这是我尝试过的

add_filter( 'post_type_link', 'wpse_64285_external_permalink', 10, 2 );
function wpse_64285_external_permalink( $link, $post )
{
    $meta = get_post_meta( $post->ID, 'eps_eps-num', TRUE );
    $url  = esc_url( filter_var( $meta, FILTER_VALIDATE_URL ) );

    return $url ? $url : $link;
}

还有这个

add_filter( 'pt_cv_ctf_value', 'cpt_link_to_cfv', 100, 3 );
function cpt_link_to_cfv( $args, $key, $post ) {
    if ( $key == 'eps_eps-num' ) {
        $args = sprintf( '<a href="%s">%s</a>', get_permalink( $post ), $args );
    }

    return $args;
}

我该怎么做??

wordpress post advanced-custom-fields custom-post-type permalinks
1个回答
0
投票

我已经找到答案了

function custom_post_permalink($permalink, $post) {
    if (get_post_type($post) == 'watch') {
        $custom_field_value = get_post_meta($post->ID, 'eps_eps-num', true);
        $post_title = get_the_title($post->ID);

        // Manipulate the permalink structure as needed, for example:
        $new_permalink = $post_title . '-' . $custom_field_value; // This is just a basic example
        
        return home_url(user_trailingslashit($new_permalink));
    }
    return $permalink;
}
add_filter('post_type_link', 'custom_post_permalink', 10, 2); 

这是更新永久链接的重要行

return home_url(user_trailingslashit($new_permalink));

任何人需要使用它,您应该从管理面板刷新 /options-permalink

© www.soinside.com 2019 - 2024. All rights reserved.