• Resolved Scott Buehler

    (@weborder)


    Hey guys and gals! I’ve searched high and low for this and I just can’t find a solution.

    I’m trying to find a functions.php code that will print the “Attributes” Order number I assign to a custom post type or page to the article HTML classes I can then style with CSS.

    <article itemtype="http://schema.org/CreativeWork" itemscope="itemscope"
    class="post-39 custom-post-type-example type-custom-post-type-example status-publish format-standard entry odd">

    In the above example CPT “Custom Post Type Example” if I assign a post with Attribute 4, I need it to add a class rank4 or similar.

    I figured out how to add odd/even to this (as bolded) through searching here:

    //* Add Odd/Even Class to WordPress Posts
    add_filter ( 'post_class' , 'oddeven_post_class' );
    	global $current_class;
    	$current_class = 'odd';
    
    function oddeven_post_class ( $classes ) {
       global $current_class;
       $classes[] = $current_class;
       $current_class = ($current_class == 'odd') ? 'even' : 'odd';
       return $classes;
    }

    Any way to add the attribute order as well? This will take my design to the next level. Thank you.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter Scott Buehler

    (@weborder)

    Spent another day looking for a solution to this. I take it this wouldn’t be possible in a function and would require actual theme template modifications?

    Moderator bcworkz

    (@bcworkz)

    I think there’s a way. You should be able to get the menu_order attribute from the global $post object. Like so:

    global $post;
    $current_class = 'Rank'.$post->menu_order;

    This only works inside the loop, which appears to be the case here.

    Thread Starter Scott Buehler

    (@weborder)

    Can’t seam to get the above to work.

    I would need a full function like my OP above to test it on my local host. I’m not a PHP coder unfortunately.

    Moderator bcworkz

    (@bcworkz)

    This should work for you, assuming you don’t need the odd even bit.

    // Add Menu Order Rank Class to WordPress Posts
    add_filter ( 'post_class' , 'menu_rank_post_class' );
    
    function menu_rank_post_class ( $classes ) {
       global $post;
       $classes[] = 'rank-'.$post->menu_order;
       return $classes;
    }

    Actually, this and your original code will work together by adding two different filters, independent of each other.

    Thread Starter Scott Buehler

    (@weborder)

    bcworks, brilliant! It works! You were right to think I would be replacing odd/even with this if I could get it to work.

    I appreciate this very much.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Adding CSS HTML Class for Attributes Order Menu_Order’ is closed to new replies.