【发布时间】:2018-12-27 16:19:11
【问题描述】:
我在一个旅游网站工作。他们有一个使用自定义插件构建的自定义帖子类型。 该自定义帖子类型具有类似“/destination/”.$post->post_name 之类的基本信息。 他们想让我删除那个基本的蛞蝓,这样它就只能是 $post->post_name
我尝试了下面列出的互联网代码。
它适用于该目的地的单层。 但是当我在美国有一个像纽约这样的父母目的地时。 所以它不起作用。 这是一个例子:
function update_destination_page_link_filter($post_link, $post, $leavename = null, $sample = null ) {
if ( $post->post_type === 'destination' ) {
$post_link = str_replace('/destination/', '/', $post_link);
if($post->post_parent !== 0){
$parent_slug = get_parent_link($post->post_parent, '');
$post_link = '/'.$parent_slug.$post->post_name;
}
$post_link = update_url_base( $post, $post_link );
}
return $post_link;
}
add_filter( 'post_type_link', 'update_destination_page_link_filter', 1, 3 );
function allow_destination_direct_by_name( $query ) {
// Only noop the main query
if ( ! $query->is_main_query() )
return;
// Only noop our very specific rewrite rule match
if ( 2 != count( $query->query )
|| ! isset( $query->query['page'] ) )
return;
// 'name' will be set if post permalinks are just post_name, otherwise the page rule will match
if ( ! empty( $query->query['name'] ) )
$query->set( 'post_type', array( 'post', 'destination', 'page' ) );
}
add_action( 'pre_get_posts', 'allow_destination_direct_by_name', 1);
单层 http://siteurl/united-state-america 效果很好 http://siteurl/united-state-america/new-york 不工作。它应该打开纽约位置页面,但显示 404 它也可能在位置上更规范 赞http://siteurl/united-state-america/new-york/brooklyn
【问题讨论】:
标签: wordpress .htaccess mod-rewrite url-rewriting custom-post-type