• 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(https://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 - 16 through 30 (of 46 total)
  • Thread Starter tibewww

    (@tibewww)

    Hi Larsn,

    Thank you but the same problem is still hapenning ??

    My post which are attached to several main-cetagory and several children Categories get the css mixed ??

    It’s all workign fine when the post is attach to only one parent category and their childrens or only parent-category.

    But if a post is attach to severals parents-category and several children-category it display one children-category in text ( which is exactly what I want), but display the css of another parent-category – It should display the css or the parent-category attach of the text getting display ( from the children-category).

    I attach here the html of on of this post who is attach to multiples parents and children category .. . Really appreciate your time, I’ve spend one week on that . .. can not solve it, i really need some help ??

    <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="https://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="https://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="https://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="https://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>

    This part is a little unclear to me:It should display the css or the parent-category attach of the text getting display ( from the children-category).

    1. Do you want the css of the specific child category to display?

    2. If you want to display the css of one parent category even though there are multiply category’s assigned you van create a custom field next to every post (in the admin area) and select te category you want to use for styling.

    Then you can output that inside the link class.

    Let me know which option you mean.

    Thread Starter tibewww

    (@tibewww)

    hmm.

    I know im sorry about my explanation;

    I have set up a css for each parent-category ( which is a different background-color and an icon): by

    article.category-pets .meta-category a {
        color: white;
        float: left;
        padding: 2px;
        padding-left: 25px;
        background: url(https://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;
        }

    Each child-category – get the same style as their parent-category.

    Otherwise – the problem I’m having is, when one post is attach to multiple parent-category and multiple child-category “it mix” the style – for example, in the html I’ve bring earlier – it display the child-category name “bedroom” – which is exactly what I want/need. But instead of having the style of its parent category (Home and garden) – it Get the style of another Parent-Category where i attached this post ( Business and Finances). The post is well attach to the Parent-Category of “bedroom” – so I don’t understand why it bring the style of another Parent . ..

    I hope this is more clear . .. . Let me know if not ?? Thx again for your time

    Thread Starter tibewww

    (@tibewww)

    So I think from your suggestion its option 2 , thx again for your help ??

    Hy,

    I’ll send you a code example in the evening (that’s in about 5 hours)
    Then you can specify the category for styling.

    I think this is the best solution cause witouth an option like this your code cannot choose so it will probably take the first class.

    In the script i will create a custom field for all your posts.
    the custom field will have a dropdown list with all the parent categories (or parent and child if you want, let me know within 3 hours).

    Thread Starter tibewww

    (@tibewww)

    This would be just so awesome ! Thank you for all your time !!
    I just need the child-category to get the style of the parents – so i think just need to select the parent-category in thsi dropdown – no need the child, thank you so much ?? ??

    Hy tibewww,

    Here is the code for the meta box:

    /**
     * Adds a box to the main column on the Post and Page edit screens.
     */
    function myplugin_add_meta_box() {
    
    	$screens = array( 'post', 'page' );
    
    	foreach ( $screens as $screen ) {
    
    		add_meta_box(
    			'myplugin_sectionid',
    			__( 'Select parent category', 'myplugin_textdomain' ),
    			'myplugin_meta_box_callback',
    			$screen,'side'
    		);
    	}
    }
    add_action( 'add_meta_boxes', 'myplugin_add_meta_box' );
    
    /**
     * Prints the box content.
     *
     * @param WP_Post $post The object for the current post/page.
     */
    function myplugin_meta_box_callback( $post ) {
    
    	// Add an nonce field so we can check for it later.
    	wp_nonce_field( 'myplugin_meta_box', 'myplugin_meta_box_nonce' );
    
    	/*
    	 * Use get_post_meta() to retrieve an existing value
    	 * from the database and use the value for the form.
    	 */
    	$parentscategory ="";
    ?>
    <select name="myplugin_new_field"><option></option>
    <?php
    foreach(get_the_category() as $category) {
    if ($category->category_parent == 0) {
       $cmf = get_post_custom($post->ID);
    $currentcat = $cmf ["_my_meta_value_key"][0];
        if($currentcat == $category->name){
    $parentscategory .= '<option value="'.$category->name.'" selected>'.$category->name.'</option>';
        }
        else{
            $parentscategory .= '<option value="'.$category->name.'">'.$category->name.'</option>';}
    }
    }
    echo $parentscategory;
    ?></select><?php
    }
    
    /**
     * When the post is saved, saves our custom data.
     *
     * @param int $post_id The ID of the post being saved.
     */
    function myplugin_save_meta_box_data( $post_id ) {
    
    	/*
    	 * We need to verify this came from our screen and with proper authorization,
    	 * because the save_post action can be triggered at other times.
    	 */
    
    	// Check if our nonce is set.
    	if ( ! isset( $_POST['myplugin_meta_box_nonce'] ) ) {
    		return;
    	}
    
    	// Verify that the nonce is valid.
    	if ( ! wp_verify_nonce( $_POST['myplugin_meta_box_nonce'], 'myplugin_meta_box' ) ) {
    		return;
    	}
    
    	// If this is an autosave, our form has not been submitted, so we don't want to do anything.
    	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
    		return;
    	}
    
    	// Check the user's permissions.
    	if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
    
    		if ( ! current_user_can( 'edit_page', $post_id ) ) {
    			return;
    		}
    
    	} else {
    
    		if ( ! current_user_can( 'edit_post', $post_id ) ) {
    			return;
    		}
    	}
    
    	/* OK, it's safe for us to save the data now. */
    
    	// Make sure that it is set.
    	if ( ! isset( $_POST['myplugin_new_field'] ) ) {
    		return;
    	}
    
    	// Sanitize user input.
    	$my_data = sanitize_text_field( $_POST['myplugin_new_field'] );
    
    	// Update the meta field in the database.
    	update_post_meta( $post_id, '_my_meta_value_key', $my_data );
    }
    add_action( 'save_post', 'myplugin_save_meta_box_data' );

    I do advice you to change the default name’s in the script ( like myplugin_meta_box or myplugin_add_meta_box )

    To show the saved parent category and show it as a class for the link use the code i posted earlier but with a little modification:

    <?php
    $category = get_the_category();
    $cmf = get_post_custom($post->ID);
    $currentcat = $cmf ["_my_meta_value_key"][0];
    ?><a href="#" class="<?php echo $currentcat.' '. $category[0]->cat_name;?>"><?php echo $category[0]->cat_name;?></a>

    The first script makes a dopdown list of all the parent categories, just select one an save the post.

    The second code take the name of the child category and the name you have selected and add it as a class inside the link.

    Hope this helps you enough!

    Kind regards,

    Larsen

    Thread Starter tibewww

    (@tibewww)

    Hi Larsen,

    Thank you very much,

    really appreciate your time, you’re amazing !!

    I’ve add the script in the functions.php ;

    and Change the loop by

    <?php
    $category = get_the_category();
    $cmf = get_post_custom($post->ID);
    $currentcat = $cmf ["_my_meta_value_key"][0];
    ?><a href="#" class="<?php echo $currentcat.' '. $category[0]->cat_name;?>"><?php echo $category[0]->cat_name;?></a>

    in the post, I can see the dropdown – and select the parent category assigner !

    Otherwise . . . . nothing happen in the front end ?? it does’nt take the style off the parent select in your dropdown ??

    I tried to replace the name of: myplugin_meta_box by myplugin_parent and myplugin_add_meta_box by myplugin_parentbox

    but doesnt change anything ?? – if you have any other idea . . .will be amazing . ..
    Thank you so much for all your time !!!

    Thread Starter tibewww

    (@tibewww)

    Update: The thing is; when I update my post with the parent category selected in the dropdown. It’s not saving .. . I mean that once i’ clicked updated – it come back to nothing selected in the dropdown. I dont knof if this is realted or not ?

    Hy,

    And if you place the original code i have send?
    Will it then work?

    Larsen

    Thread Starter tibewww

    (@tibewww)

    Do you mean this one ?:

    <?php
    $category = get_the_category();
    $cmf = get_post_custom($post->ID);
    $currentcat = $cmf ["_my_meta_value_key"][0];
    ?><a href="#" class="<?php echo $currentcat.' '. $category[0]->cat_name;?>"><?php echo $category[0]->cat_name;?></a>

    If yes, I;ve tried this one also yes, and didnt hcange anything ??

    ( PS; this code should go in my loop and not functions.php isnt it ?)

    no, i mean just add this code as it is in your functions.php:

    /**
     * Adds a box to the main column on the Post and Page edit screens.
     */
    function myplugin_add_meta_box() {
    
    	$screens = array( 'post', 'page' );
    
    	foreach ( $screens as $screen ) {
    
    		add_meta_box(
    			'myplugin_sectionid',
    			__( 'Select parent category', 'myplugin_textdomain' ),
    			'myplugin_meta_box_callback',
    			$screen,'side'
    		);
    	}
    }
    add_action( 'add_meta_boxes', 'myplugin_add_meta_box' );
    
    /**
     * Prints the box content.
     *
     * @param WP_Post $post The object for the current post/page.
     */
    function myplugin_meta_box_callback( $post ) {
    
    	// Add an nonce field so we can check for it later.
    	wp_nonce_field( 'myplugin_meta_box', 'myplugin_meta_box_nonce' );
    
    	/*
    	 * Use get_post_meta() to retrieve an existing value
    	 * from the database and use the value for the form.
    	 */
    	$parentscategory ="";
    ?>
    <select name="myplugin_new_field"><option></option>
    <?php
    foreach(get_the_category() as $category) {
    if ($category->category_parent == 0) {
       $cmf = get_post_custom($post->ID);
    $currentcat = $cmf ["_my_meta_value_key"][0];
        if($currentcat == $category->name){
    $parentscategory .= '<option value="'.$category->name.'" selected>'.$category->name.'</option>';
        }
        else{
            $parentscategory .= '<option value="'.$category->name.'">'.$category->name.'</option>';}
    }
    }
    echo $parentscategory;
    ?></select><?php
    }
    
    /**
     * When the post is saved, saves our custom data.
     *
     * @param int $post_id The ID of the post being saved.
     */
    function myplugin_save_meta_box_data( $post_id ) {
    
    	/*
    	 * We need to verify this came from our screen and with proper authorization,
    	 * because the save_post action can be triggered at other times.
    	 */
    
    	// Check if our nonce is set.
    	if ( ! isset( $_POST['myplugin_meta_box_nonce'] ) ) {
    		return;
    	}
    
    	// Verify that the nonce is valid.
    	if ( ! wp_verify_nonce( $_POST['myplugin_meta_box_nonce'], 'myplugin_meta_box' ) ) {
    		return;
    	}
    
    	// If this is an autosave, our form has not been submitted, so we don't want to do anything.
    	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
    		return;
    	}
    
    	// Check the user's permissions.
    	if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
    
    		if ( ! current_user_can( 'edit_page', $post_id ) ) {
    			return;
    		}
    
    	} else {
    
    		if ( ! current_user_can( 'edit_post', $post_id ) ) {
    			return;
    		}
    	}
    
    	/* OK, it's safe for us to save the data now. */
    
    	// Make sure that it is set.
    	if ( ! isset( $_POST['myplugin_new_field'] ) ) {
    		return;
    	}
    
    	// Sanitize user input.
    	$my_data = sanitize_text_field( $_POST['myplugin_new_field'] );
    
    	// Update the meta field in the database.
    	update_post_meta( $post_id, '_my_meta_value_key', $my_data );
    }
    add_action( 'save_post', 'myplugin_save_meta_box_data' );
    Thread Starter tibewww

    (@tibewww)

    Hi, its’ not changing anything ??

    Just to be sur, which code should i put in my loop to get the_category ?

    this one:

    <?php
    $category = get_the_category();
    $cmf = get_post_custom($post->ID);
    $currentcat = $cmf ["_my_meta_value_key"][0];
    ?><a href="#" class="<?php echo $currentcat.' '. $category[0]->cat_name;?>"><?php echo $category[0]->cat_name;?></a>

    ?

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