web-dev-qa-db-fra.com

Masquer sans catégorie quand aucune catégorie n'est définie

J'ai la structure permalien suivante:

/%category%/%year%/%monthnum%/%postname%

J'utilise le code que j'ai trouvé ici pour supprimer le slug "non catégorisé" lorsque je ne mets aucune catégorie.

Les URL sont bien générées, mais lorsque je les visite, j'en reçois 404. On dirait qu'il me manque une réécriture ou un élément similaire?

Je suis sur WordPress 4.3.1 et le code que j'ai placé dans mon functions.php est:

function my_pre_post_link( $permalink, $post, $leavename ) {
  if( $post->post_type != 'post' ) return $permalink;
  $cats = get_the_category($post->ID);
  if( ! count($cats) ) return $permalink;

  usort($cats, '_usort_terms_by_ID');
  $category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post );

  $category_object = get_term( $category_object, 'category' );

  return _clear_uncategorized($category_object, $permalink);
}

function _clear_uncategorized($cat, $permalink) {
  if( $cat->slug == 'uncategorized' ) {
    return str_replace('%category%/', '', $permalink);
  }
  $parent = $cat->parent;
  if ( !$parent )
    return $permalink;
  return _clear_uncategorized($parent, $permalink);
}

add_filter( 'pre_post_link', 'my_pre_post_link', 9, 3 );
1
EvIl_DeViL

ok j'ai trouvé une solution. Faites-moi savoir si je peux avoir des ennuis en faisant cela.

fondamentalement, nous ajoutons la catégorie au lien url par programme.

à la demande de la page, nous vérifions si l’URL commence par un nom de catégorie/sous-catégorie. si c'est le cas, nous ajoutons simplement pour cette demande la section% category% à la structure permalink.

Ce qui est bien, c'est que site.com/%category%/my-article-name et site.com/my-article-name désignent le même contenu. la catégorie sera en première position, mais avec quelques ajustements, vous pouvez la placer où vous le souhaitez.

  1. définir la structure des permaliens sans catégorie (par exemple /% année% /% monthnum% /% postname%)
  2. ajoutez ce qui suit au functions.php

    function add_categories( $permalink, $post, $leavename ) {
      if( $post->post_type != 'post' ) return  $permalink;
      $cats = get_the_category($post->ID);
      if( ! count($cats) ) return $permalink;
    
      usort($cats, '_usort_terms_by_ID');
      $category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post );
    
      $category_object = get_term( $category_object, 'category' );
    
      return _clear_uncategorized($category_object, $permalink);
    }
    
    function _clear_uncategorized($cat, $permalink) {
      if( $cat->slug != 'uncategorized' ) {
        return '/%category%' . $permalink;
      }
      $parent = $cat->parent;
      if ( !$parent )
        return $permalink;
      return _clear_uncategorized($parent, $permalink);
    }
    add_filter( 'pre_post_link', 'add_categories', 9, 3 );
    
    function custom_rewrite_basic() {
      global $wp_rewrite;
      $categories = get_the_categories();
      foreach ($categories as $cat) {
        if(strrpos($_SERVER["REQUEST_URI"], $cat) === 0 )
        {
            $wp_rewrite->permalink_structure = "/%category%" . $wp_rewrite->permalink_structure;
        }
      }
      $wp_rewrite->flush_rules();
    }
    add_action('init', 'custom_rewrite_basic');
    
    function get_the_categories( $parent = 0, $pSlug = "" ) 
    {
        $slug = array();
        $categories = get_categories( "hide_empty=0&parent=$parent" );
        if (!$categories) return $slug;
        foreach ( $categories as $cat )
        {
            $slug[] = (($pSlug)?"/".$pSlug : "")."/".$cat->slug;
            $slug = array_merge($slug, get_the_categories( $cat->term_id, $cat->slug ));
        }
        return $slug;
    }
    
0
EvIl_DeViL