• 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 - 31 through 45 (of 46 total)
  • yes.

    Thread Starter tibewww

    (@tibewww)

    Its not working ?? ; I can see the dropdown in the post – but when i update it – nothing happens . . .

    This is the link if you to take a a look, exemple is the last article of the homepage (A Guide to Clearing Out the Clutter : You…) – this post is attach to 4 differents parent categories and two child categories from 2 parents.
    .

    https://i delete the link.com/

    here it display the child- category bedroom – which is good – but take the css style of the parent category ” business and finance”- it should take the css of the parent category bedroom – which is “home and garden” . . .

    If it make it easier for you ?

    Really apprecaite yoru time

    Hy,

    First.. you need to set the parent category of each post individuality.

    second.. the class in the link say’s (example) class=”Home & Garden”
    this means that in the css there are 3 different classes: .Home .& .Garden

    Maybe the problem is that you display the category name instead of the category slug in the link class, maybe you want the class in the link to be more like home-&-garden.

    is that correct?

    Oke..

    Add this to the loop

    <?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]->slug;?>"><?php echo $category[0]->cat_name;?></a>

    This is the code for the 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->slug.'" 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 back,

    Each post are attach to a parent category.

    I jsut double chedked, and notice that when i have delete the !important in my css . ..

    It start workings !

    Example – in the article exampel i ve give you, when i set up the drodown category to ‘London’ it take the style of the category london i ve given !!

    The problem is now – if i set up this dropdown to another category ( For eg, home and garden – which i want), when i update the post – it doesnt select it ( once i click update, the dropown become empty).
    The thing is i dt want to set up a css forl the child category – this will be too long , and the editor of the website are goin to addc hild ategory themselve – so i want this to be dynamic.

    Do you ahve any idea about this issue that i dt save some of the categories with the dropdown ?

    Thank you

    Yes.

    If you have your post…

    then select the child category and top lvl category you want to attach.
    then save the post. ( just as you would normally add a post to a specific category)

    After saving the dropdown will be populated with the top lvl category’s you have attached to the post.

    Select the top lvl category you want to attach in the dropdown and save the post again.

    On the front-end the link will now contain a class with the chosen parent category slug and the child category slug.

    Thread Starter tibewww

    (@tibewww)

    I can see yes in the html it does display the class set up via the dropdown.

    SO if i understand – ia have to create a new css for that isn it ?

    the previous class i ve been using was:

    article.category-others .meta-category a {my css
    }

    which is overiding the one from your code.

    Which new css class should i use now ?

    Sorry to ask so much – i m getting lost

    Hy,

    Don’t worry about asking to much on the wordpress forum.

    To answer your question:

    The class to target is now inside the .meta-category

    so you target the .meta-category before you target the selected parent ( or child) category inside the link

    Sample:

    Before it was :

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

    Now it is:

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

    Note the class=” in the link.

    Thread Starter tibewww

    (@tibewww)

    Allright, I got it !!

    I just add this css

    article.category-others .meta-category a.Others {

    my css}

    when Others is selected in the dropdown .. and it works perfectly for all the child ??

    Thank you so much !!

    There is only one problem . . .

    This works perfectly, butm- I don’t know why – in some post – when I save the dropdown – it doesnt save it like i said ( when i updated – it return the dropdown blank – without the category selected) for some of the posts . . .. some of them works fine — some this issue appear .. . do you have any trick to fix that by any chance ?

    Thank you ??

    Another question –

    In fact i would like to be able to choose which child-category and category text appear when the post is assign to severals . . . DO you know how this is possible also ? Thank you for all your time, I really really appreciate it !

    Thread Starter tibewww

    (@tibewww)

    OK
    I knwo from what is this problem comign from, it’s happening only if my category have some space in the name or a character such as ‘&’

    Do you know to solve this in the code of the functions by any chance ?

    Thread Starter tibewww

    (@tibewww)

    solved it by replacing all $category->name by $category->slug and works perfectly ?? ??

    If by any chance yo have any idea if i can be able to choose which child-category or/and parent category text appear when the post is assign to severals of them, it would be awesome ??

    Thanks again for all your time !

    Hy,

    Selecting a child category could work the same as the parent category.
    check : https://codex.www.ads-software.com/Function_Reference/get_categories

    child_of under parameters will explain a little about how to get the children of specific category’s.

    I currently don’t have any time to create a sample for the child cat to..
    Tonight or tomorrow at the end of the day i can probably create a sample for the child cat to.

    If you can create this on yourself do let me know so I don’t have to start tonight or tomorrow if I found the time for it.

    Kind regards,

    Larsen

    Thread Starter tibewww

    (@tibewww)

    Hi back,

    Thank you,

    I’ll try to fin d a solution, obviously i would like to have a dropdown in the post page, where i can choose wich child/parent category to display. As main of my post are attach to multiple category, id like to be able to choose a specific one to display using this method. IF you have any others links/pointer, let me know.

    If i manage to go far I would of course let you know

    Thread Starter tibewww

    (@tibewww)

    Trying so far, i’m not managing to do it at the moment;

    I’ve been trying to use back the same function that you did previously, renaming it in another name – i have the dropdown who display correctly in the post.

    I’m blocked at this level:

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

    I’ve been trying to replace $parentscategory by $category and others solution,, to be able to display any parents/children category – but not succesfully ?? :(.

    If you have any time to help me out a last time with that . . it would be incredible .. . .

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