0

I would like to use this function, but I don't know correctly what is the perfect way..

The code:

<li  id="post-<?php the_ID(); ?>" <?php post_class(get_the_content() == "" ? "no-content" : ""); ?> <?php post_class(has_post_thumbnail() ? "" : "no-image"); ?>>

There are two if statements. Currently only the first working, the second not but if I remove the first <?php post_class(get_the_content() == "" ? "no-content" : ""); ?> the second working. How can in combine these lines?

3

Use the post_class filter to test multiple conditions:

function wpa_post_class( $classes ) {
    global $post;
    if( ! has_post_thumbnail() ) $classes[] = "no-image";
    if( get_the_content() == "" ) $classes[] = "no-content";
    return $classes;
}
add_filter( 'post_class', 'wpa_post_class' );

Then your html will just be:

<li  id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
  • Thank you, just one question. Is it possible to use this function in the template file like my code above? – hepii110 Mar 30 '13 at 18:16
  • If your theme has a functions.php file, that's where the top snippet ought to go. The latter you can use in your template file. +1 – Johannes Pille Mar 30 '13 at 18:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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