Xóa Parent Page Trong Đường Dẫn Slug Của WordPress Permalink

Xóa parent page trong đường dẫn Slug của WordPress permalink

WordPress mặc định có 2 dạng tạo và và lưu trữ nội dung chính là “Post” và “Page”. Ở dạng “Post” nếu muốn loại bỏ “category” có trong đường dẫn thì bạn chỉ cần tùy chỉnh permalink trong Setting của trình quản trị là xong, nhưng với dạng “Page” thì hơi gian nan chút. Bài viết sau đây ItVN Blog sẽ hướng dẫn bạn xóa parent page trong đường dẫn slug của wordpress nhé.

Cách xóa parent page trong đường dẫn (slug)

Ví dụ bạn muốn biến site.com/parent-page/child-page thành site.com/child-page Bạn sẽ can thiệp vào function để remove “parent page” slug đi. Cách làm này khác với một số cách thông thường, chúng lạm dụng phương pháp redirect (301) để điều hướng url, điều này gây nhiều phiền toái về sau nếu bạn làm SEO hoặc chuyển giao diện…

wordpres parent page
WordPress parent page

Dễ dàng nhất là bạn copy đoạn này và thêm vào function.php của theme hoặc child-theme là xong:


function wpse_182006_pages_permalink( $link, $post_id) {

// get the post_type and evaluate
$posttype = get_post_field( 'post_type', $post_id, 'display' );
if ($posttype !== 'page'){
return link;
}

// build the uri from the slug
$slugname = get_post_field( 'post_name', $post_id, 'display' );
$slugname = $slugname."/";
$link = home_url($slugname);
return $link;

}

add_filter( 'page_link', 'wpse_182006_pages_permalink', 10, 3 );

Nguồn: chuotbu.com/cach-xoa-parent-page-trong-duong-dan-slug-cua-wordpress-permalink

Theo dõi
Thông báo của
guest
0 Góp ý
Phản hồi nội tuyến
Xem tất cả bình luận