• I’m attempting to change the background image based on what page or category the user is on. I can’t seem to get the code to work for the categories though.

    The url: https://asvpart.com/new/category/portfolio/

    the code:

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
            <?php $cat_obj = $wp_query->get_queried_object(); $cat_id = $cat_obj->cat_ID; ?> 
    
            <?php if (is_page('5') ):
    			  // page home ?>
    			<style type="text/css">
    			body {background-color:#000;background-image:url(https://asvpart.com/new/wp-content/uploads/2011/07/bkgrnd_gallery.jpg);background-repeat:no-repeat; background-size: 100%;}
    			</style>
    
                <?php elseif (in_category('portfolio') ):
    	  		// category 5 ?>
    			<style type="text/css">
    			body {background-color:#000;background-image:url(https://asvpart.com/new/wp-content/uploads/2011/07/bkgrnd_gallery.jpg);background-repeat:no-repeat; background-size: 100%;}
    			</style>
    <?php endif; ?>

    I’m not sure why the ‘portfolio’ will not target the ‘portfolio’ page.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The in_category() is a check to see if a post has a category assigned to it and would be used in single.php for a single post.

    A page would be in page.php, so I am not sure why you have the statements together or what you mean here?.

    The link only has one post and I am assuming you mean the list view?

    If it is the list view, showing all the posts then look at the template hierarchy category-{slug}.php

    In your themes folder, look for category.php if not use index.php

    Open the file and save it as category-portfolio.php and add your code in there!

    Whenever the portfolio category is called it will use the new file.

    The body class is usually in header.php and you would add conditional code for the style there, and not in the loop?

    HTH

    David

    your theme uses body_class() – therefore it already puts a category specific class into the body tag; for instance: .category-portfolio – which you can use in the style.css;

    example:

    body.category-portfolio { background-color:#000;background-image:url(https://asvpart.com/new/wp-content/uploads/2011/07/bkgrnd_gallery.jpg);background-repeat:no-repeat; background-size: 100%;}

    https://codex.www.ads-software.com/Function_Reference/body_class

    Thread Starter meganlee1984

    (@meganlee1984)

    In using the body_class, how can I get the posts that are listed in the given category to have the same background? I’m trying this for the art category:

    body.category-art {
    background-color:#000;
    background-image:url(https://asvpart.com/new/wp-content/uploads/2011/07/bkgrnd_gallery.jpg);
    background-repeat:no-repeat; background-size: 100%;
    }

    https://asvpart.com/new/art/dope-asvp-shots-via-redboy-at-streetandstage-com/

    But its not working ??

    body-class is only outputting the category css class in category archive pages; to have the same effect in single posts, you might need to add a bit of code to functions.php (idea copied from Twenty Eleven):

    /**
     * Adds category classes to the array of body classes - in single post only.
     *
     */
    function add_category_body_classes( $classes ) {
    
    	if ( is_single() ) {
    		global $post;
    		$cats = get_the_category($post->ID);
    		foreach( $cats as $cat ) {
    		$classes[] = 'category-' . $cat->slug;
    		}
    	}
    
    	return $classes;
    }
    add_filter( 'body_class', 'add_category_body_classes' );

    this will add css classes into the body tag for all categories of the post.

    Thread Starter meganlee1984

    (@meganlee1984)

    Thank you for your response. I was able to get this to work for one category, but not another. The category that is working is not in the blog section, should that make a difference?

    The Working Page
    CSS:

    body.category-portfolio {
    	background-color:#000;
    	background-image:url(https://asvpart.com/new/wp-content/uploads/2011/07/bkgrnd_gallery.jpg);
    	background-repeat:no-repeat; background-size: 100%;
    	}

    The Non-working Page
    The CSS:

    body.category-art {
    	background-color:#000;
    	background-image:url(https://asvpart.com/new/wp-content/uploads/2011/07/bkgrnd_gallery.jpg);
    	background-repeat:no-repeat; background-size: 100%;
    	}

    Does the page have to be targeted differently because the art category is show through the blog?

    the non-working example is no single post – but an attachment page;

    right now, i have no idea if an attachment page even has a category – at least none is showing in the post_class.

    the background for this post ‘Dope ASVP shots via Redboy at streetandstage.com’, for instance, is working.

    Thread Starter meganlee1984

    (@meganlee1984)

    Yes, thank you, I need to uncheck the attachment link from the image. Thank you!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Targeting a specific category to replace background images’ is closed to new replies.