我正在创建一个wordpress边栏小部件,它将显示最近的帖子。为此,我创建了一个自定义小部件(popular_posts_widget.php) 并包含在我的主题中functions.php. 我试图显示帖子标题,点击后会显示与之相关的各个帖子。但不幸的是,链接不起作用。我怎样才能解决这个问题??
这是我的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\');
?>
这是我的
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\');
}
?>
您还可以检查上面列出的预览。