WordPress.org

Ready to get started?Download WordPress

Forums

custom styling categories through category.php (5 posts)

  1. martiniboy
    Member
    Posted 4 years ago #

    Hi

    I hope that someone can help, I am creating a CMS and I have a page called Sectors and within Sectors I have sub-pages called Education, Healthcare, Business and Public each with their own background image controlled through css. With these pages I have linked them to categories so when the education page is selected all the categories within education show in the sidebar. What I am having trouble with is when the sidebar link is clicked it goes through to the category.php page but I would like to have the background for each appropriate page, ie. education posts with the education background, catering posts with the catering background etc.

    I have used this code but does not work so has any one any idea why not.

    <div id="postcontent"
    	  <?php
    if (in_category('education'))
    {echo 'class="educationcontent"';}
    if (in_category('healthcare'))
    {echo 'class="healthcarecontent"';}
    if (in_category('business & industry'))
    {echo 'class="businesscontent"';}
    if (in_category('public sector'))
    {echo 'class="publiccontent"';}
    ?>>

    I thought that this meant :IF in the category of education use this class of educationcontent, catering uses cateringcontent etc.. Have I made an error in the code or this is not for this.

  2. syncbox
    Member
    Posted 4 years ago #

    I think you have to use elseif and else in there rather than all those "ifs"... without going into what it is you are trying to do, did you try:

    <?php
    if (in_category('education')) {
    {echo 'class="educationcontent"';}
    elseif (in_category('healthcare')) {
    {echo 'class="healthcarecontent"';}
    elseif (in_category('business & industry')) {
    {echo 'class="businesscontent"';}
    elseif (in_category('public sector')) {
    {echo 'class="publiccontent"';}
    else {//the default action if not any of the above }
    ?>

    I'm no php programmer, but...

    and I'd move the php inward, since you KNOW you want a class written, there's no need to put that part into the code (saving bytes) because you could write
    <div class="<?php /*all the logic above*/ ?>">

    just my two. hth, and good luck with it!

  3. Edward Caissie
    Theme Review Admin
    Posted 4 years ago #

    I might suggest something along these lines ...

    First, make use of category templates (if you are not), here is some information on them: http://codex.wordpress.org/Category_Templates

    Also, be sure to use the body_class() function, here is some reading on it: http://www.nathanrice.net/blog/wordpress-2-8-and-the-body_class-function/

    Combined you will have a specific template being called for each category, and the body_class() function will provide a specific class you will be able to style.

    Without a link to your blog it appears from your sample code above you are adding your "category class" to the post, not the page itself, whereas the body_class() function provides classes that encompass the entire page ... especially useful for "page" specific backgrounds.

    I do not believe you will even need to worry about using the if statements you are trying to implement (above).

    Hope this helps some.

  4. martiniboy
    Member
    Posted 4 years ago #

    Thanks for replying after all that my initial code worked I just needed to alter a part in the wordpress dashboard. Thanks Cais now I know about the body_class_function it will make future projects easier.
    Except now it has brought me to another problem - when I click on the link on the sidebar it directs me to the page and posts that I want except the sidebar is now empty. Here is the code which I have used in the sidebar.php

    <ul><?php
    		if (get_post_meta($post->ID,'categorynumber', true)) {
            $catid = get_post_meta($post->ID,'categorynumber', true);
    		wp_list_categories("child_of=" .$catid . "&title_li=");
    		}
    ?>
    </ul>

    but now I want to tell it that if it is on a page which is a child category still display the categories of the parent.
    I tried using at the end of the above code

    else {
    	if (in_category ('16,10,5,18'))
    	 wp_list_categories ('parent_of=' .$catid . "&title_li=" );

    but does not work,
    I hope what I am trying to do makes sense
    if not let me know I will explain in more detail
    }

  5. Edward Caissie
    Theme Review Admin
    Posted 4 years ago #

    Here is the codex page for wp_list_categories: http://codex.wordpress.org/Template_Tags/wp_list_categories ... there is no 'parent_of' parameter.

    You might also consider custom sidebar templates in your category template files. Here is another link into the codex: http://codex.wordpress.org/Function_Reference/get_sidebar

    Hope that helps to sort things out.

Topic Closed

This topic has been closed to new replies.

About this Topic