I have create a custom piece of code that grabs the most recent post, and displays it on the home page in a fixed size box.
If the most recent post is really small, it will grab the second post as well.
Only a summary of the post is displayed.
The text is truncated after a specified number of characters.
images are stripped out, as I only want the text to display. I want to maintain hyperlinks so that they still work.
The problem I am having is the following 2 things:
-- Becuase i am counting the characters, hidden text - primarily html elements (ie a hyperlink) are counted, even though these are not visible text when displayed = results in a shorter than anticipate summary
-- Sometimes the substring cuts off in the middle of of a hyperlink, thus breaking the hyperlink and causing other problems (in one case the read more link that is appended afterwards started hyperlinking to the wrong place)
any help or suggestions would be greatly appreciated. I have been looking at an investigating the evermore plugin. whilst this does not fix my problem, i thought i might be able to borrow some of the ideas.
ideally i need to cut the text and then just check that i am not in the middle of a hyperlink, or other html tag. I would also like to only count the visible text when choosing the truncation point, so that i can get a more consistent length.
I appologise that this is not the most elegant piece of code!
Code: Select all
<?php
$numberofposts = 1; //this value gets changed by the code below..
$summary_length = 350; //this value can get changed by the code below...
$post_is_too_small_length = 200;
$short_summary_length = 210;
//get first 2 posts, and check their length. use this to decide how many posts to display on home page.
$posts = get_posts("numberposts=".$numberofposts);
foreach ($posts as $post)
{
$full_content = apply_filters('the_content', $post->post_content);
$content_less_img = strip_tags($full_content, '<p><a><h2><blockquote><code><ul><li><i><em><strong>'); //content with images stripped. only tags listed will not be removed.
//if the post is really short, then we want to go and grab the next post but we also want to make the length a bit shorter, in case the next post is really long
$post_length = strlen($content_less_img);
if ($post_length > $post_is_too_small_length)
{
$numberofposts = 1;
break; //first post is long enough, so no need to check any further
}
elseif($post_length <= $post_is_too_small_length)
{
$numberofposts = 2;
$summary_length = $short_summary_length;
}
else
{
$numberofposts = 1;
}
}
//now get the content for display
$full_content='';
echo '<h3>Latest News</h3><br />';
$posts = get_posts("numberposts=".$numberofposts);
foreach ($posts as $post)
{
echo '<strong><a href="'.get_permalink($post->ID).'">'.$post->post_title.'</a></strong><br />';
echo the_time('<\i>F jS, Y</\i>').'<br />';
$full_content = apply_filters('the_content', $post->post_content);
$content_less_img = strip_tags($full_content, '<p><a><h2><blockquote><code><ul><li><i><em><strong><br /><span>'); //content with images stripped. only tags listed will not be removed.
$short_content = substr($content_less_img,0,$summary_length);
echo '<span>'.$short_content.'</span>';
echo '<a href="'.get_permalink($post->ID).'">Read More...</a><br /><br />';
}
?>