下面的代码应该可以工作,但不能工作(没有激活插件)。在后端,将正确显示自定义帖子类型的永久链接,但如果在前端调用,将显示不正确的帖子。
但是,如果永久链接被禁用,它会工作:
// register post type
add_action(\'init\',\'register_item\');
function register_item() {
register_post_type( \'hebamme\',
array(
\'labels\' => array(
\'name\' => __( \'Hebammen\' ), //this name will be used when will will call the investments in our theme
\'singular_name\' => __( \'Hebamme\' ),
),
\'description\' => \'\',
\'public\' => true,
\'show_ui\' => true,
\'hierarchical\' => false, //it means we cannot have parent and sub pages
\'capability_type\' => \'post\', //will act like a normal post
\'rewrite\' => array(\'slug\' => \'hebammen\',\'with_front\'=>true), //this is used for rewriting the permalinks
\'query_var\' => true,
\'supports\' => array( \'title\', \'editor\', \'revisions\',\'custom-fields\'), //the editing regions that will support
\'menu_position\' => 5
)
);
}
// Permalinks
add_action(\'init\', \'smc_add_rewrite_rules\');
function smc_add_rewrite_rules() {
// Register custom rewrite rules
global $wp_rewrite;
$wp_rewrite->add_rewrite_tag(\'%location%\', \'([^/]+)\', \'location=\');
$wp_rewrite->add_rewrite_tag(\'%pmcustom%\', \'([^/]+)\', \'pmcustom=\');
$wp_rewrite->add_permastruct(\'hebamme\', \'/hebammen/%location%/%pmcustom%/\',false);
}
add_filter(\'post_type_link\', \'smc_permalinks\', 10, 3);
function smc_permalinks($permalink, $post, $leavename) {
$no_data = \'na\';
$post_id = $post->ID;
if($post->post_type != \'hebamme\' || empty($permalink) || in_array($post->post_status, array(\'draft\', \'pending\', \'auto-draft\'))) {
return $permalink;
}
$var1 = get_post_meta($post_id, \'sc_heb_city\', true);
$var1 = sanitize_title($var1);
if(!$var1) {
$var1 = $no_data;
}
return str_replace(\'%pmcustom%\',$post->post_name,str_replace(\'%location%\', $var1, $permalink));
}