2016-01-14 1 views

Répondre

0

Il existe deux méthodes pour ce faire,

Méthode 1

// Update the post-type post and then add your custom slug 
add_action('init', 'update_default_post_slug', 1); 
function update_default_post_slug() { 
    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' => 'blogs'), 
     'query_var' => false, 
     'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats'), 
    )); 
} 

Méthode 2

Vérifiez ceci: Different permalink structure for blog posts than pages in Wordpress?

J'espère que cette aide.

+0

La méthode 1 son travail mais quand l'url montre "blogs /" l'url n'existe pas, apparait erreur 404 ... – user3810167

0

Ajoutez le code suivant à la fin de votre fichier functions.php. Si le fichier functions.php de votre thème a?> À la fin, alors collez le code juste avant?> Sinon, collez-le. Après cette mise à jour permalink de wordpress admin.

/** 
    * Add new rewrite rule 
    */ 
    function create_new_url_querystring() { 
     add_rewrite_rule(
      'blog/([^/]*)$', 
      'index.php?name=$matches[1]', 
      'top' 
     ); 
     add_rewrite_tag('%blog%','([^/]*)'); 
    } 
    add_action('init', 'create_new_url_querystring', 999); 
    /** 
    * Modify post link 
    * This will print /blog/post-name instead of /post-name 
    */ 
    function append_query_string($url, $post, $leavename) { 
     if ($post->post_type == 'post') {  
      $url = home_url(user_trailingslashit("blog/$post->post_name")); 
     } 
     return $url; 
    } 
    add_filter('post_link', 'append_query_string', 10, 3); 
    /** 
    * Redirect all posts to new url 
    * If you get error 'Too many redirects' or 'Redirect loop', then delete everything below 
    */ 
    function redirect_old_urls() { 
     if (is_singular('post')) { 
      global $post; 
      if (strpos($_SERVER['REQUEST_URI'], '/blog/') === false) { 
       wp_redirect(home_url(user_trailingslashit("blog/$post->post_name")), 301); 
       exit(); 
      } 
     } 
    } 
    add_filter('template_redirect', 'redirect_old_urls');