WordPress插件无法检查热门帖子

时间:2013-01-28 作者:swapnesh

我在wordpress中创建了一个插件来显示热门帖子,但这并不能正确返回帖子和评论数。

甚至我在我的帖子中也有评论(也被批准了,所以不是这样)它显示-

"No commented post this time!!";
虽然如果我把echo 在78号线附近的这条线上-$popular .= \'</li>\'; 它显示了流行的帖子,尽管我认为仍然有一些错误(无论是逻辑上还是代码上)。

已设置调试选项true define(\'WP\\u DEBUG\',true);

我的代码-->

<?php
/*
Plugin Name: Sw Popular Post Widget
Plugin URI: http://demo.test.com
Author: Swapnesh Kumar Sinha
Description: Go your Widget Section to check the widget. It is a general purpose plugin that creates a widget to limit the popular post display from 1 to 10 at front end of your site. Please leave your honest comments,suggestions, bugs etc regarding the plugin at my email id - "[email protected]". The plugin is in working condition, tested at - WordPress 3.5.
########
INSTALL it via plugin section in your Wordpress Admin panel and then find it under APPEARANCE -> WIDGETS section.
######## 
Version: 1.0
Author URI: http://swapneshsinha.wordpress.com
*/

class SwPopularPostWidget extends WP_Widget {

public function __construct() {
    //Widget actual processes
    $widget_options = array( \'Description\' => \'Show Popular Posts\' );
    parent::__construct( \'SwPopularPostWidget\', \'Sw Popular Post Widget\', $widget_options); 
}

public function form( $instance ) {
    //outputs the options form on admin
$instance = wp_parse_args( (array) $instance, array( \'popularpostcount\' => \'\',\'popularpostheading\' => \'\' ) );
$popularpostcount = $instance[\'popularpostcount\'];
$popularpostheading = $instance[\'popularpostheading\'];
echo "Heading Title";   
echo "<input type=\'text\' name=\'".$this->get_field_name(\'popularpostheading\')."\' id=\'".$this->get_field_name(\'popularpostheading\')."\' value=\'".$popularpostheading."\' />";
echo "Number of post to show "; 
//echo "<select name=\'popularpostcount\' id=\'popularpostcount\'>";
echo "<select name=\'".$this->get_field_name(\'popularpostcount\')."\' id=\'".$this->get_field_name(\'popularpostcount\')."\'>";
    for( $i =1; $i<=10; $i++ )
    {
        if ( $i == $popularpostcount )
        echo "<option value=\'".$i."\' selected>".$i."</option>";
        else
        echo "<option value=\'".$i."\'>".$i."</option>";
    } 
echo "</select>";
}

public function update( $new_instance, $old_instance ) {
    //processes widget options to be saved
    $instance = $old_instance;
    $instance[\'popularpostcount\'] = $new_instance[\'popularpostcount\'];
    $instance[\'popularpostheading\'] = $new_instance[\'popularpostheading\'];
    return $instance;
}

public function widget( $args ,  $instance ) {
    //outputs the content of widget
    extract($args);
    $popularpostcount = apply_filters(\'widget_title\', $instance[\'popularpostcount\']);
    $popularpostheading = apply_filters(\'widget_title\', $instance[\'popularpostheading\']);
    echo \'<aside class="widget widget_meta" id="popular-2">\';
    echo  \'<h3 class="widget-title">\'.$popularpostheading.\'</h3>\';
    echo  "<ul>";
    echo $this->popularPosts($popularpostcount);
    echo  "</ul>";
}


public function popularPosts($num)
{
     global $wpdb;

    $posts = $wpdb->get_results("SELECT * FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , $num");
    //echo "<pre>";print_r($posts);die;
    foreach ($posts as $post) {
        setup_postdata($post);
        $id = $post->ID;
        $title = $post->post_title;
        $count = $post->comment_count;

        if ($count != 0) {
            $popular  = \'<li>\';
            $popular .= \'<a href="\' . get_permalink($id) . \'" title="">\' . $title.\'</a> \'." - ".$count." Comments";
            $popular .= \'</li>\';
        }
        else
        $popular = "No commented post this time!!";
    }
    return $popular;
}


}

add_action( \'widgets_init\' , create_function(\'\', \'register_widget( "swpopularpostwidget" );\' ) );

1 个回复
SO网友:kallookoo

一种可能性是:

public function popularPosts($num)
{
    global $post;
    $args = array( \'orderby\' => \'comment_count\', \'order\' => \'DESC\' );
    $posts = get_posts( $args );
    $count = 0;
    foreach ($posts as $post):
        if($post->comment_count != 0 ){
            $count++;
        }   
    endforeach;
    if ( $count > 0 ):
        foreach( $posts as $post ) : setup_postdata($post);
            if($post->comment_count != 0 ){ 
                ?>
                <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> - <?php echo $post->comment_count; ?> Comments</li>
                <?php
            }
        endforeach;
    else :
        echo \'<li>No commented post this time!!</li>\';
    endif;
}


}
function swpopularpostwidget_init() {
    if ( !is_blog_installed() )
        return;

    register_widget(\'swpopularpostwidget\');
    do_action(\'widgets_init\');
}
add_action( \'init\' , \'swpopularpostwidget_init\');

结束

相关推荐

Google Map在Tab Plugins的第二个选项卡上无法使用

我正在使用postTabs plugin 和Comprehensive Google Map Plugin, 我的问题是,当我在第二个选项卡上有我的谷歌地图时,地图没有按预期加载。但是如果我把它移到第一个标签上,效果会非常好。。有没有办法让地图在第二个选项卡上工作?实际上,无论我使用哪个选项卡插件,地图都不会正确加载到第二个选项卡上。。欢迎提出任何建议。谢谢:)