I am creating a WordPress custom widget and I created the below one that is working fine when I add it in function.php
.
<?php
/* ------------------------------------------------------------------------- *
* Most Commented Post Widget
/* ------------------------------------------------------------------------- */
class show_popular_commented extends WP_Widget {
function show_popular_commented() {
$widget_ops = array('classname' => 'show_popular_commented', 'description' => __('Show your popular commented posts.'));
$this->WP_Widget('show_popular_commented', __('EXE_Widgets - Popular Commented Posts'), $widget_ops);
}
function widget($args, $instance){
extract($args);
$title = $instance['title'];
$postscount = $instance['posts'];
//Show The Most Commented Posts
global $postcount;
$myposts = get_posts(array('orderby' => 'comment_count','numberposts' =>$postscount));
echo $before_widget . $before_title . $title . $after_title; //Widget Data
echo '
<style type="text/css">
.commented_post {display:block;margin:10px 0;border-bottom:1px solid #DEDEDE;}
.commented_post h4{font-size:16px;clear:both;display:block;}
.commented_post p{font-size:13px;text-align:justify;line-height:18px;margin:10px 0;}
</style>
';
foreach($myposts as $post){
setup_postdata($post);
echo '<div class="commented_post">';
echo '<h4><a href="';
echo the_permalink();
echo '">';
echo the_title();
echo'</a></h4>';
echo the_excerpt();
echo '</div>';
}
echo $after_widget; //Widget Data
//Show The Most Commented Posts
}
function update($newInstance, $oldInstance){
$instance = $oldInstance;
$instance['title'] = strip_tags($newInstance['title']);
$instance['posts'] = $newInstance['posts'];
return $instance;
}
function form($instance){
if(empty($instance['title'])){ $instance['title'] = '';}{
echo '<p><label for="'.$this->get_field_id('title').'">' . __('Title:') . ' </label><input style="width:100%;" id="'.$this->get_field_id('title').'" name="'.$this->get_field_name('title').'" type="text" value="'.$instance['title'].'" /></p>';
}
if(empty($instance['posts'])){ $instance['posts'] = '';}{
echo '<p><label for="'.$this->get_field_id('posts').'">' . __('Number of Posts:', 'widgets') . ' </label><input style="width:50px;" id="'.$this->get_field_id('posts').'" name="'.$this->get_field_name('posts').'" type="text" value="'.$instance['posts'].'" /></p>';
}
echo '<input type="hidden" id="custom_recent" name="custom_recent" value="1" />';
}
}
add_action('widgets_init', create_function('', 'return register_widget("show_popular_commented");'));
?>
Now the problem is that I don't want to use too many echo'';
as I used in the above code...
echo '<div class="commented_post">';
echo '<h4><a href="';
echo the_permalink();
echo '">';
echo the_title();
echo'</a></h4>';
echo the_excerpt();
echo '</div>';
But when I use one echo'';
and code the above one like below then my widget is not working properly. It is getting all variables data but not the HTML tags that I wrote in echo'';
...
echo '<div class="commented_post"><h4><a href="'.the_permalink().'">'.the_title().'</a></h4>'.the_excerpt().'</div>';
Can you answer me why it is not accepting single echo'';
?
extract()
and most of all never usecreate_function()