Yar*_*oza 5 wordpress wordpress-plugin
所以我在WordPress引擎上有网站.我希望帖子网址看起来像http://website.com/blog/post-name/或http://website.com/blog/categ-name/post-name/.但其他一切看起来都像http://website.com/page-name/.只是想为所有帖子而不是页面添加前缀"博客"到网址.我的永久链接设置是/%postname%/.如果我将/ blog /%postname%/我将在我的网站的每个URL中都有'blog'前缀:(我在WordPress管理员后端找不到解决方案,我没有找到任何可接受的插件.任何帮助非常感谢,谢谢.
1)在您结尾添加此重写 function.php
function add_rewrite_rules( $wp_rewrite )
{
$new_rules = array(
'blog/(.+?)/?$' => 'index.php?post_type=post&name='. $wp_rewrite->preg_index(1),
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'add_rewrite_rules');
function change_blog_links($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', 'change_blog_links', 1, 3);
Run Code Online (Sandbox Code Playgroud)
2)转到设置>永久链接,然后单击Save Changes。
我这样做了:
add_action( 'init', 'redefine_post', 1 );
function redefine_post() {
register_post_type( 'post', array(
'labels' => array(
'name_admin_bar' => _x( 'Post', 'add new on admin bar' ),
),
'public' => true,
'_builtin' => false,
'_edit_link' => 'post.php?post=%d',
'capability_type' => 'post',
'map_meta_cap' => true,
'hierarchical' => false,
'rewrite' => array( 'slug' => 'blog' ),
'query_var' => false,
'has_archive' => true,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ),
) );
}
Run Code Online (Sandbox Code Playgroud)
它涉及创建自定义帖子类型(这有点烦人),但它会覆盖并与帖子一起使用。
我希望有更好的方法来做到这一点!(否则,如果您只是更改永久链接,您会在所有内容前面看到/blog/)。
转到“设置”>“永久链接”,选择“自定义结构”并将此字段重写为“/blog/%postname%/”。您的帖子将获得 /blog/ 前缀,但您的页面则不会。我在 WordPress 5.2.2 上测试过
| 归档时间: |
|
| 查看次数: |
3433 次 |
| 最近记录: |