我正在尝试显示基于this, 但我认为它工作不正常。当你查看一篇帖子时,它似乎会为每个帖子添加一个视图,而不仅仅是唯一的ID。
<?php
class SEM_Podcast_Widget extends WP_Widget
{
protected $defaults;
public function __construct()
{
// Set the default widget settings
$this->defaults = array(
\'title\' => \'\',
\'showdate\' => \'1\',
\'posts_cat\' => \'\',
\'postid\' => \'\',
\'showsummary\' => 1,
\'showfeatured\' => 1,
\'featuretype\' => \'latest\'
);
// Widget settings
$widget_ops = array(
\'classname\' => \'featuredpodcast\', // CSS classname for widget div
\'description\' => \'Displays the latest / featured podcast with excerpt and player.\' // Description shown in the WP Admin on the widgets page
);
// Widget control settings
$control_ops = array(
\'id_base\' => \'featured-podcast\',
\'width\' => 250,
\'height\' => 250
);
// Create the widget
$this->WP_Widget(\'featured-podcast\', \'SEM Geeks Featured Podcast Widget\', $widget_ops, $control_ops);
}
// prints the form on the widgets page
public function form ($instance)
{
// Merge with defaults
$instance = wp_parse_args((array) $instance, $this->defaults);
?>
<p>
<label for="<?php echo $this->get_field_id(\'title\'); ?>">Title:</label>
<input type="text" name="<?php echo $this->get_field_name(\'title\') ?>" id="<?php echo $this->get_field_id(\'title\') ?> " value="<?php echo $instance[\'title\'] ?>" size="25">
</p>
<p>
<label for="<?php echo $this->get_field_id(\'showdate\'); ?>">Date:</label>
<input type="checkbox" name="<?php echo $this->get_field_name(\'showdate\') ?>" id="<?php echo $this->get_field_id(\'showdate\') ?> " value="1" <?php checked($instance[\'showdate\']) ?>>
</p>
<p>
<label for="<?php echo $this->get_field_id(\'showsummary\'); ?>">Show Summary:</label>
<input type="checkbox" name="<?php echo $this->get_field_name(\'showsummary\') ?>" id="<?php echo $this->get_field_id(\'showsummary\') ?> " value="1" <?php checked($instance[\'showsummary\']) ?>>
</p>
<p>
<label for="<?php echo $this->get_field_id(\'posts_cat\'); ?>">Podcast Category:</label>
<?php
$categories_args = array(
\'name\' => $this->get_field_name(\'posts_cat\'),
\'selected\' => $instance[\'posts_cat\'],
\'orderby\' => \'Name\',
\'hierarchical\' => 1,
\'show_option_all\' => \'All Categories\',
\'hide_empty\' => \'0\'
);
wp_dropdown_categories( $categories_args ); ?>
</p>
<fieldset>
<legend>Select which podcast episode you would like to display:</legend>
<p>
<input type="radio" name="<?php echo $this->get_field_name(\'featuretype\'); ?>" id="<?php echo $this->get_field_id(\'featuretype\'); ?>_val1" value="latest" <?php checked($instance[\'featuretype\'], \'latest\'); ?>/>
<label for="<?php echo $this->get_field_id(\'featuretype\'); ?>_val1">Latest Podcast from Category</label><br />
<input type="radio" name="<?php echo $this->get_field_name( \'featuretype\' ); ?>" id="<?php echo $this->get_field_id(\'featuretype\'); ?>_val2" value="featured" <?php checked($instance[\'featuretype\'], \'featured\'); ?>/>
<label for="<?php echo $this->get_field_id(\'featuretype\'); ?>_val2">Featured Podcast (set id below)</label><br />
<label for="<?php echo $this->get_field_id(\'postid\'); ?>">Featured Post\'s ID:</label>
<input type="text" name="<?php echo $this->get_field_name(\'postid\') ?>" id="<?php echo $this->get_field_id(\'postid\') ?> " value="<?php echo $instance[\'postid\'] ?>" size="5">
</p>
</fieldset>
<?php
echo \'<p><b>\';
if (function_exists(\'powerpress_init\'))
echo \' PowerPress is installed. <br />\';
echo \'</b></p>\';
echo \'<p><b>Widget Shortcode:</b> <code>[featuredpodcast]</code></p>\';
}
// used when the user saves their widget options
public function update ($new_instance, $old_instance)
{
$instance = $old_instance;
$instance[\'title\'] = strip_tags($new_instance[\'title\']);
$instance[\'showdate\'] = $new_instance[\'showdate\'];
$instance[\'posts_cat\'] = $new_instance[\'posts_cat\'];
$instance[\'postid\'] = $new_instance[\'postid\'];
$instance[\'showsummary\'] = $new_instance[\'showsummary\'];
$instance[\'showfeatured\'] = $new_instance[\'showfeatured\'];
$instance[\'featuretype\'] = $new_instance[\'featuretype\'];
return $instance;
}
// used when the sidebar calls in the widget
public function widget ($args, $instance)
{
extract($args);
// Merge with defaults
$instance = wp_parse_args((array) $instance, $this->defaults);
echo $before_widget;
if (!empty($instance[\'title\']))
echo $before_title . apply_filters(\'widget_title\', $instance[\'title\'], $instance, $this->id_base) . $after_title;
// retrieve post information from database
if ($instance[\'featuretype\'] == \'featured\')
{
$query_args = array(
\'post_type\' => \'post\',
\'cat\' => $instance[\'posts_cat\'],
\'showposts\' => \'3\',
\'ignore_sticky_posts\' => true,
\'meta_key\' => \'post_views_count\',
\'orderby\' => \'meta_value_num\',
\'order\' => \'DESC\'
);
}
else
{
$query_args = array(
\'post_type\' => \'post\',
\'cat\' => $instance[\'posts_cat\'],
\'showposts\' => \'3\',
\'ignore_sticky_posts\' => true,
\'meta_key\' => \'post_views_count\',
\'orderby\' => \'meta_value_num\',
\'order\' => \'DESC\'
);
}
$featured_post = new WP_Query($query_args);
echo \'<img class="headphones-logo" src="\';
echo site_url();
echo \'/wp-content/themes/b4b/images/headphones.png"/>\';
if ($featured_post->have_posts())
{
while ($featured_post->have_posts())
{
$featured_post->the_post();
observePostViews(get_the_ID());
echo \'<div class="widget-inner-wrap">\';
if ($instance[\'showdate\'])
echo \'<span class="date">\';
echo get_the_date(\'m.d.Y\', $post->ID);
echo \'</span>\';
// Show hyperlinked post title
printf(\'<h3><a href="%s" title="%s">%s</a></h3>\', get_permalink(), the_title_attribute(\'echo=0\'), get_the_title());
// If selected, show excerpt from the post
if ($instance[\'showsummary\'])
the_excerpt(); // Show excerpt from the post
echo \'<span class="fp-player">\';
if ($instance[\'playerwidth\'] > 0)
$output_text = do_shortcode(\'[powerpress width="\' . $instance[\'playerwidth\'] . \'"px]\');
else
$output_text = do_shortcode(\'[powerpress]\');
echo $output_text;
echo \'</span>\';
// printf(\'<span class="fp-more"><a href="%s">[Read More...]</a></span>\', get_permalink());
echo \'</div>\';
}
echo \'<div class="widget-bottom-wrap">\';
echo \'<a class="view-more-podcasts" href="\';
echo site_url();
echo \'/category/podcast/">\';
echo "VIEW MORE PODCASTS";
echo \'</a>\';
echo \'<a href="https://itunes.apple.com/us/podcast/bad-4-business/id897719223?mt=2&ign-mpt=uo%3D4">\';
echo \'<img class="itunes-widget-logo" src="\';
echo site_url();
echo \'/wp-content/themes/b4b/images/itunes.png"/>\';
echo \'</div>\';
echo \'</a>\';
}
echo $after_widget;
wp_reset_query();
}
}
// Hook the widget up to WordPres
add_action(\'widgets_init\', create_function(\'\', \'return register_widget("SEM_Podcast_Widget");\'));