【问题标题】:Wordpress Custom Type permalink with custom Taxonomy slug带有自定义分类 slug 的 Wordpress 自定义类型永久链接
【发布时间】:2014-08-28 07:29:30
【问题描述】:

我的网站上有一个名为 Homes 的自定义帖子类型,其中包含一个名为 home-category 的自定义分类。它们都正常工作,但我在使用固定链接时遇到了问题。我需要永久链接是家庭/家庭类别/页面名称。我写了一个重写函数,所以每当我转到一个家庭项目时,永久链接都会正确显示,但我在页面本身上得到一个 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);

【问题讨论】:

    标签: wordpress custom-post-type permalinks taxonomy


    【解决方案1】:

    你需要为'%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上的此博客文章

    【讨论】:

      猜你喜欢
      • 2011-12-05
      • 2012-05-05
      • 1970-01-01
      • 1970-01-01
      • 2014-05-17
      • 1970-01-01
      • 2013-04-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多