I have a sidebar in wordpress that shows my recent posts. The php code that does this is simply:
$recent_posts = wp_get_recent_posts(array("numberposts"=>5));
I would like to include an IF statement to say:
"If the wordpress post is more than 100 words, display 10 recent posts, else display 5"
I will work out the relevant numbers etc. once I know how this is achieved.
You can use the global $post
to inspect the length of the post_content
and then set the $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));