WordPress.org

Ready to get started?Download WordPress

Forums

Unique photo for each parent category (8 posts)

  1. manifestcreative
    Member
    Posted 5 years ago #

    I have a layout for http://www.theesperanzacenter.org that I'm working on. Based upon reading in the Codex and other forum posts, I came up with the following function that I had hoped would allow me to have a unique photo for each parent category. However, when I visit the archives of a sub-category, I get the following error:

    Catchable fatal error: Object of class WP_Error could not be converted to string in /home/esperanz/public_html/wp-content/themes/thesis/custom/custom_functions.php on line 66

    Line 66 of my code refers to creating the photo name plus the category_nicename (ie photo-category-nice-name.jpg).

    This is the function that I'm using:

    function image_header () {
    	if (is_home() || is_front_page()) {
    		$header_photo = "photo-home";
    	}
    	elseif (is_search()) {
    		$header_photo = "photo-search";
    	}
    	elseif (is_category() || is_archive() || is_single()) {
    			$cat = get_the_category();
    			$category = get_category_parents($cat[0]->category_parent, false, '', true);
    			$header_photo = "photo-".$category."";
    	} else {
    		$cat = get_the_category();
    		$category = get_category_parents($cat[0]->category_parent, false, '', true);
    		$header_photo = "photo-".$category."";
    	}
    
    	$header_photo_url = "".get_bloginfo('template_url')."/custom/images/category-headers/".$header_photo.".jpg";
    
    echo ("<img src='."$header_photo_url".'>");

    I believe that the function that I've written is only pulling the current (child) category, and not the top level category.

    Can anyone offer me some advice?

  2. manifestcreative
    Member
    Posted 5 years ago #

    Can anyone give me a little WordPress love on this stumper?

  3. manifestcreative
    Member
    Posted 5 years ago #

    I'm answering as many noob questions as I can in hopes of someone taking a look!

  4. saki_kawa
    Member
    Posted 5 years ago #

    Hi!
    I'm having a similar problem, I think it's get_category_parents(), its output is an object instead a string, I'm looking for a solution too.
    Do you find anything about?
    If not, when I'll find the answer I'll write you here.

  5. saki_kawa
    Member
    Posted 5 years ago #

    I have read again your code and I don't think we have the same problem.
    Try var_dump($category), to see what is it, or var_dump(any other var), so you can find where is the problem.

  6. dremation
    Member
    Posted 5 years ago #

    @manitfestcreative, you figure it out yet?

  7. mwilliams
    Member
    Posted 4 years ago #

    i am having the same problem and it is caused by incompability of 2.7 and get_category_parents. any solution?

  8. mwilliams
    Member
    Posted 4 years ago #

    oh I found it, insert this
    $category = get_the_category();
    $cat = $category[0]->cat_ID;

Topic Closed

This topic has been closed to new replies.

About this Topic