Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Custom Widget Request WordPress



I am in need of a custom widget that I can add to the functions.php file of my wordpress theme.

I would like a custom widget for 'recent posts'. I would like for it to have the exact same functionality as the default 'recent posts' widget (allow user to enter title and choose how many posts to display).

The only thing I would like changed is the output. Instead of just displaying the post title with the link to the post, I would like to display the post title and below that the first 25 words of the post content with a "..." added at the end.

Here is the static HTML to give you an idea of how I'd like the the final output to appear. The post title should be in an <h4> and the post content below it should be wrapped in a <p class="footer_post">. Also, the post content should link to the post.

<h4>March 7, 2010</h4>
<p class="footer_post"><a href="#">Aunt Polly placed small trust in such evidence. She went out to see for herself; and she would have been content to find twenty per cent. of Tom’s statement true...</a></p>

<h4>February 28, 2010</h4>
<p class="footer_post"><a href="#">Within two minutes, or even less, he had forgotten all his troubles. Not because his troubles were one whit less heavy and bitter to him than...</a></p>

<h4>February 14, 2010</h4>
<p class="footer_post"><a href="#">Tom appeared on the sidewalk with a bucket of whitewash and a long-handled brush. He surveyed the fence, and all gladness left him and a deep melancholy settled...</a></p>

Answers (2)


Buzu B answers:

No worries, currently working on it... a widget from scratch so you don't have to do nothing else that just copy & paste...

WP Answers comments:

You rock. Thank you.

Buzu B comments:

Hey. Where do you want the link to point to?

This is what I have so far. Just need to know where the link is supposed to link. I'm guessing it links to the post, but since you put only a # I have just put a # as well.

//all this block was added by BUZU. widget practice.
function show_recent(){
if ( !function_exists('register_sidebar_widget') )

function widget_gsearch($args) {

$options = get_option('custom_recent');
$title = $options['title'];
$posts = $options['posts'];
//GET the posts
$myposts = get_posts('numberposts='.$posts.'&offset=1');

echo $before_widget . $before_title . $title . $after_title;

//SHOW the posts
foreach($myposts as $post){
<h4><?php the_date(); ?></h4>
<p class="footer_post"><a href="#"><?php echo substr($post->post_content, 0, 10); ?>...</a></p>


echo $after_widget;

function widget_control() {

$options = get_option('custom_recent');
if ( !is_array($options) )
$options = array('title'=>'Most Recent Posts', 'posts'=>5);
if ( $_POST['custom_recent'] ) {

$options['title'] = strip_tags(stripslashes($_POST['custom_recent_title']));
$options['posts'] = strip_tags(stripslashes($_POST['custom_recent_number']));
update_option('custom_recent', $options);

$title = htmlspecialchars($options['title'], ENT_QUOTES);
$posts = htmlspecialchars($options['posts'], ENT_QUOTES);

echo '<p style="text-align:right;"><label for="custom_recent_title">' . __('Title:') . ' <input style="width: 200px;" id="custom_recent_title" name="custom_recent_title" type="text" value="'.$title.'" /></label></p>';
echo '<p style="text-align:right;"><label for="custom_recent_number">' . __('Number of Posts:', 'widgets') . ' <input style="width: 50px;" id="custom_recent_number" name="custom_recent_number" type="text" value="'.$posts.'" /></label></p>';
echo '<input type="hidden" id="custom_recent" name="custom_recent" value="1" />';

register_sidebar_widget(array('Custom Recent Posts', 'widgets'), 'widget_gsearch');

register_widget_control(array('Custom Recent Posts', 'widgets'), 'widget_control', 255, 100);

add_action('widgets_init', 'show_recent');

Nice and Shiny.

This part:
<p class="footer_post"><a href="#"><?php echo substr($post->post_content, 0, 10); ?>...</a></p>
is what cuts the post to show only a part of it. I'm showing only 10 characters. Adjust the number to your needs.

Just copy & paste into your functions.php file and you are all set. Remember your theme has to support widgets.

WP Answers comments:

Thank you very much for this, but it doesn't seem to be working 100% correctly.

- The link should go to the post's permalink. I tried adding "<?php the_permalink() ?>" instead of "#", but that gives the permalink of the page you are currently on. Do you know how to link to the post?

- It doesn't appear to be displaying the correct dates. I have it set to display 3 items, and all 3 items are showing the same date even though all 3 items have different dates. Any ideas how to fix?

Buzu B comments:

Sorry... my mistake. I forgot to add a line U_U!

Find this:

//GET the posts

and ad this:

global $post;

so it looks like this:

//GET the posts
global $post;

That fixes the date and the permalink

WP Answers comments:

No worries at all. The dates and permalinks are now working.

We are almost there.

- It seems that if all my posts have the same date, the date only shows for the 1st item, and no dates are displayed for the other items.

- Also, it appears to be 'behind' by one post. When I make a new post, it doesn't get added right away, but the most recent post before that one will get added.

Buzu B comments:

change this:

$myposts = get_posts('numberposts='.$posts.'&offset=1');

to this:

$myposts = get_posts('numberposts='.$posts.'&offset=0');

I thought since the last post was int the front page, it'd be good to show only starting from the second. The dates is a bit strange... I'm working on it.

WP Answers comments:

Ok sounds great. I switched the offset and all is well there. Thank you for that.

Buzu B comments:

Ok, solved it... change the part that prints the date for this:

<h4><?php the_time(get_option('date_format')); ?></h4>

You are replacing this:
<h4><?php the_date(); ?></h4>
with this:
<h4><?php the_time(get_option('date_format')); ?></h4>

That fixes it.

WP Answers comments:

That did the trick.

Thank you again for all of your hard work.

I don't know if you saw it, but I posted a question to the custom options panel you helped me out with earlier. I am able to save everything now, but for some reason I'm unable to retrieve any of the data.

I need to go to sleep right now, but perhaps tomorrow you might be able to take another look at that question? I can provide you more payment if needed.

Thanks again - good night.

Buzu B comments:

Hi, I saw the question on the other post, but you had already closed the question so couldn't answer. I'll be glad to take a look at the problem. Please contact me tomorrow.

Buzu B comments:

Let me know how everything goes...


Dan Davies answers:

Have you checked this plugin out?

WP Answers comments:


The plugin looks pretty cool, but I need this widget to be built into the theme as this is going to be a premium wordpress theme for sale.

Dan Davies comments:

You could probably get away with copying and pasting the code from the widget into the functions.php file. You might need to contact the plugin author if you were to do that though.