繁体   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