Can't get some categories's name on archive.php (7 posts)

  1. Tailzip
    Posted 4 years ago #


    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 :)

  2. Michael
    Forum Moderator
    Posted 4 years ago #

    instead of all your code, try:

    $cat_name = single_cat_title('',false);

  3. Tailzip
    Posted 4 years ago #

    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 ! :(

  4. Digital Raindrops
    Posted 4 years ago #

    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:

    // 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?



  5. Tailzip
    Posted 4 years ago #

    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...

  6. Digital Raindrops
    Posted 4 years ago #

    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!

    // 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



  7. Tailzip
    Posted 4 years ago #

    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. ]

Topic Closed

This topic has been closed to new replies.

About this Topic