自定义帖子类型固定链接%CATEGOR%

时间:2013-05-18 作者:Muhammad Sajid

我创建了新的帖子类型和分类法。以下是脚本:

add_action(\'init\', \'my_custom_post_type_init\');
function my_custom_post_type_init()
{
  $labels = array(
    \'name\' => _x(\'Roundabouts\', \'post type general name\'),
    \'singular_name\' => _x(\'Roundabout\', \'post type singular name\'),
    \'add_new\' => _x(\'Add New\', \'Roundabout\'),
    \'add_new_item\' => __(\'Add New Roundabout\'),
    \'edit_item\' => __(\'Edit Roundabout\'),
    \'new_item\' => __(\'New Roundabout\'),
    \'view_item\' => __(\'View Roundabout\'),
    \'search_items\' => __(\'Search Roundabouts\'),
    \'not_found\' =>  __(\'No Roundabouts found\'),
    \'not_found_in_trash\' => __(\'No Roundabouts found in Trash\'),
    \'parent_item_colon\' => \'\'
  );
  $args = array(
    \'labels\' => $labels,
    \'public\' => true,
    \'publicly_queryable\' => true,
    \'show_ui\' => true,
    \'query_var\' => true,
    \'rewrite\' => true,
    \'capability_type\' => \'post\',
    \'hierarchical\' => false,
    \'menu_position\' => 5,
    \'rewrite\' => array(\'slug\' => \'%category%\', \'with_front\' => FALSE),
    \'taxonomies\' => array( \'post_tag\', \'category\'),
    \'supports\' => array(\'title\',\'editor\',\'author\',\'thumbnail\',\'excerpt\',\'comments\', \'thumbnail\')
  );
  register_post_type(\'roundabouts\',$args);
}
这是永久链接设置,我不允许更改。

/%category%/%postname%/
现在我需要生成permalink,如:

http://my_site/category_name/post_title
虽然我在谷歌上找到了许多文章,但问题如下:

  • Permalinks 404 with custom post type

  • Custom Permalinks for Custom Post Types in WordPress 3.0+

  • custom post type with post_id in permalink structure

    这会以某种方式将我的post permalink转换为我所需要的,但如果我点击了该链接,就会出现404错误。

    下面是最适合我的上述代码的修改后的脚本:

    // Add filter to plugin init function
    add_filter(\'post_type_link\', \'gallery_permalink\', 10, 3);  
    // Adapted from get_permalink function in wp-includes/link-template.php
    function gallery_permalink($permalink, $post_id, $leavename) {
        $post = get_post($post_id);
    
        $rewritecode = array(
            \'%year%\',
            \'%monthnum%\',
            \'%day%\',
            \'%hour%\',
            \'%minute%\',
            \'%second%\',
            $leavename? \'\' : \'%postname%\',
            \'%post_id%\',
            \'%category%\',
            \'%author%\',
            $leavename? \'\' : \'%pagename%\',
        );
    
        if ( \'\' != $permalink && !in_array($post->post_status, array(\'draft\', \'pending\', \'auto-draft\')) ) {
    
            $unixtime = strtotime($post->post_date);
    
            $category = \'\';
            if ( strpos($permalink, \'%category%\') !== false ) {
    
                $cats = get_the_category($post->ID);
                if ( $cats ) {
                    usort($cats, \'_usort_terms_by_ID\'); // order by ID
                    $category = $cats[0]->slug;
                    if ( $parent = $cats[0]->parent )
                        $category = get_category_parents($parent, false, \'/\', true) . $category;
                }
                // show default category in permalinks, without
                // having to assign it explicitly
                if ( empty($category) ) {
                    $default_category = get_category( get_option( \'default_category\' ) );
                    $category = is_wp_error( $default_category ) ? \'\' : $default_category->slug;
                }
            }
    
            $author = \'\';
            if ( strpos($permalink, \'%author%\') !== false ) {
                $authordata = get_userdata($post->post_author);
                $author = $authordata->user_nicename;
            }
    
            $date = explode(" ",date(\'Y m d H i s\', $unixtime));
            $rewritereplace =
            array(
                $date[0],
                $date[1],
                $date[2],
                $date[3],
                $date[4],
                $date[5],
                $post->post_name,
                $post->ID,
                $category,
                $author,
                $post->post_name,
            );
            $permalink = str_replace($rewritecode, $rewritereplace, $permalink);
        } else { // if they\'re not using the fancy permalink option
        }
        return $permalink;
    }
    
    请指导我还能做些什么。

2 个回复
SO网友:Biranit Goren

我无法对您的脚本发表评论,但如果您更改了与永久链接相关的任何内容,建议您转到仪表板->设置->永久链接,然后单击“更新”按钮。这会告诉Wordpress刷新其重写规则。如果您的脚本是正确的,那么在执行“更新”之后,您将不会得到404个错误。

SO网友:Muhammad Sajid

多亏了sanchothefat提供的这个插件,它才真正解决了我的问题。

WP Permastructure

结束

相关推荐

WP_LIST_CATEGORIES,将类添加到具有子项的所有列表项

我正在使用wp_list_categories(); 要显示自定义分类法中所有术语的列表,但我需要为具有子级的列表项设置与不具有子级的列表项不同的样式。有没有一种方法,PHP或jQuery,我可以给所有父元素一个特殊的类?