WordPress에서 사용자 지정 게시 유형 및 분류에 동일한 슬러그를 사용하려면 어떻게 해야 합니까?
저는 이곳에 처음 왔는데 어떻게 해결해야 할지 몰라서 당신의 도움이 필요합니다.
사용자 지정 게시물 유형인 "튜터링"을 새로 등록해야 하는데, 이는 사용자 지정 게시물에 대한 디자인을 다르게 하고, 나머지 게시물과는 별도로 보관하고 싶기 때문입니다.저도 범주별로 정리하고 싶어서 분류법을 이용하겠습니다.저는 많은 튜토리얼(여기서 그리고 구글에서)을 읽었고, 이것을 할 수 있는 많은 방법들을 찾았지만, 그 결과는 제가 기대했던 것과 다릅니다.마지막으로 플러그인으로 코드를 작성했습니다.
<?php
/*
Plugin Name: My Custom Post Types
Description: A plugin for our custom post types like tutorials etc.
*/
/*====================================================
Register new custom post type - tutorials
======================================================*/
function register_my_custom_post_type_tutorials() {
$labels = array(
'name' => 'Tutorials',
'singular_name' => 'Tutorial',
'add_new' => 'Add New',
'add_new_item' => 'Add New Tutorial',
'edit_item' => 'Edit Tutorial',
'new_item' => 'New Tutorial',
'all_items' => 'All Tutorials',
'view_item' => 'View Tutorial',
'search_items' => 'Search Tutorials',
'not_found' => 'No tutorials found',
'not_found_in_trash' => 'No tutorials found in Trash',
'parent_item_colon' => '',
'menu_name' => 'Tutorials'
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'tutorials' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments','custom-fields','page-attributes','post-formats' )
);
register_post_type( 'wizz-tutorials', $args );
}
add_action( 'init', 'register_my_custom_post_type_tutorials' );
/*====================================================
Register Custom Taxonomies for Tutorials - Categories
======================================================*/
add_action('init', 'register_tutorials_taxonomy');
function register_tutorials_taxonomy() {
// Add new taxonomy, make it hierarchical (like categories)
register_taxonomy('wizz-tutorials-category',
'wizz-tutorials',
array (
'labels' => array (
'name' => 'Tutorials Categories',
'singular_name' => 'Tutorials Categories',
'search_items' => 'Search Tutorials Categories',
'popular_items' => 'Popular Tutorials Categories',
'all_items' => 'All Tutorials Categories',
'parent_item' => 'Parent Tutorials Category',
'parent_item_colon' => 'Parent Tutorials Category:',
'edit_item' => 'Edit Tutorials Category',
'update_item' => 'Update Tutorials Category',
'add_new_item' => 'Add New Tutorials Category',
'new_item_name' => 'New Tutorials Category',
),
'hierarchical' => true,
'show_ui' => true,
'show_tagcloud' => true,
'rewrite' => array( 'slug' => 'tuts-category' ),
'public' => true
)
);
}
?>
관리자 패널에서 이제 "자습서"를 추가할 수 있습니다.저는 그들을 위해 몇 가지 카테고리(포토샵, 웹 등)도 만들었습니다.문제는 민달팽이입니다.
지금 가지고 있는 것:
mywebsite.com/tutorials/- 모든 튜토리얼 아카이브(사용)archive.php)mywebsite.com/tutorials/post-name- 자습서 표시(사용)single.php)mywebsite.com/tuts-category/photoshop/- 이 범주에 속하는 자습서만 표시합니다.
완벽하지만 뭔가 다른 걸 원해요
mywebsite.com/tutorials/mywebsite.com/tutorials/post-namemywebsite.com/tutorials/photoshop/
그러나 분류학을 위해 민달팽이에 변화가 있다면, 예를 들어,'rewrite' => array( 'slug' => 'tutorials' ), 저는 지금.404 error.
이것을 할 방법이 방법이 있습니까?그리고 또 다른 질문은, 제 코드가 맞습니까?감사합니다!
제 답변으로 100% 당신의 요청을 해결할 수는 없겠지만, 저는 많은 프로젝트에서 이 방법을 사용해 왔고 이 경우에 최선의 방법이라고 생각합니다.
우선 사이트를 백업합니다.그런 다음 모든 코드를 하나의 INIT 기능에 입력합니다. 규칙을 플러시해야 하고 등록이 끝날 때 발생하는 것이 더 좋기 때문에 중요합니다(마지막에 설명합니다).
post type args에서 chage는 다음으로_archive를 가집니다.
'has_archive' => 'tutorials' // This will tell wp to use the taxonomy tutorials for the post type archive
다시 쓰기를 다음과 같이 바꿉니다.
'rewrite' => array(
'slug' => 'tutorial', // Notice this will be the slug for single tutorial and can´t be the same as the archive slug, but has sence, since it´s ONE tutorial post, not ALL the tutorials, singlular, plural things in other words.
'with_front' => true,
),
그리고 taxonomy args에서 다음으로 다시 쓰기를 변경합니다.
'rewrite' => array( 'slug' => 'tutorials', 'with_front' => true, 'hierarchical' => false),
그런 다음 플러시를 한 번 실행하고 init 기능이 끝나면 다음을 추가합니다.
flush_rewrite_rules();
한 번만 실행한 다음 함수에서 flush_rewrite_rules를 제거합니다.이것은 단지 재건을 위한 것이기 때문에, 당신은 영원히 그것을 내버려 둘 수 없습니다.
그런 다음 영구 링크로 이동하여 저장합니다.
분류나 포스트 타입 민달팽이로 변경할 때마다 규칙을 플러시하고 영구 링크를 저장해야 합니다. 그렇지 않으면 404 오류가 발생합니다.
이렇게 하면 다음과 같은 이점을 얻을 수 있습니다.
mywebsite.com/tutorials/ (this will use the archive or taxonomy template, ej: taxonomy-tutorials.php)
mywebsite.com/tutorial/post-name (notice what i describe about single slugs, template in use: single-tutorial.php)
mywebsite.com/tutorials/photoshop/ (this will use the archive or taxonomy template as well and also you could have a particular template only for that term)
참고: 말씀드린 것처럼, 단일 게시물 유형의 민달팽이는 분류 기록의 민달팽이와 동일할 수 없으며, 단일 게시물과 분류 기록의 민달팽이를 동시에 인식하는 것은 문제가 될 것입니다. 따라서, 제가 발견한 최선의 방법은 단일 게시물에는 단일 민달팽이가 있고 세무 기록의 경우에는 복수 버전이 있는 이 방법을 사용하는 것입니다.적어도 민달팽이에는 "고양이 과외" 같은 것이 없을 것이고, 사람이 더 잘 읽을 수 있고, SEO에게 좋을 것입니다.
도움이 되길 바랍니다.
핵심은 사용자 지정 게시 유형의 재작성에 동일한 사용자 지정 분류 이름을 사용하는 것입니다.이와 같습니다."rewrite" => array( "slug" => "web-tutorials/%custom-taxonomy-name%" ),
정의 과 같습니다.custom-taxonomy-name.
여기서 찾을 수 있는 전체적인 설명과 해결책.
미래의 독자들을 위해, 맞춤형 분류와 게시물은 동일한 기본 민달팽이를 공유할 수 있습니다. 오랫동안 게시물 유형이 분류 후에 등록되었습니다.
와 CPT '' 를 분류법의 .'rewrite' => array( 'slug' => 'tutorials/category' ).
먼저 포스트 타입을 등록하고 다음 분류법을 등록하면 다음 순서로 다시 쓰기 규칙을 얻을 수 있는 순서는 다음과 같습니다.
- 자습서/(.+?)(?):/([0-9]+)?/?$
- 튜토리얼/category/([^/]+)/?$
따라서 워드 프레스는 분류 체계의 용어가 게시물이라고 생각하고 첫 번째 규칙에서 중지됩니다.
그러나 분류법을 먼저 등록한 다음 포스트 유형을 선택하면 하나의 포스트 이전에 분류 경로가 적중되어 작동합니다!
언급URL : https://stackoverflow.com/questions/21959851/how-do-i-use-the-same-slugs-for-custom-post-types-and-taxonomies-in-wordpress
'codememo' 카테고리의 다른 글
| DOM에서 JavaScript 이동 요소 (0) | 2023.10.05 |
|---|---|
| NSURL 요청 HTTP 헤더 설정 (0) | 2023.10.05 |
| Oracle 날짜를 Java 날짜로 (0) | 2023.10.05 |
| Check Constraint on Oracle의 내용을 보는 방법 (0) | 2023.10.05 |
| 자바스크립트에서 HTML 테이블 본문에 행을 삽입하는 방법 (0) | 2023.10.05 |