簡體   English   中英

帶有自定義分類標准的Wordpress自定義類型永久鏈接

[英]Wordpress Custom Type permalink with custom Taxonomy slug

我在我的網站上有一個名為Homes的自定義帖子類型,並帶有一個稱為homes-category的自定義分類法。 它們都正常工作,但是我在固定鏈接方面遇到了麻煩。 我需要將永久鏈接設置為homes / homes-category / pagename。 我寫了一個重寫函數,因此每次訪問房屋商品時,固定鏈接都能正確顯示,但是頁面本身卻顯示404。 我不知道是什么原因造成的,並且不知道如何解決。 如果沒有永久鏈接中的自定義分類法,則房屋項目可以正常工作,但不能與之兼容。 有人對如何解決這個問題有任何想法嗎? 我一直在尋找沒有運氣的日子。

這是我的自定義帖子類型的代碼:

register_post_type( 'Homes',
    array(
        'labels' => array(
            'name' => __( 'Homes' ),
            'singular_name' => __( 'Homes Item' ),
            'add_new_item' => __('Add New Homes Item'),
            'edit_item' => __('Edit Homes Item'),
            'new_item' => __('New Homes Item'),
        ),
        'supports' => array('title', 'thumbnail', 'editor'),
        'taxonomies' => array('homes-category'),
        'public' => true,
        'has_archive' => false,
        'show_in_nav_menus'   => TRUE,
        'show_in_menu'        => TRUE,
        'rewrite' => array(
            'slug' => 'homes/%homes-category%', 
            'with_front' => true,
            'hierarchical' => true,

           ),
    )
);

這是我的自定義分類法的代碼

register_taxonomy(
    'homes-category',
    'homes',
    array(
        'hierarchical' => true,
        'label' => __( 'Availability Category' ),
        'rewrite' => array( 
            'slug' => 'homes',
            'with_front' => false,
        ),
    )
);

這是重寫功能

function custom_post_link($post_link, $id = 0)
{
    $post = get_post($id);

    if(!is_object($post) || $post->post_type != 'homes')
{
     return $post_link;
}
    $homes = 'misc';

    if($terms = wp_get_object_terms($post->ID, 'homes-category'))
{
    $client = $terms[0]->slug;

//Replace the query var surrounded by % with the slug of 
//the first taxonomy it belongs to.
    return str_replace('%homes-category%', $homes, $post_link);
}

//If all else fails, just return the $post_link.
    return $post_link;
}

add_filter('post_type_link', 'custom_post_link', 1, 3);

您需要為'%homes-category%'添加rewrite_tag

即使您需要添加自定義rewrite_rule來正確解釋它。

您可以使用以下代碼,

add_action('init','wdm_register_post_types');

function wdm_register_post_types()
{
    global $wp_rewrite;

    register_post_type( 'Homes',
    array(
        'labels' => array(
            'name' => __( 'Homes' ),
            'singular_name' => __( 'Homes Item' ),
            'add_new_item' => __('Add New Homes Item'),
            'edit_item' => __('Edit Homes Item'),
            'new_item' => __('New Homes Item'),
        ),
        'supports' => array('title', 'thumbnail', 'editor'),
        'taxonomies' => array('homes-category'),
        'public' => true,
        'has_archive' => false,
        'show_in_nav_menus'   => TRUE,
        'show_in_menu'        => TRUE,
        'rewrite' => array(
            'slug' => 'homes/%homes-cat%', 
            'with_front' => true,
            'hierarchical' => true
           ),
    )
  );

    register_taxonomy(
    'homes-category',
    'homes',
    array(
        'hierarchical' => true,
        'label' => __( 'Availability Category' ),
        'rewrite' => array( 
            'slug' => 'homes-category',
            'with_front' => true,
        ),
    )
    );

    $wp_rewrite->add_rewrite_tag( '%homes-cat%', '[a-zA-Z0-9_]');

    add_rewrite_rule('^homes/([^/]+)/([^/]+)/?$','index.php?homes=$matches[2]','top');
}

function wp_tuts_filter_post_link( $permalink, $post ) {

    if ( false === strpos( $permalink, '%homes-cat%' ) )
        return $permalink;

    $terms =  current(wp_get_post_terms( $post->ID, 'homes-category', array('fields' => 'slugs')));

    $permalink = str_replace( '%homes-cat%', $terms , $permalink );

    return $permalink;
}
add_filter( 'post_link', 'wp_tuts_filter_post_link' , 10, 2 );

add_filter('post_type_link','wp_tuts_filter_post_link' , 10, 2 );

這將解釋為

http://domain.com/homes/2-bhk/home-1/

正確地。 有關更多詳細信息,您還可以查看有關為漂亮的永久鏈接創建自定義WordPress重寫規則的博客文章。

暫無
暫無

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

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