自定义帖子类型:从包含/ newses /的帖子中删除slug,但保持页面为slug / newses

时间:2016-11-21 11:24:30

标签: php wordpress url url-rewriting custom-post-type

我想删除slug' newses'来自自定义帖子类型,例如:

example.com/lorem-ipsum代替example.com/newses/lorem-ipsum

但是当我使用this method

时,我也有页面example.com/newses无法正常工作

有可能吗?

使用此代码newses / lorem-ipsum和其他帖子不起作用。

 register_post_type('newses', array(
  'labels' => array(
  'name' => __('Newses'),
  'singular_name' => __('News'),
  'meta_value' => "0"
  ),
'public' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'show_ui' => true,
'query_var' => true,
'capability_type' => 'post',
'hierarchical' => false,
'has_archive' => true,
'with_front' => false,
'supports' => array('title', 'editor', 'thumbnail')
));

function remove_my_post_type($post_link, $post, $leavename) {
  if ($post->post_type != 'newses' || $post->post_status !=  'publish') {
    return $post_link;
  }
  $post_link = str_replace('/' . $post->post_type . '/', '/',    $post_link);
  return $post_link;
  }
add_filter('post_type_link', 'remove_my_post_type', 10, 3);


function set_my_post($query) {
  if(isset($query->query['post_type'])){
    return;
  }
  if (!empty($query->query['name'])) {
    $query->set('post_type', array('post', 'newses', 'page'));
  }
 }
 add_action('pre_get_posts', 'set_my_post');

0 个答案:

没有答案