php - Display recent posts based on post size using IF STATEMENT -
i have sidebar in wordpress shows recent posts. php code simply:
$recent_posts = wp_get_recent_posts(array("numberposts"=>5));
i include if statement say:
"if wordpress post more 100 words, display 10 recent posts, else display 5"
i work out relevant numbers etc. once know how achieved.
you can use global $post
inspect length of post_content
, set $numberposts
accordingly.
global $post; $numberposts = 1; // default number of posts if ( !empty($post) ){ $len = strlen( $post->post_content ); // change $numberposts based on length of $post->post_content if ( $len < 300 ){ $numberposts = 8; } elseif ( $len < 500 ){ $numberposts = 5; } elseif ( $len < 800 ){ $numberposts = 3; } else { $numberposts = 1; } } $recent_posts = wp_get_recent_posts(array("numberposts"=>$numberposts));
Comments
Post a Comment