-1

i explain the situatio I'm having:

I have a blog, which on the homepage, have display the most recent post.

For each post category, I have set up one specific color and icon by css.

ex: for category pet:

article.category-pets .meta-category a {
color: white;
float: left;
padding: 2px;
padding-left: 25px;
background: url(http://mywebsite.com/wp-content/uploads/2015/02/pets-30_30.png) no-repeat left;
background-size: 15px 15px;
background-color: #a2591e;
background-position-x: 5px;
padding-right: 5px;
}

which works perfectly.

The Loop i'm using to display the category name is:

<?php
$category = get_the_category(); 
echo $category[0]->cat_name;
?>

What i would like to achieve is:

Display only the child-category of the post ( If the post is linked to a child category and his parent category) - and if the post is setup to only one parent category - display this parent category.

This loop works not really good, because, If i have my post linked for example:

Parent Category A Child Category A1

and

Parent Category B Child category B1

It is going to mess it up, in my case it display the child-category name ( eg. B1) - but with the icon and background-color of the Parent-Category A.

IT should display only the name of Child-Category A - with background-color and icon of A / not of B.

I can not use a loop to display only child-category or only parent-category because some post ( there is thousands) are only setup to a parent category.

I would really love any helps in that, would be lovely !

Thank you for all your time :)

--EDIT -- I add the article produced:

This is example when it works fine, because the post is only setup to one parent-category and its child

    <article class="post-3546 post type-post status-publish format-standard has-post-thumbnail hentry category-pet-of-the-week category-pets vce-post vce-lay-c">

<h2 class="entry-title"><a href="http://mywebsite.com/pets/pet-of-the-week/pets-sale-5th-feb/" title="Pets For Sale On (5th Feb)">Pets For Sale On (5th Feb)</a></h2>

    <div class="meta-image">        
                    <a href="http://mywebsite.com/pets/pet-of-the-week/pets-sale--5th-feb/" title="Pets For Sale On (5th Feb)">

            <img width="292" height="195" src="http:/mywebsite.com/wp-content/uploads/2015/02/siberian-husky.jpg" class="attachment-vce-lay-b wp-post-image" alt="Siberian Husky For Sale" />                           </a>

                            <span class="meta-category">    
     <a href="http://mywebsite.com/category/pets/pet-of-the-week/">Pet Of The Week</a>
</span> 
            </div>


<header class="entry-header">

    <div class="entry-meta"><div class="meta-item views">26 Views</div><div class="meta-item date"><span class="updated">2 weeks ago</span></div></div>
</header>

        <div class="entry-content">
        <p>As another month rolls in, with it comes a fresh supply of adorable pets for your viewing pleasure...</p>
    </div>
</article>

Here it's when its not working correctly, because the post is attach to 3 parents cvategory and some of their child - so it mixed it up display the name of a child-category, but the background-colro and icon of another parent-category . . .

<article class="post-3107 post type-post status-publish format-standard has-post-thumbnail hentry category-bedroom-2 category-business-and-finance category-childrens category-home-garden category-london category-money-saving category-others category-selling-and-advertising tag-cleaning tag-clutter tag-declutter tag-home tag-house tag-london tag-sell tag-space tag-summer tag-to-do-list vce-post vce-lay-c">

     <h2 class="entry-title"><a href="http://mywebsite.com/others/london/home/" title="A Guide to Clearing Out the Clutter : You Can De-junk Your House in a Weekend">A Guide to Clearing Out the Clutter : You...</a></h2>

        <div class="meta-image">        
                        <a href="http://mywebsite.com/others/london/home/" title="A Guide to Clearing Out the Clutter : You Can De-junk Your House in a Weekend">

                <img width="225" height="195" src="http://mywebsite.com/wp-content/uploads/2014/07/AM-quotes-laundryTsunami-final-Edited.jpg" class="attachment-vce-lay-b wp-post-image" alt="AM-quotes-laundryTsunami-final - Edited" />                           </a>

                                <span class="meta-category">    
         <a href="http://mywebsite.com/category/home-garden/bedroom-2/">Bedroom</a>
    </span> 
                </div>


    <header class="entry-header">

        <div class="entry-meta"><div class="meta-item views">7 Views</div><div class="meta-item date"><span class="updated">7 months ago</span></div></div>
    </header>

            <div class="entry-content">
            <p>OUT with the Old&#8230;  A Guide to Clearing Out the Clutter : You Can De-junk Your House in a...</p>
        </div>
    </article>

----- EDIT - 2 relevant code ---

<article <?php post_class('vce-post vce-lay-c'); ?>>



    <?php if($fimage = vce_featured_image('vce-lay-b')): ?>


        <h2 class="entry-title"><a href="<?php echo esc_url(get_permalink()); ?>" title="<?php echo esc_attr(get_the_title()); ?>"><?php echo vce_get_title('lay-c'); ?></a></h2>

        <div class="meta-image">        
                        <a href="<?php echo esc_url(get_permalink()); ?>" title="<?php echo esc_attr(get_the_title()); ?>">

                <?php echo $fimage; ?>
                <?php if($icon = vce_post_format_icon('lay_c')) :?>
                    <span class="vce-format-icon">
                    <i class="fa <?php echo $icon; ?>"></i>
                    </span>
                <?php endif; ?>
            </a>

            <?php if( vce_get_option('lay_c_cat')) : ?>
            <?php if (function_exists('z_taxonomy_image_url')) echo z_taxonomy_image_url(); ?>
        <span class="meta-category">    
<?php
$category = get_the_category(); 
echo $category[0]->cat_name;
?>

    </span> 
        <?php endif; ?>
        </div>
    <?php endif; ?> 

    <header class="entry-header">

        <div class="entry-meta"><?php echo vce_get_meta_data('lay-c'); ?></div>
    </header>

    <?php if( vce_get_option('lay_c_excerpt')) : ?>
        <div class="entry-content">
            <p><?php echo vce_get_excerpt('lay-c'); ?></p>
        </div>
    <?php endif; ?>
</article>
  • Please post the <article> tag that your template produces. – geomagas Feb 18 '15 at 9:41
  • Please also post the relevant code from you template that produces the <article>. – David Gard Feb 18 '15 at 10:07
  • Thank you for your time guys, the loop is insert in the <span class="meta-category"> Hope it helps better :) – tibewww Feb 18 '15 at 10:09
0

For the example you posted, the article is styled according to the css rule(s) for category-selling-and-advertising which is defined last (among the category-* classes) in the article's class attribute.

You need to intercept the functionality of post_class() to change the order these classes appear. The post_class filter will do the job for you:

add_filter(
  'post_class',
  function($classes) // $classes is an array
    {
    $cats=get_the_category();
    $master_cat=$cats[0]->slug;
    if($k=array_search("category-$master_cat",$classes))
      {
      unset($classes[$k]);
      $classes[]="category-$master_cat";
      }
    return $classes;
    }
  );

The above is a rather naive solution, but I think it's a good place to start.

  • HI, Hm yes you re totally right, I understand more or less - But my functions / php / query skills are really low. I Suppose I need to add this in the functions.php - But How can I set up one ? IF you have tie to give me an example .. . . it would be wonderfull . . . . – tibewww Feb 18 '15 at 10:30
  • Most probably there's a functions.php in your theme's directory, but I'd suggest you make a child theme. OTOH, you could put this code in a plugin which is the way I prefer to do things myself. – geomagas Feb 18 '15 at 12:29
  • Hi,Thank you, Jsut another question, is there any this I have to change in this code ? ( I mean - where I add my category name/ID,etc ?) WHen i add this code in my function.sphp from my child theme, I get a code mistake in classes[]="category-$master_cat"; } THank you for your time . .. – tibewww Feb 18 '15 at 12:48
  • Oops! You're right, it's $classes instead of classes. Edited my answer. My bad, sorry! :) – geomagas Feb 18 '15 at 12:50
  • Hi again, thank you . . but it doenst work :( I've zero post get display with this code . . . .:( SHould I change 'post-class' by a specific class fro my theme ? Or let this code raw as it is ? – tibewww Feb 18 '15 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.