簡體   English   中英

網頁的Wordpress類別

[英]Wordpress Category for pages

我在$wp_rewrite->page_structure%category%接收值時遇到問題:

function custom_page_rules() {
    global $wp_rewrite;
    $wp_rewrite->page_structure = $wp_rewrite->root .'/%category%/%pagename%'; 
    //flush_rewrite_rules();
}
add_action( 'init', 'custom_page_rules',1 );

如何在$wp_rewrite->page_structure收到%category%

現在返回: http : //example.com/%category%/the-page-slug/

而是: http : //example.com/my-cat-slug/the-page-slug/


注意頁面的類別運行良好:

http://example.com/my-cat-slug/返回該類別的所有頁面。

解決方案可能是:

function rudr_post_permalink( $url, $post ){
    if( !is_object( $post ) )
        $post = get_post( $post_id );

    $replace = $post->post_name;

    /* We should use a post ID to make a replacement. It is required if you use urf-8 characters in your URLs */

    if( $post->ID == 1 ) 
        $replace = 'hello-planet';
    if( $post->ID == 12 ) 
        $replace = 'Contacts';

    $url = str_replace($post->post_name, $replace, $url );
    return $url;
}

add_filter( 'post_link', 'rudr_post_permalink', 'edit_files', 2 );
add_filter( 'page_link', 'rudr_post_permalink', 'edit_files', 2 );
add_filter( 'post_type_link', 'rudr_post_permalink', 'edit_files', 2 );

在這里解釋

您可以將if( $post->ID == 1 )更改為所需的任何內容。

我已經更改了整個代碼,以在頁面是URL時添加類別。

問候,丹尼

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM