-2

Is it possible to add a number to the body class according to how many custom posts a current user has published, e.g. CUSTOMPOST-4

2
  • What do you mean by "current user"?
    – engelen
    Feb 6, 2017 at 12:55
  • the current logged in user/author
    – Pete
    Feb 6, 2017 at 12:56

2 Answers 2

1

add to functions.php

function wpc_body_class_section($classes) {  
    if (is_user_logged_in()) {        
        $classes[] = 'CUSTOMPOST-'.count_user_posts(get_current_user_id());
    } 

    return $classes;  
}  
add_filter('body_class','wpc_body_class_section');  
2

You can use count_user_posts

See:

https://developer.wordpress.org/reference/functions/count_user_posts/

Edit:

<?php
    if (is_user_logged_in()) {
       $count_class = 'CUSTOMPOST-'.count_user_posts(get_current_user_id());
    }
?>
<body <?php body_class($count_class); ?>> 

Something like this in your header

2
  • How would I use that function to add it to the body class?
    – Pete
    Feb 25, 2017 at 3:40
  • See the edit, included the example code Feb 27, 2017 at 7:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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