prosource

같은 슬러그의 커스텀 포스트 같은 슬러그의 잘못된 포스트로 리다이렉트

probook 2023. 2. 16. 21:53
반응형

같은 슬러그의 커스텀 포스트 같은 슬러그의 잘못된 포스트로 리다이렉트

커스텀 포스트 타입의 투고가 2개 있습니다.비디오 포스트 및 시티 가이드 투고첫 번째 포스트(Video Post)에는 104.130.239.132/rick-owens/ 및 두 번째 포스트(City-Guide Post) 코나틴 URL:http://104.130.239.132/city-guide/rick-owens/(시티 가이드)가 포함되어 있습니다.따라서 첫 번째 URL에 접속하려고 하면 템플릿과 두 번째 URL의 내용이 여기에 표시됩니다.두 번째 URL은 최근에 공개되었습니다.Yoast Seo Plugin을 비활성화하여 스스로 해결하려고 시도했지만 변경은 없었고 permalink refresh도 마찬가지였습니다.

첫 번째 URL 투고용 Yoast SEO 스니펫을 첨부해 주세요.또한 두 번째 URL 투고용 Yoast SEO 스니펫 Any Help를 첨부해 주시면 감사하겠습니다.감사해요.

추가:

제 CPT 코드입니다.

1. 비디오 투고:

$labels = array(
'name'                => _x( 'Videos', 'Post Type General Name', 'roots' ),
'singular_name'       => _x( 'Video', 'Post Type Singular Name', 'roots' ),
'menu_name'           => __( 'Video Posts', 'roots' ),
'parent_item_colon'   => __( 'Parent Video:', 'roots' ),
'all_items'           => __( 'All Videos', 'roots' ),
'view_item'           => __( 'View Video', 'roots' ),
'add_new_item'        => __( 'Add New Video', 'roots' ),
'add_new'             => __( 'Add New', 'roots' ),
'edit_item'           => __( 'Edit Video', 'roots' ),
'update_item'         => __( 'Update Video', 'roots' ),
'search_items'        => __( 'Search Video', 'roots' ),
'not_found'           => __( 'Not found', 'roots' ),
'not_found_in_trash'  => __( 'Not found in Trash', 'roots' ),
);
$rewrite = array(
'slug'                => 'rewrite',
'with_front'          => true,
'pages'               => true,
'feeds'               => true,
);
$args = array(
'label'               => __( 'video', 'roots' ),
'description'         => __( 'Videos Post Type', 'roots' ),
'labels'              => $labels,
'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'trackbacks', 'revisions', 'custom-fields', ),
'taxonomies'          => array( 'category', 'post_tag' ),
'hierarchical'        => false,
'public'              => true,
'show_ui'             => true,
'show_in_menu'        => true,
'show_in_nav_menus'   => true,
'show_in_admin_bar'   => true,
'menu_position'       => 5,
'can_export'          => true,
'has_archive'         => true,
'exclude_from_search' => false,
'publicly_queryable'  => true,
'rewrite'       => $rewrite,
'capability_type'     => 'post',
'yarpp_support'    => TRUE
);
register_post_type( 'video', $args );

2. 시티 가이드 투고:

function register_post_types(){
register_post_type( 'city-guide', [
'has_archive' => TRUE,
'hierarchical' => TRUE,
'labels' => [
'name' => 'City Guide'
],
'public' => TRUE,
'supports' => ['editor', 'page-attributes', 'revisions', 'thumbnail', 'title','custom-fields','excerpt'],
'taxonomies' => array('post_tag')
] );
add_image_size( 'ipad-city-thumb', 650, 650, TRUE );
}
add_action( 'init', __NAMESPACE__.'\register_post_types', 20 );

그럼 이건 내 코드에 있어

remove /rewrite/ slug from custom permalinks, to allow domain/slug for all post types
public function post_link_rewrite( $post_link, $post, $leavename ){
      $post_link = str_replace( '/rewrite/', '/', $post_link );

      return $post_link;
    }

아래 코드로 레지스터 포스트 타입 코드를 업데이트하고 가능하면 시티 가이드 포스트 타입에 동일한 코드를 사용합니다.

$labels = array(
'name'                => _x( 'Videos', 'Post Type General Name', 'roots' ),
'singular_name'       => _x( 'Video', 'Post Type Singular Name', 'roots' ),
'menu_name'           => __( 'Video Posts', 'roots' ),
'parent_item_colon'   => __( 'Parent Video:', 'roots' ),
'all_items'           => __( 'All Videos', 'roots' ),
'view_item'           => __( 'View Video', 'roots' ),
'add_new_item'        => __( 'Add New Video', 'roots' ),
'add_new'             => __( 'Add New', 'roots' ),
'edit_item'           => __( 'Edit Video', 'roots' ),
'update_item'         => __( 'Update Video', 'roots' ),
'search_items'        => __( 'Search Video', 'roots' ),
'not_found'           => __( 'Not found', 'roots' ),
'not_found_in_trash'  => __( 'Not found in Trash', 'roots' ),
);

$args = array(
'labels'              => $labels,
'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments',         'trackbacks', 'revisions', 'custom-fields', ),
'taxonomies'          => array( 'category', 'post_tag' ),
'hierarchical'        => false,
'public'              => true,
'show_ui'             => true,
'show_in_menu'        => true,
'show_in_nav_menus'   => true,
'show_in_admin_bar'   => true,
'menu_position'       => 5,
'can_export'          => true,
'has_archive'         => true,
'exclude_from_search' => false,
'publicly_queryable'  => true,
'query_var' => true,
'rewrite'       => true,
'capability_type'     => 'post',
'yarpp_support'    => TRUE
);
register_post_type( 'video', $args );

두 포스트 타입의 슬러그 이름이 비슷하기 때문에 최신 포스트가 표시됩니다.현재 포스트 타입에는 반영되지 않습니다.permalink 구조에서 post-name 또는 %postname%를 선택했을 것입니다.이 문제를 해결하기 위해 가장 쉽고 첫 번째 방법은 게시물 중 하나의 포스트 슬러그를 변경하는 것입니다.

또 하나는 기능에서 포스트 타입의 퍼머링크 구조를 변경하는 것이다.php에서 WordPress의 클래스 함수를 다시 씁니다.그러기 위해서는 투고 제목 아래의 링크를 참조해야 합니다.특정 커스텀 포스트 타입의 포스트 타입의 슬래그를 알아내기 위해서입니다.

로컬 설치에 제공된 코드를 구현했습니다.이제 저는 당신의 2개의 커스텀 포스트 타입과 두 이름 모두 "rick-owens" 포스트를 가지고 있습니다.우선, 전술한 것과 같은 에러가 표시됩니다.

저는 이 기능을 함수에 추가하여 이 문제를 해결했습니다.php

function add_rewrite_rules_custom_post(){

global $wp_rewrite;

    $structure   = 'rewrite/%rewrite%';
    $structure1  = 'city-guide/%city-guide%';

    $wp_rewrite->add_permastruct('%rewrite%', $structure, false);
    $wp_rewrite->add_permastruct('%city-guide%', $structure1, false);
}
add_action('init', 'add_rewrite_rules_custom_post');

위의 코드는 나에게도 유효하고, 테스트도 실시해, 당신에게도 유효합니다.

이미 말씀드렸듯이, 그 게시물의 편집 페이지에서 게시물의 URL을 알 수 있을 것입니다.

비디오 커스텀 투고 타입의 경우 투고 URL은 다음과 같습니다.

www.siteurl.com/rewrite/rick-owens

시티 가이드 투고 타입의 경우 투고 URL은 다음과 같습니다.

www.siteurl.com/city-guide/rick-owens

제가 맞다면, 당신의 페이지에도 같은 링크 구조가 있을 것이고, 그래서 제가 맨 위에 제공한 코드가 작동할 것입니다.위의 링크와 다를 경우, 기능에 따라 변경해 주세요.php 코드입니다.예를 들어, 비디오 커스텀 투고 타입의 경우는,

www.siteurl.com/video/rick-owens

그런 다음 기능을 변경합니다.php 코드:

$structure   = 'video/%rewrite%';

비디오 URL 체크에 대해서는 img를 참조해 주세요.

시티가이드의 커스텀 포스트 타입이 있다면

www.siteurl.com/city_guide/rick-owens

$structure1   = 'city_guide/%rewrite%';

시티 가이드 URL 체크에 대해서는 img를 참조해 주세요.

이것은 또 다른 선택사항으로, 내 프로젝트에서 당신과 유사한 충돌이 발생할 경우 주로 그렇게 합니다.못 받으셨다면 이 게시물에 제 이름을 올려주세요.

이게 먹히길 바라.

갱신된 프로세스에서 갱신된 답변

이 코드를 사용하여 게시 유형을 Wordpress Core 기능으로 업데이트하십시오.

function update_parse_request( $query ) {

    if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
        return;
    }

    if ( ! empty( $query->query['name'] ) ) {
        $query->set( 'post_type', array( 'post', 'page', 'video', 'rewrite' ) );
    }
}
add_action( 'pre_get_posts', 'update_parse_request' );

이 코드가 잘 작동하기를 바라며 앞서 말씀드린 바와 같이 코드를 삭제하고 permalink를 업데이트 하는 것을 잊지 마십시오.

감사해요.

메모: 코드를 함수에 넣으면 permalink를 업데이트하는 것을 잊지 마십시오.php

Wordpress permalinks 설정에서 /%category%/%postname%/와 같은 커스텀 구조를 넣은 다음 첫 번째 인터뷰 포스트에서 yast seo의 기어 탭을 주목하십시오./rick-owens/를 Canonical URL로 지정할 수 있습니다.

언급URL : https://stackoverflow.com/questions/42153635/custom-posts-with-same-slug-redirecting-to-wrong-post-with-same-slug

반응형