【问题标题】:WP: setting with_front to false and removing post type slugWP:将 with_front 设置为 false 并删除帖子类型 slug
【发布时间】:2019-01-21 18:56:41
【问题描述】:

对于我的一种特定帖子类型,我正在尝试通过以下方式删除默认的永久链接结构:

 add_filter('register_post_type_args', function($args, $post_type) {
    if ('procedures' === $post_type ) {
      $args['rewrite']['with_front'] = false;
    }
    return $args;
 }, 99, 2);

效果很好。然后我还尝试使用以下内容从 URL 中删除帖子类型 slug:

function remove_procedures_slug($post_link, $post, $leavename) {
   if ($post->post_type != 'procedures' || $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_procedures_slug', 10, 3);

function set_procedures_url($query) {
  if(isset($query->query['post_type'])) {
    return;
  }

  if (!empty($query->query['name'])) {
    $query->set('post_type', array('post', 'procedures', 'page'));
  }
}

add_action('pre_get_posts', 'set_procedures_url');

这在删除 register_post_type_args 过滤器时可以正常工作。如何将 /blog/procedures/page-1 更改为 /page-1?

如果我这样做:

if ('procedures' === $post_type && is_array($args)) {
  $args['rewrite']['with_front'] = false;
  $args['rewrite']['slug'] = '/';
}

这可行,但会删除存档页面。任何帮助将不胜感激。

【问题讨论】:

    标签: wordpress url url-rewriting


    【解决方案1】:

    您能否使用is_post_type_archive() 检查当前请求是否针对存档?可能类似于以下内容:

    if( 'procedures' === $post_type && is_array($args) && !is_post_type_archive('procedures') ){
        $args['rewrite']['with_front'] = false;
        $args['rewrite']['slug'] = '/';
    }
    

    【讨论】:

    • 虽然这看起来可行,但它没有:(
    猜你喜欢
    • 2019-03-15
    • 1970-01-01
    • 1970-01-01
    • 2020-02-07
    • 1970-01-01
    • 1970-01-01
    • 2012-08-27
    • 2019-02-23
    • 2017-09-19
    相关资源
    最近更新 更多