自定义POST类型url重写中的多个参数

时间:2012-09-20 作者:Matt

我有一个自定义的帖子类型,叫做venues 当前它们通过url格式显示:

http://mywebsite.com/venue/{venue-name}
我想要格式:

http://mywebsite.com/venue/{venue-suburb}/{venue-name}
但是郊区实际上与venue 自定义帖子类型。名称总是唯一的。

需要做的是在重写时{venue-suburb} 基于{venue-name} 或id并用其中的信息重写url。

这是可能的吗?如果可能的话,我该如何开始呢?

Update: 这是我经过一些研究后整理出来的代码。结果:http://mywebsite.com/my-venues/{venue-suburb}/{venue-name}/{post_id}

add_action(\'init\', \'myposttype_rewrite\');
function myposttype_rewrite() {
    global $wp_rewrite;
    $queryarg = \'post_type=venue&p=\';
    $wp_rewrite->add_rewrite_tag(\'%cpt_id%\', \'([^/]+)\', $queryarg);
    $wp_rewrite->add_rewrite_tag(\'%suburb%\', \'([^/]+)\', $queryarg);
    $wp_rewrite->add_rewrite_tag(\'%title%\', \'([^/]+)\', $queryarg);
    $wp_rewrite->add_permastruct(\'venue\', \'/my-venues/%suburb%/%title%/%cpt_id%\', false);
}

add_filter(\'post_type_link\', \'venue_permalink\', 1, 3);
function venue_permalink($post_link, $id = 0, $leavename) {
    global $wp_rewrite;
    global $wpdb;

    $post = &get_post($id);
    if ( is_wp_error( $post ) )
        return $post;

    $newlink = $wp_rewrite->get_extra_permastruct(\'venue\');

    $sql = "
        SELECT meta_value 
        FROM " . $wpdb->postmeta . " 
        WHERE post_id = " . $post->ID . " AND meta_key = \'address\'";
    $address = $wpdb->get_var( $sql );
    $address = unserialize( $address );
    $suburb = str_replace(" ", "-", strtolower($address[\'suburb\']));
    $title = str_replace(" ", "-", strtolower(get_the_title( $post->ID )));

    $newlink = str_replace("%suburb%", $suburb, $newlink);
    $newlink = str_replace("%title%", $title, $newlink);
    $newlink = str_replace("%cpt_id%", $post->ID, $newlink);
    $newlink = home_url(user_trailingslashit($newlink));
    return $newlink;
}
删除%cpt\\u id%的节会使页面显示为404。将%cpt\\u id%重写为“”也会使页面显示为404。有人知道如何删除post\\u id吗?

1 个回复
SO网友:Milo

可能最简单的方法之一是venue suburb 非层次自定义分类法。创建场馆帖子时,请为郊区添加一个术语并将其指定给该帖子。register post type rewrite参数包含对%venue_suburb% 标记,然后您可以筛选post_type_link 要将该标签替换为选定的场馆郊区术语:

add_action( \'init\', \'wpa65770_venue_custom_types\' );

function wpa65770_venue_custom_types() {

    // extra parameters for tax and post type trimmed to save space
    register_taxonomy( \'venue_suburb\',\'venue\',
        array(
            \'hierarchical\' => false,
            \'show_ui\' => true,
            \'query_var\' => true,
            \'rewrite\' => array( \'slug\' => \'venue-suburb\' ),
        )
    );

    register_post_type( \'venue\',
        array(
            \'public\' => true,
            \'query_var\' => true,
            \'rewrite\' => array( \'slug\' => \'venue/%venue_suburb%\' ), // rewrite with %venue_suburb%
            \'hierarchical\' => false,
            \'supports\' => array(\'title\', \'editor\', \'thumbnail\')
        )
    );

}

add_filter( \'post_type_link\', \'wpa65770_venue_permalink\', 10, 3 );

function wpa65770_venue_permalink( $permalink, $post, $leavename ) {
    if ( false !== strpos( $permalink, \'%venue_suburb%\' )
        && $venue_suburb = get_the_terms( $post->ID, \'venue_suburb\' ) ) {
        $permalink = str_replace( \'%venue_suburb%\', array_pop( $venue_suburb )->slug, $permalink );
    }
    return $permalink;
}
创建帖子时,您将看到%venue_suburb% 标记URL,直到您将一个术语与帖子关联。添加上述代码以刷新重写规则后,请确保访问admin中的permalinks设置页面,以便所有这些都生效。

EDIT- 基于您的代码的其他版本。您必须修改它以从序列化的元中提取郊区,我刚刚用字符串val测试了地址:

add_action(\'init\', \'myposttype_rewrite\');
function myposttype_rewrite() {
    global $wp_rewrite;
    $queryarg = \'post_type=venue&venue=\';
    $wp_rewrite->add_rewrite_tag(\'%suburb%\', \'([^/]+)\', $queryarg);
    $wp_rewrite->add_permastruct(\'venue\', \'/my-venues/%suburb%/%venue%/\', false);
}

add_filter(\'post_type_link\', \'venue_permalink\', 1, 3);
function venue_permalink($post_link, $post_obj, $leavename) {

    $post = &get_post($post_obj);
    if ( is_wp_error( $post ) )
        return $post;

    $address = get_post_meta( $post_obj->ID, \'address\', true );
    $newlink = str_replace( \'%suburb%\', $address, $post_link );
    return $newlink;
}

结束

相关推荐