仅更改帖子类型‘POST’的固定链接

时间:2018-06-05 作者:Mihaela Grigore

像许多其他人一样,我希望拥有:域。com/post标题已更改为域。com/blog/post title但仅适用于“post”类型的帖子,不适用于“page”,尤其不适用于自定义帖子类型(我的主题似乎有很多)。

我已经在这个论坛和其他来源上做了研究,我知道一般的答案似乎是:

注册帖子类型时,rewrite的with\\u front参数应为false。

$args = array(
    \'rewrite\' => array( \'with_front\' => false ),
); 
register_post_type( \'your-post-type\', $args );
不幸的是,这对初学者没有帮助。我们不知道上面的说明是什么意思。显然,我们应该以某种方式重新注册默认的post类型“post”(虽然post类型“post”已经存在并正在使用),但我们不知道如何以及在哪里进行注册。如果有人能解释一下更改博客帖子URL的必要程序,我将不胜感激。

3 个回复
SO网友:Zeth

我找到了答案here. 别忘了进去,给它一个赞。

我会把它贴在这里,给匆忙的人看。

将其放入functions.php-文件:

function wp1482371_custom_post_type_args( $args, $post_type ) {
  if ( $post_type == "post" ) {
    $args[\'rewrite\'] = array(
      \'slug\' => \'blog\'
    );
  }

  return $args;
}
add_filter( \'register_post_type_args\', \'wp1482371_custom_post_type_args\', 20, 2 );
(已测试并运行)。

记住

Remember A) 记住在之后更新你的永久链接(通过进入Settings >> Permalinks >> Click \'Save Changes\' ).

Remember B) 如果你得到了wierd结果,那么试着打开一个匿名窗口,看看它是否有文字。WordPress具有重定向到的功能\'Nearest Matching URL\', 在玩permalinks的时候,这看起来很混乱。

你也可以尝试找到一个插件来实现这一点。我不会这么做,因为仅仅为了这个目的添加插件是相当广泛的。但是,嘿,有时用大炮射杀鸟是令人满意的(没有鸟因为这个笑话而受到伤害)。

SO网友:Rajilesh Panoli

你试过这个吗?

function generate_rewrite_rules( $wp_rewrite ) {
      $new_rules = array(
        \'(.?.+?)/page/?([0-9]{1,})/?$\' => \'index.php?pagename=$matches[1]&paged=$matches[2]\',
        \'blog/([^/]+)/?$\' => \'index.php?post_type=post&name=$matches[1]\',
        \'blog/[^/]+/attachment/([^/]+)/?$\' => \'index.php?post_type=post&attachment=$matches[1]\',
        \'blog/[^/]+/attachment/([^/]+)/trackback/?$\' => \'index.php?post_type=post&attachment=$matches[1]&tb=1\',
        \'blog/[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$\' => \'index.php?post_type=post&attachment=$matches[1]&feed=$matches[2]\',
        \'blog/[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$\' => \'index.php?post_type=post&attachment=$matches[1]&feed=$matches[2]\',
        \'blog/[^/]+/attachment/([^/]+)/comment-page-([0-9]{1,})/?$\' => \'index.php?post_type=post&attachment=$matches[1]&cpage=$matches[2]\',     
        \'blog/[^/]+/attachment/([^/]+)/embed/?$\' => \'index.php?post_type=post&attachment=$matches[1]&embed=true\',
        \'blog/[^/]+/embed/([^/]+)/?$\' => \'index.php?post_type=post&attachment=$matches[1]&embed=true\',
        \'blog/([^/]+)/embed/?$\' => \'index.php?post_type=post&name=$matches[1]&embed=true\',
        \'blog/[^/]+/([^/]+)/embed/?$\' => \'index.php?post_type=post&attachment=$matches[1]&embed=true\',
        \'blog/([^/]+)/trackback/?$\' => \'index.php?post_type=post&name=$matches[1]&tb=1\',
        \'blog/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$\' => \'index.php?post_type=post&name=$matches[1]&feed=$matches[2]\',
        \'blog/([^/]+)/(feed|rdf|rss|rss2|atom)/?$\' => \'index.php?post_type=post&name=$matches[1]&feed=$matches[2]\',
        \'blog/page/([0-9]{1,})/?$\' => \'index.php?post_type=post&paged=$matches[1]\',
        \'blog/[^/]+/page/?([0-9]{1,})/?$\' => \'index.php?post_type=post&name=$matches[1]&paged=$matches[2]\',
        \'blog/([^/]+)/page/?([0-9]{1,})/?$\' => \'index.php?post_type=post&name=$matches[1]&paged=$matches[2]\',
        \'blog/([^/]+)/comment-page-([0-9]{1,})/?$\' => \'index.php?post_type=post&name=$matches[1]&cpage=$matches[2]\',
        \'blog/([^/]+)(/[0-9]+)?/?$\' => \'index.php?post_type=post&name=$matches[1]&page=$matches[2]\',
        \'blog/[^/]+/([^/]+)/?$\' => \'index.php?post_type=post&attachment=$matches[1]\',
        \'blog/[^/]+/([^/]+)/trackback/?$\' => \'index.php?post_type=post&attachment=$matches[1]&tb=1\',
        \'blog/[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$\' => \'index.php?post_type=post&attachment=$matches[1]&feed=$matches[2]\',
        \'blog/[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$\' => \'index.php?post_type=post&attachment=$matches[1]&feed=$matches[2]\',
        \'blog/[^/]+/([^/]+)/comment-page-([0-9]{1,})/?$\' => \'index.php?post_type=post&attachment=$matches[1]&cpage=$matches[2]\',
      );
      $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
    }
    add_action( \'generate_rewrite_rules\', \'generate_rewrite_rules\' );

    function update_post_link( $post_link, $id = 0 ) {
      $post = get_post( $id );
      if( is_object( $post ) && $post->post_type == \'post\' ) {
        return home_url( \'/blog/\' . $post->post_name );
      }
      return $post_link;
    }
    add_filter( \'post_link\', \'update_post_link\', 1, 3 );

SO网友:Harshal Solanki
//Place code in function.php 
add_action(\'init\', \'my_new_default_post_type\', 1); 
function my_new_default_post_type() 
{ 
register_post_type(\'post\', 
array(
// \'labels\' => $labels, 
\'public\' => true, 
//\'show_in_admin_bar\' => true,
\'_builtin\' => false, 
\'_edit_link\' => \'post.php?post=%d\', 
\'capability_type\' => \'post\', 
\'map_meta_cap\' => true, 
\'hierarchical\' => false, 
\'rewrite\' => array(\'slug\' => \'resources/blog\'), // Add your slug here 
\'query_var\' => false, 
\'supports\' => array(\'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'trackbacks\', \'custom-fields\', \'comments\', \'revisions\', \'post-formats\'),
)); 
/* If you add above code in function.php, Your blog pagination is stopped to working, You need to add rewrite rule using "Debug This" Plugin under Query >> Rewrite Section & modify your rewrite_rule. */ 
add_rewrite_rule(\'(.?.+?)/page/?([0-9]{1,})/?$\', \'index.php?pagename=resources/blog&paged=$matches[1]\', \'top\'); }
结束

相关推荐

自定义帖子类型‘文章’忽略POSTS_PER_PAGE,还是保留POST_TYPE?

我头痛!我有许多不同的帖子类型,默认的帖子/页面/附件+一些使用默认CPT UI设置创建的帖子。其中只有一篇文章(WP\\u查询)行为不端posts_per_page 参数被忽略。除了上面提到的惯常嫌疑犯之外,我找不到任何关于保留岗位人员的信息。因此,我们有一堆post\\u type=文章。为了测试,我进入数据库,将其中3个更改为文章,删除了。虽然我还没有在CPT IU或函数中创建post类型,但WP\\u Query可以返回它。。。正确无误! $a_articles__featu