วิธีเปลี่ยน url จาก postname เป็น category/postname

วิธีเปลี่ยนโครงสร้าง URL ของ WordPress
/%postname%/ เป็น /%category%/%postname%/ มีดังนี้
1.แก้ไขที่ไฟล์ function.php
2.เพิ่ม code เหล่านี้ลงไป

add_filter( '404_template', 't5_redirect_to_category' );
function t5_redirect_to_category( $template )
{
    if ( ! is_404() )
        return $template;
    global $wp_rewrite, $wp_query;
    if ( '/%category%/%postname%/' !== $wp_rewrite->permalink_structure )
        return $template;
    if ( ! $post = get_page_by_path( $wp_query->query['category_name'], OBJECT, 'post' ) )
        return $template;
    $permalink = get_permalink( $post->ID );
    wp_redirect( $permalink, 301 );
    exit;
}

ใส่ความเห็น

อีเมลของคุณจะไม่แสดงให้คนอื่นเห็น ช่องข้อมูลจำเป็นถูกทำเครื่องหมาย *

Up Next:

เก็บรูปบน Google Photo แทน iCloud

เก็บรูปบน Google Photo แทน iCloud