按视图显示帖子(热门帖子小工具)

时间:2014-09-10 作者:James Geiger

我正在尝试显示基于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");\'));

1 个回复
SO网友:phpsmashcode

可以使用会话进行修复,请尝试以下操作。

在函数中使用以下代码初始化会话。php。

    function init_sessions() {
    if (!session_id()) {
        session_start();
    }
}
add_action(\'init\', \'init_sessions\');
添加设置会话的功能

function set_session_posts_viewed($postID)
{
    if(empty($_SESSION[\'posts_viewed\']))
    {
        $posts_viewed = array($postID);
        $_SESSION[\'posts_viewed\'] = $posts_viewed;
    }
    else
    {
        $posts_viewed = $_SESSION[\'posts_viewed\'];
        $posts_viewed[] = $postID;
        $_SESSION[\'posts_viewed\'] = $posts_viewed;
    }
}
像这样更改日志计数更新代码

function observePostViews($postID) {
    $count_key = \'post_views_count\';
    $count = get_post_meta($postID, $count_key, true);
    if(!isset($_SESSION[\'posts_viewed\']) || (isset($_SESSION[\'posts_viewed\']) && !in_array($postID, $_SESSION[\'posts_viewed\'])))
    {
        if($count==\'\'){
            $count = 0;
            delete_post_meta($postID, $count_key);
            add_post_meta($postID, $count_key, \'0\');
            set_session_posts_viewed($postID);
        }else{
            $count++;
            update_post_meta($postID, $count_key, $count);
            set_session_posts_viewed($postID);
        }
    }
}
希望它能解决这个问题。如果你有困难,请告诉我。

结束

相关推荐

Multiple widgets in wordpress

我已经创建了一个wordpress小部件。它在小部件选项中有一个下拉列表。下拉列表包含“facebook”和“twitter”。如果管理员选择“twitter”,那么twitter关注者计数将显示在小部件中,与facebook的情况类似。使用jquery,该计数显示在id为“social count”的div中。当页面加载时,jquery ajax将获取计数并将其插入到“社交计数”中。问题是它在单个实例中运行良好。但是,如果在侧栏中添加了2个实例,则在这两个实例中都会显示最新的计数,因为我使用的是$(“.