WordPress.org

Ready to get started?Download WordPress

Forums

Php issue once uploaded to server (3 posts)

  1. phantomdentist
    Member
    Posted 4 years ago #

    I have header.php body tag modified to include the body_class

    <body <?php body_class(category, paged, tag, page, archive, blog, home, attachment, tag); ?>>

    I also have some code added to my functions.php so that the category slug is added to the body_class

    Heres the full functions.php code

    <?php
    if ( function_exists('register_sidebars') )
    register_sidebars(3);
    
    add_filter('body_class','add_category_to_single');
    function add_category_to_single($classes, $class) {
    	if (is_single() ) {
    		global $post;
    		foreach((get_the_category($post->ID)) as $category) {
    			echo $category->cat_name . ' ';
    			// add category slug to the $classes array
    			$classes[] = 'category-'.$category->slug;
    		}
    	}
    	// return the $classes array
    	return $classes;
    }
    
    ?>

    This works brilliantly, a great way to be able to add news banner image to all posts of the news category for example.

    Hosting the site from my pc works fine, when I allow people in to view it all is fine. When I upload it to my host I get problems

    At the top of each page I get

    Warning: Missing argument 2 for add_category_to_single() in /home/tonguet1/public_html/wp-content/themes/TTM/functions.php  on line 6
    News class="single postid-493 logged-in category category-news">

    For each page the last line of the error changes to match the classes that are being generated by body_class for that page.

    Any ideas?

  2. Jack Reichert
    Member
    Posted 4 years ago #

    Had the same problem... It worked, then it stopped.

    If you replace this line:

    function add_category_to_single($classes, $class) {

    with this:

    function add_category_to_single($classes) {

    it should work.

    Not sure though, why it worked before, and now it doesn't...

  3. thatnorwegianguy
    Member
    Posted 3 years ago #

    Thankyou @jackreichert! I had the same problem.

    I had ($classes, $class) working on my local dev, but on the live server I had to change it to ($classes)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.