需要添加重写规则,将有关帖子的附加信息添加到URL

时间:2012-08-19 作者:Matt

A有一个职位类型venues 具有以下属性title, state, 和suburb. 目前使用默认WordPress permalinks,我得到以下格式:

mysite.com/venue/{title}

但是,我希望完整的url为:

mysite.com/venue/{state}/{suburb}/{title}

这些信息可以从数据库中检索,永久链接的url格式如下:

mysite.com/?venue=title

我相信也有可能通过身份证找到一些方法。。。

我的问题是,如何创建一个重写规则,根据post_idtitle 要创建此格式的url吗?

有可能吗?

1 个回复
SO网友:Manny Fleurmond

对不起,我花了这么长时间才写出来。希望它仍然有用。

这是基于找到的教程here 还有一些将军在附近玩耍。

<?php

//This function sets up the permastructure
add_action(\'init\', \'setup_permastruct\', 40);

function setup_permastruct() {
    //Setup query vars
    add_rewrite_tag(\'%state%\',\'([^/]+)\');
    add_rewrite_tag(\'%suburb%\',\'([^/]+)\');  
    
    //Permastruct for custom post type
    add_permastruct(\'venue\', \'venue/%state%/%suburb%/%postname%\', false);   
}

//This one fills in the blanks of your permastructure so that when WP makes a link for a venue, it creates it with all the right info
add_filter(\'post_type_link\', \'create_permalink\', 10, 4);

function create_permalink($permalink, $post_id, $leavename, $sample) {
    $post = get_post($post_id);
    $rewritecode = array(
        \'%year%\',
        \'%monthnum%\',
        \'%day%\',
        \'%hour%\',
        \'%minute%\',
        \'%second%\',
        $leavename? \'\' : \'%postname%\',
        \'%post_id%\',
        \'%category%\',
        \'%author%\',
        $leavename? \'\' : \'%pagename%\',
        \'%state%\',
        \'%suburb%\'
    );
    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));
        
        //Your custom data
        $state = get_post_meta($post_id, \'state_meta\', true);
        $suburb = get_post_meta($post_id, \'suburb_meta\', true);;
        
        //Enter permalink manipulations here            
        $rewritereplace = array(
            $date[0],
            $date[1],
            $date[2],
            $date[3],
            $date[4],
            $date[5],
            $post->post_name,
            $post->ID,
            $category,
            $author,
            $post->post_name,
            $state,
            $suburb
            //Add custom tag replacements here
        );
        $permalink = str_replace($rewritecode, $rewritereplace, $permalink);
    }
    return $permalink;      
}
需要注意的关键是在第二个函数中,我为您的自定义数据提供了注释。替换的第二个参数get_post_meta 使用各自自定义元数据的名称调用函数。插入代码后,转到wp admin>;设置(>);永久链接并单击保存以刷新您的重写,瞧。

如果你需要澄清什么,请告诉我。

结束

相关推荐