Wordpress自定义类型永久链接与自定义分类法slug

时间:2014-07-07 18:08:46

标签: wordpress custom-post-type permalinks taxonomy

我的网站上有一个名为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);

1 个答案:

答案 0 :(得分:0)

您需要为,'%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/

正确。 有关详细信息,您还可以在Creating Custom WordPress Rewrite Rules for Pretty Permalinks

上查看此博文