WordPress.org

Support

Support » How-To and Troubleshooting » Can't get some categories's name on archive.php

Can't get some categories's name on archive.php

  • Hey,

    I’ve got a weird problem. For only some categories (2 of 7), I can’t get their name. On archive.php I have this code :

    $cat = get_category( get_query_var( 'cat' ) );
    $cat_id = $cat->cat_ID;
    $cat_name = $cat->name;

    I got this error when printing $cat = get_category( get_query_var( 'cat' ) ); :

    WP_Error Object ( [errors] => Array ( [invalid_term] => Array ( [0] => Pas de terme ) ) [error_data] => Array ( ) )

    This is really strange, cause the default loop can retrieve the posts of the category who is being watched. And for others categories, this works well…

    Thanks for any suggestions 🙂

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator Michael

    @alchymyth

    Forum Moderator

    instead of all your code, try:

    $cat_name = single_cat_title('',false);

    Thanks for the help, but again its working only on 5 of 7 categories….
    The TWO are still not working…

    I can’t get it ! 🙁

    Hi, archive.php will already have the WordPress global $cat which already is the category->ID, so no need to get it

    You are then trying to cast an array into $cat, that is the error message, try changing that name of the variable!

    Try this and see what the missing ones say:

    <?php
    // Get the Current Category
    $curr_cat = get_category( $cat );
    // If the array is returned then get the Category Name
    $cat_name = ( $curr_cat ) ? $curr_cat->cat_name : 'No Category Found!!';
    echo $cat_name;
    //Returns category name or No Category Found!!
    ?>

    As already said if it is the archive.php then the call single_cat_title(”,false); will return the Category Title, are there invalid character in the category name?

    HTH

    David

    Thanks, but this is not working…

    I’ve found something interesting, my body class seems to be bugged. On category archive where the category name doesn’t display, I have this :
    logged-in archive category category-musique category-20

    On bugged archive page, I have this :
    logged-in archive post-type-archive post-type-archive-cinema

    It seems that WordPress can see it’s an archive…
    I tried to regenerate the menu, and create a new category with the same name…don’t work ! 🙁

    My categories’ name that don’t work are :”Cinéma” and “Livres”, but I have some with “é” that are working well…

    Are these being accessed from the same place, do you have category.php and archive.php?

    archive.php will be used for a date filter with is_day(), is_month(), is_year(), this is date and not category specific!
    This will normaly have the period as a title.

    category.php will be used for a category lists, if category.php does not exist the it will fall through to archive.php and then index.php
    archive.php would not generate a title for a category as posts could be mixed.

    It does say post-type-archive and not category, which makes me wonder how they are formed and being called, from a widget, or custom menu item, and how the links are generating different post-types.

    Try this!

    <?php
    // Is it a Category Archive
    if ( is_category() ) {
    	global $cat;
    	$curr_cat = get_category( $cat );
    	$cat_name = ( $curr_cat ) ? $curr_cat->cat_name : 'No Category Found!!';
    	echo $cat_name;
    }
    // End Category Archive Title
    ?>

    HTH

    David

    Thanks for replying.

    I use the archive.php page to load my custom-posts-type, I don’t have a category.php, by the way I tried to create category-livres.php, category-musique.php etc…still not working for 2 of 7, always the same.

    All my archive’s page are call from custom menu item.
    My li menu classes look like : menu-item menu-item-type-taxonomy menu-item-object-category menu-item-416

    I got url like : http://www.mywebsite.com/livres (I use the plugin No category Base to remove /category).
    I think I got it, I just desactivate this plugin and it’s working now for all category !…

    Why the hell it’s working with only 5 categories ?

    EDIT : I just created a category.php, it’s working still for 5 of 7. The brokens two load the archive.php and not category.php

    [ Please do not bump including ‘Up’, it’s not permitted here. ]

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Can't get some categories's name on archive.php’ is closed to new replies.