0

enter image description here

I was creating a wordpress sidebar widget that will display recent posts. For, this i've created a custom widget(popular_posts_widget.php) and included in my theme functions.php. I was trying to display post titles which when clicked display respective posts associated with it. But, unfortunately link doesn't work. How can i fix this??

This is my functions.php

<?php 

/****************************************************/
/* Define Constants */   
/***************************************************/

define ('THEMEROOT', get_stylesheet_directory_uri());
define ('IMAGES', THEMEROOT . '/img' );
define ('FONTS', THEMEROOT . '/fonts' );
define ('JS', THEMEROOT . '/js' );

/****************************************************/
/* Menus */  
/***************************************************/

function register_my_menus() {
         register_nav_menus(array(

         'main-menu'=> __('Main Menu')
         ));
}
add_action('init' , 'register_my_menus');

if(function_exists('register_sidebar')) {

    register_sidebar(
    array(
         'name' => __('Main Sidebar'),
         'id' => 'main-sidebar',
         'description' => __('The main sidebar area'),
         'before_widget' => '<div class="widget">',
         'after_widget' => '</div>',
         'before_title' => '<h2>',
         'after_title' => '</h2>'
   ));

}

/****************************************************/
/* Add Theme Support for post thumbnail */  
/***************************************************/

if(function_exists('add_theme_support')) {
   add_theme_support('post-formats', array('link', 'quote', 'gallery'));
   add_theme_support('post-thumbnails', array('post'));
}

/****************************************************/
/* Load Custom Widgets */  
/***************************************************/

require_once('functions/widget-ad-260.php');
require_once('functions/popular-posts-widget.php');
require_once('functions/rh_about.php');
require_once('functions/rh_tags.php');

 ?>

This is my popular-posts-widget.php

<?php 

/****************************************************/
/* Widget that displays a popular posts widget */  
/***************************************************/

class Popular extends WP_Widget {
    function __construct() {
        $params = array(
            'name' => 'Creative : Popular Posts Widget',
            'description' => 'Displays recent posts of your blog'
            );
        parent:: __construct('Popular','',$params);
    }

    public function form($instance) {
        //display our form in widget page
        extract($instance);
        ?>
        <p>
            <label for="<?php echo $this->get_field_id('title') ?>">Title : </label>
            <input class="widefat" id="<?php echo $this->get_field_id('title') ?>" name="<?php echo $this->get_field_name('title') ?>" 
            value="<?php if(isset($title)) echo esc_attr($title); ?>" />
        </p>
        <?php
    }

    public function widget($args,$instance) {
        //displays our widget
        extract($args);
        extract($instance);
        echo $before_widget;
           echo $before_title .$title. $after_title;
           echo '<div class="popular">';
           if(have_posts()) {
            while (have_posts()) {
                the_post();
                echo "<ul>";
                echo "<li>";
                $link = the_permalink();
                echo "<a href=$link>";
                echo the_title();
                echo "</a>";
                echo "<p>";
                echo the_time(get_option('date_format'));
                echo "</p>";
                echo "</li>";
                echo "</ul>";
            }
           }
           else {
            echo "<h1>No posts were found!</h1>";
           }
           echo '</div>';
        echo $after_widget;
    }
} 

add_action('widgets_init','rh_register_popular');
function rh_register_popular() {
    register_widget('Popular');
}

 ?>

You can also check preview listed above.

1

This is the problem:

$link = the_permalink();

Replace it with:

$link = get_permalink();

Notice that the WordPress the_* functions will echo the output, but get_* will return it. But of course there are exceptions to this rule ;-) So you don't need this:

echo the_title();

Just use this instead:

the_title();

or the usage example given by @PieterGoosen.

Don't use extract() as it's bad for debugging and deprecated in the core. See for example this answer by @toscho.

1

Just to add to the answer from @birgire, you can also just use the_title( $before, $after, $echo ); in the following matter to get the same result

the_title( '<h1 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h1>' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.