• Hi Everybody,

    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 πŸ™‚

Viewing 15 replies - 1 through 15 (of 46 total)
  • Hy tibewww,

    Can you provide me with a link to a specific category you have set up so i can check if the css is used correctly?

    Kind regards,

    Larsen

    Thread Starter tibewww

    (@tibewww)

    HI Larsen,

    ANyway to send you this a private message or email ? I woudl not liek to share the url of the website publicly,

    Thank you

    Sure, email me at [redacted]

    Hi there – it’s really strongly discouraged to contact people off the forums – http://codex.wordpress.org/Forum_Welcome#Helping_Out

    Please do keep help here.

    Hy WPyogi,

    Oke, is there any way to let tibewww send me a private message?

    Larsen

    No, these forums really only provide help here – so generally, that’s the appropriate reply – that these forums don’t have private messages. Exceptions may be plugin or theme authors that opt to provide a higher level of support – but in general, they are encouraged to help as much as possible here so that everyone can benefit from the help. Not everyone wants to share their URL or their site may not be on a public server, which is fine but it may limit what help can be offered here.

    Oke, thanks.

    tibewww it look’s like you will have to place the URl here.

    You can edit the post in about half an hour and remove the link from you message so it wont be here for ever.

    Thread Starter tibewww

    (@tibewww)

    Hi . . hm no worries, I can post the Html of the <article> product, if this can help out ?

    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…  A Guide to Clearing Out the Clutter : You Can De-junk Your House in a...</p>
        		</div>
        	</article>

    Do you just want to change the background color of the link?

    is so use :

    .category-childrens .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;
        }

    You don’t have to include article cause you target the article class already.
    Also there isn’t a pets category ( or maybe it is just the example..)

    is this an answer to you question?

    Larsen

    Thread Starter tibewww

    (@tibewww)

    HI,

    Hmm not really, im sorry πŸ™

    Yes, I’ve took another example,

    The problem I’m having is coming from the loop – because it mix it up – it display the first children-category name – which is good – but the css of another parent-category and not its own parent-category . . . . Don’t know if it make more sense ?

    Thread Starter tibewww

    (@tibewww)

    I attach the full code if it helps . . .:(

    <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>

    I have to pick up my kid from school, i will get back to you after i’m back

    Thread Starter tibewww

    (@tibewww)

    No Worries, thank you so much πŸ™‚

    Thread Starter tibewww

    (@tibewww)

    Hi, Are you back πŸ™‚ ?

    I’ve tried this function:

    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;
        }
      );

    But it doenst change anything . . . . do you have any highlite by any chance ?

    Hy Tibewww,

    You want to display parent category’s if there is no child category present en child category’s when they are there.

    Try adding this code

    <?php
    $category = get_the_category();
    $catParentname = get_cat_name ($category[0]->category_parent);
    ?><a href="#" class="<?php echo $catParentname.' '. $category[0]->cat_name;?>"><?php echo $category[0]->cat_name;?></a>

    It give’s the child category link a class with both the parentcat and the childcat name.

    If this work’s you can now target all links specifically.

    let me know if this works!

    Goodluck,

    Larsen

Viewing 15 replies - 1 through 15 (of 46 total)
  • The topic ‘loop mix my child-category and parent-category’ is closed to new replies.