我只想让两个自定义的post类型元属性作为URL的标识符。我实现了一个解决方案,但它只是404的
注册职位类型:
final private static function register_post_types ()
{
$labels = array(
\'name\' => _x( \'Properties\', \'post type general name\', \' propertystreambootstrap\' ),
\'singular_name\' => _x( \'Property\', \'post type singular name\', \' propertystreambootstrap\' ),
\'menu_name\' => _x( \'Properties\', \'admin menu\', \' propertystreambootstrap\' ),
\'name_admin_bar\' => _x( \'Property\', \'add new on admin bar\', \' propertystreambootstrap\' ),
\'add_new\' => _x( \'Add New\', \'property\', \' propertystreambootstrap\' ),
\'add_new_item\' => __( \'Add New Property\', \' propertystreambootstrap\' ),
\'new_item\' => __( \'New Property\', \' propertystreambootstrap\' ),
\'edit_item\' => __( \'Edit Property\', \' propertystreambootstrap\' ),
\'view_item\' => __( \'View Property\', \' propertystreambootstrap\' ),
\'all_items\' => __( \'All Properties\', \' propertystreambootstrap\' ),
\'search_items\' => __( \'Search Properties\', \' propertystreambootstrap\' ),
\'parent_item_colon\' => __( \'Parent Properties:\', \' propertystreambootstrap\' ),
\'not_found\' => __( \'No properties found.\', \' propertystreambootstrap\' ),
\'not_found_in_trash\' => __( \'No properties found in Trash.\', \' propertystreambootstrap\' )
);
$args = array(
\'labels\' => $labels,
\'description\' => __( \'Properties for your website.\', \' propertystreambootstrap\' ),
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'menu_icon\' => \'dashicons-building\',
\'query_var\' => true,
//\'rewrite\' => array( \'slug\' => \'malta-property/%department%/%location%/%reference%\' ),
\'rewrite\' => array( \'slug\' => \'malta-property/%location%/%reference%\'),
\'has_archive\' => \'about-cool-post-types\',
\'capability_type\' => \'post\',
\'has_archive\' => false,
\'hierarchical\' => false,
\'menu_position\' => null,
\'supports\' => array(\'title\', \'editor\', \'excerpt\', \'post-thumbnails\', \'thumbnail\')
);
register_post_type( \'property\', $args );
}
Url重写:
final public static function url_rewrite()
{
global $wp_rewrite;
$wp_rewrite->add_rewrite_tag(\'%reference%\', \'([^&]+)\', \'reference=\');
$wp_rewrite->add_rewrite_tag(\'%location%\', \'([^&]+)\', \'location=\');
return null;
}
过滤器功能:
final public static function permalink_structure ($permalink, $post, $leavename)
{
if (false !== strpos($permalink, \'%reference%\')) {
$reference = get_post_meta($post->ID, \'propertystream_agent_ref\', true);
$location = wp_get_post_terms($post->ID, \'location\');
$location = str_replace(\'-cat\', \'\', $location[0]->slug);
// die($location);
// $department = wp_get_post_terms($post->ID, \'department\');
$rewritecode = array(
\'%reference%\',
\'%location%\',
// \'%department%\',
$post->post_name,
);
$rewritereplace = array(
$reference,
$location,
// $department,
$leavename? $post->post_name : \'\',
);
$permalink = str_replace($rewritecode, $rewritereplace, $permalink);
}
return $permalink;
}
这一切都生成了我想要的代码,但它只是404的-为了让它工作,我必须添加
$leavename = true
但这会将postname添加到URL,我不希望这样。
最合适的回答,由SO网友:Sally CJ 整理而成
修复了404错误,因为([^&]+)
在您的add_rewrite_tag()
电话:
$wp_rewrite->add_rewrite_tag(\'%reference%\', \'([^&]+)\', \'reference=\');
//$wp_rewrite->add_rewrite_tag(\'%location%\', \'([^&]+)\', \'location=\');
要修复错误,应使用
([^/]+)
而不是
([^&]+)
.
我还特意注释掉了第二行,因为如果分类法的slug是location
, 那么WordPress应该已经添加了%location%
重写标签。
从permalink中移除post-slug,您不应该从permalink_structure()
方法,我假设它与post_type_link
滤器
相反,您可以很容易地删除slug,如下所示:(WordPress默认附加/%<post type slug>%
到$permastruct[\'struct\']
, 因此,下面的代码删除了后段塞标识符)
// Add this after the post type has been registered (after the register_post_type() call).
global $wp_rewrite;
$permastruct = $wp_rewrite->extra_permastructs[\'property\'];
$permastruct[\'struct\'] = str_replace( \'/%property%\', \'\', $permastruct[\'struct\'] );
$wp_rewrite->extra_permastructs[\'property\'] = $permastruct;
在
permalink_structure()
方法
$rewritecode
不应包括后段塞:
$rewritecode = array(
\'%reference%\',
\'%location%\',
);
$rewritereplace = array(
$reference,
$location,
);
根据请求/页面路径中的“引用”部分定位正确的“属性”/post下面的代码非常简单,但如果您有任何问题,请告诉我:
add_action( \'parse_request\', function( $wp ){
// Check if the request/page path begins with /malta-property/<location>/<reference>
if ( preg_match( \'#^malta-property/([^/]+)/([^/]+)#\', $wp->request, $matches ) ) {
$posts = get_posts( [
\'post_type\' => \'property\',
\'meta_key\' => \'propertystream_agent_ref\',
\'meta_value\' => $matches[2],
\'posts_per_page\' => 1,
] );
if ( ! empty( $posts ) ) {
$wp->query_vars[\'name\'] = $posts[0]->post_name;
$wp->query_vars[\'post_type\'] = \'property\'; // must set post type
}
}
} );
记住刷新重写规则-只需访问“永久链接设置”页面。
此外,正如我在评论中提到的,我假设每个“属性”帖子都有一个名为propertystream_agent_ref
价值是独一无二的