W3总缓存和POST__NOT_IN

时间:2014-09-01 作者:Sebastian Starke

我正在使用最近发布的插件的修改版本以及WP Total Cache。我正在使用post\\uu not\\u in,因为我不想再次显示当前帖子。它工作得很好,但如果对象缓存被激活,在某些帖子上,post\\u not\\u in不起作用。

这是我的代码:

<?php function nothing_recent_posts_init() {
                register_widget(\'nothing_recent_posts\');
            }
            add_action(\'widgets_init\', \'nothing_recent_posts_init\');


            class nothing_recent_posts extends WP_Widget {

                function __construct() {
                    $widget_ops = array(\'classname\' => \'nothing_recent_posts\', \'description\' => __( "Customized version of Recent Posts. You can choose what to display and add custom classes.") );
                    parent::__construct(\'nothing-recent-posts\', __(\'Customized Recent Posts\'), $widget_ops);
                    $this->alt_option_name = \'nothing_recent_entries\';

                    add_action( \'save_post\', array($this, \'flush_widget_cache\') );
                    add_action( \'deleted_post\', array($this, \'flush_widget_cache\') );
                    add_action( \'switch_theme\', array($this, \'flush_widget_cache\') );
                }

                function widget($args, $instance) {
                    $cache = wp_cache_get(\'nothing_recent_posts\', \'widget\');

                    if ( !is_array($cache) )
                        $cache = array();

                    if ( ! isset( $args[\'widget_id\'] ) )
                        $args[\'widget_id\'] = $this->id;

                    if ( isset( $cache[ $args[\'widget_id\'] ] ) ) {
                        echo $cache[ $args[\'widget_id\'] ];
                        return;
                    }

                    ob_start();
                    extract($args);

                    $title = ( ! empty( $instance[\'title\'] ) ) ? $instance[\'title\'] : __( \'Recent Posts\' );
                    $title = apply_filters( \'widget_title\', $title, $instance, $this->id_base );
                    $number = ( ! empty( $instance[\'number\'] ) ) ? absint( $instance[\'number\'] ) : 10;
                    if ( ! $number )
                        $number = 10;
                    $show_title = isset( $instance[\'show_title\'] ) ? $instance[\'show_title\'] : false;
                    $show_date = isset( $instance[\'show_date\'] ) ? $instance[\'show_date\'] : false;
                    $show_img = isset( $instance[\'show_img\'] ) ? $instance[\'show_img\'] : false;
                    $custom_class = ( ! empty( $instance[\'custom_class\'] ) ) ? $instance[\'custom_class\'] : \' \';
                    $query_by_cat = isset( $instance[\'query_by_cat\'] ) ? $instance[\'query_by_cat\'] : false;

                    if( $query_by_cat ){
                        $current_category = wp_get_post_categories( get_the_id() );
                        $r = new WP_Query( apply_filters( \'widget_posts_args\', array(
                            \'category__in\' => $current_category,
                            \'posts_per_page\' => $number,
                            \'no_found_rows\' => true,
                            \'post_status\' => \'publish\',
                            \'ignore_sticky_posts\' => true,
                            \'post__not_in\' => array(get_the_id())
                        ) ) );
                    }else{
                        $r = new WP_Query( apply_filters( \'widget_posts_args\', array(
                            \'posts_per_page\' => $number,
                            \'no_found_rows\' => true,
                            \'post_status\' => \'publish\',
                            \'ignore_sticky_posts\' => true,
                            \'post__not_in\' => array(get_the_id())
                        ) ) );
                    }
                    if ($r->have_posts()) : ?>
                    <?php echo $before_widget; ?>
                    <?php if ( $title ) echo $before_title . $title . $after_title; ?>

                    <?php while ( $r->have_posts() ) : $r->the_post(); ?>

                    <article class="hentry <?php echo $custom_class; ?>">
                        <a href="<?php the_permalink(); ?>" rel="bookmark" title=\'Den Beitrag "<?php the_title(); ?>" ansehen\'>

                            <?php if ( $show_img ) : ?>
                            <div class="entry-thumbnail">
                                <?php the_post_thumbnail( \'thumbnail\' ); ?>
                            </div><!-- .entry-thumbnail -->
                            <?php endif; ?>

                            <?php if ( $show_title ) : ?>
                            <header class="widget-entry-header">
                            <div class="inner">
                                <h2><?php the_title(); ?></h2>
                            </div>
                            </header><!-- .entry-header -->
                            <?php endif; ?>

                            <?php if ( $show_date ) : ?>
                                <span class="post-date"><?php echo get_the_date(); ?></span>
                            <?php endif; ?>
                        </a>
                    </article>
                    <?php endwhile; ?>

                    <?php echo $after_widget; ?>
                    <?php wp_reset_postdata();

                    endif;

                    $cache[$args[\'widget_id\']] = ob_get_flush();
                    wp_cache_set(\'nothing_recent_posts\', $cache, \'widget\');
                }

                function update( $new_instance, $old_instance ) {
                    $instance = $old_instance;
                    $instance[\'title\'] = strip_tags($new_instance[\'title\']);
                    $instance[\'number\'] = (int) $new_instance[\'number\'];
                    $instance[\'show_date\'] = isset( $new_instance[\'show_date\'] ) ? (bool) $new_instance[\'show_date\'] : false;
                    $instance[\'show_title\'] = isset( $new_instance[\'show_title\'] ) ? (bool) $new_instance[\'show_title\'] : false;
                    $instance[\'show_img\'] = isset( $new_instance[\'show_img\'] ) ? (bool) $new_instance[\'show_img\'] : false;
                    $instance[\'custom_class\'] = strip_tags($new_instance[\'custom_class\']);
                    $instance[\'query_by_cat\'] = isset( $new_instance[\'query_by_cat\'] ) ? (bool) $new_instance[\'query_by_cat\'] : false;
                    $this->flush_widget_cache();

                    $alloptions = wp_cache_get( \'alloptions\', \'options\' );
                    if ( isset($alloptions[\'nothing_recent_entries\']) )
                        delete_option(\'nothing_recent_entries\');

                    return $instance;
                }

                function flush_widget_cache() {
                    wp_cache_delete(\'nothing_recent_posts\', \'widget\');
                }

                function form( $instance ) {
                    $title     = isset( $instance[\'title\'] ) ? esc_attr( $instance[\'title\'] ) : \'\';
                    $number    = isset( $instance[\'number\'] ) ? absint( $instance[\'number\'] ) : 5;
                    $show_title = isset( $instance[\'show_title\'] ) ? (bool) $instance[\'show_title\'] : true;
                    $show_date = isset( $instance[\'show_date\'] ) ? (bool) $instance[\'show_date\'] : false;
                    $show_img = isset( $instance[\'show_img\'] ) ? (bool) $instance[\'show_img\'] : false;
                    $custom_class     = isset( $instance[\'custom_class\'] ) ? esc_attr( $instance[\'custom_class\'] ) : \'\';
                    $query_by_cat = isset( $instance[\'query_by_cat\'] ) ? (bool) $instance[\'query_by_cat\'] : false;
            ?>
                    <p><label for="<?php echo $this->get_field_id( \'title\' ); ?>"><?php _e( \'Title:\' ); ?></label>
                    <input class="widefat" id="<?php echo $this->get_field_id( \'title\' ); ?>" name="<?php echo $this->get_field_name( \'title\' ); ?>" type="text" value="<?php echo $title; ?>" /></p>

                    <p><label for="<?php echo $this->get_field_id( \'number\' ); ?>"><?php _e( \'Number of posts to show:\' ); ?></label>
                    <input id="<?php echo $this->get_field_id( \'number\' ); ?>" name="<?php echo $this->get_field_name( \'number\' ); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>

                    <p><input class="checkbox" type="checkbox" <?php checked( $show_title ); ?> id="<?php echo $this->get_field_id( \'show_title\' ); ?>" name="<?php echo $this->get_field_name( \'show_title\' ); ?>" />
                    <label for="<?php echo $this->get_field_id( \'show_title\' ); ?>"><?php _e( \'Display post title?\' ); ?></label></p>

                    <p><input class="checkbox" type="checkbox" <?php checked( $show_date ); ?> id="<?php echo $this->get_field_id( \'show_date\' ); ?>" name="<?php echo $this->get_field_name( \'show_date\' ); ?>" />
                    <label for="<?php echo $this->get_field_id( \'show_date\' ); ?>"><?php _e( \'Display post date?\' ); ?></label></p>

                    <p><input class="checkbox" type="checkbox" <?php checked( $show_img ); ?> id="<?php echo $this->get_field_id( \'show_img\' ); ?>" name="<?php echo $this->get_field_name( \'show_img\' ); ?>" />
                    <label for="<?php echo $this->get_field_id( \'show_img\' ); ?>"><?php _e( \'Display post image?\' ); ?></label></p>

                    <p><label for="<?php echo $this->get_field_id( \'custom_class\' ); ?>"><?php _e( \'Custom class:\' ); ?></label>
                    <input class="widefat" id="<?php echo $this->get_field_id( \'custom_class\' ); ?>" name="<?php echo $this->get_field_name( \'custom_class\' ); ?>" type="text" value="<?php echo $custom_class; ?>" /></p>

                    <p><input class="checkbox" type="checkbox" <?php checked( $query_by_cat ); ?> id="<?php echo $this->get_field_id( \'query_by_cat\' ); ?>" name="<?php echo $this->get_field_name( \'query_by_cat\' ); ?>" />
                    <label for="<?php echo $this->get_field_id( \'query_by_cat\' ); ?>"><?php _e( \'Filter by current category?\' ); ?></label></p>

            <?php
                }
            }

2 个回复
最合适的回答,由SO网友:Mark Kaplun 整理而成

如果您遵循了最近的页面小部件的代码,那么您想要实现的目标就不能像您想要实现的那样天真。最近发布的小部件缓存基于以下假设:相同的输出显示在任何地方,这就是为什么它可以在任何页面上计算缓存并在任何其他页面上使用缓存,但此假设不适用于您的情况,因为您希望在某些页面上有不同的输出。

最简单的方法是为每个帖子(如最近的帖子)使用不同的缓存键{post id}。

SO网友:Frederick Townes

您是否确保缓存密钥中没有可能的冲突;i、 e.确保在密钥命名中使用特定于post的哈希?

结束

相关推荐

How can I find plugins' slug?

我想知道如何才能找到插件的slug(slug=WordPress用来更新插件和确定哪些插件当前处于活动状态的内部名称)?它通常是插件的文件夹名,但如果插件没有文件夹,则是其文件名(如hello.php)。是否有其他例外情况?大小写字符重要吗</插件的slug是否可以与其文件夹名不同?如果有一个叫做hello的插件呢。php和另一个/您好。php/您好。php

W3总缓存和POST__NOT_IN - 小码农CODE - 行之有效找到问题解决它

W3总缓存和POST__NOT_IN

时间:2014-09-01 作者:Sebastian Starke

我正在使用最近发布的插件的修改版本以及WP Total Cache。我正在使用post\\uu not\\u in,因为我不想再次显示当前帖子。它工作得很好,但如果对象缓存被激活,在某些帖子上,post\\u not\\u in不起作用。

这是我的代码:

<?php function nothing_recent_posts_init() {
                register_widget(\'nothing_recent_posts\');
            }
            add_action(\'widgets_init\', \'nothing_recent_posts_init\');


            class nothing_recent_posts extends WP_Widget {

                function __construct() {
                    $widget_ops = array(\'classname\' => \'nothing_recent_posts\', \'description\' => __( "Customized version of Recent Posts. You can choose what to display and add custom classes.") );
                    parent::__construct(\'nothing-recent-posts\', __(\'Customized Recent Posts\'), $widget_ops);
                    $this->alt_option_name = \'nothing_recent_entries\';

                    add_action( \'save_post\', array($this, \'flush_widget_cache\') );
                    add_action( \'deleted_post\', array($this, \'flush_widget_cache\') );
                    add_action( \'switch_theme\', array($this, \'flush_widget_cache\') );
                }

                function widget($args, $instance) {
                    $cache = wp_cache_get(\'nothing_recent_posts\', \'widget\');

                    if ( !is_array($cache) )
                        $cache = array();

                    if ( ! isset( $args[\'widget_id\'] ) )
                        $args[\'widget_id\'] = $this->id;

                    if ( isset( $cache[ $args[\'widget_id\'] ] ) ) {
                        echo $cache[ $args[\'widget_id\'] ];
                        return;
                    }

                    ob_start();
                    extract($args);

                    $title = ( ! empty( $instance[\'title\'] ) ) ? $instance[\'title\'] : __( \'Recent Posts\' );
                    $title = apply_filters( \'widget_title\', $title, $instance, $this->id_base );
                    $number = ( ! empty( $instance[\'number\'] ) ) ? absint( $instance[\'number\'] ) : 10;
                    if ( ! $number )
                        $number = 10;
                    $show_title = isset( $instance[\'show_title\'] ) ? $instance[\'show_title\'] : false;
                    $show_date = isset( $instance[\'show_date\'] ) ? $instance[\'show_date\'] : false;
                    $show_img = isset( $instance[\'show_img\'] ) ? $instance[\'show_img\'] : false;
                    $custom_class = ( ! empty( $instance[\'custom_class\'] ) ) ? $instance[\'custom_class\'] : \' \';
                    $query_by_cat = isset( $instance[\'query_by_cat\'] ) ? $instance[\'query_by_cat\'] : false;

                    if( $query_by_cat ){
                        $current_category = wp_get_post_categories( get_the_id() );
                        $r = new WP_Query( apply_filters( \'widget_posts_args\', array(
                            \'category__in\' => $current_category,
                            \'posts_per_page\' => $number,
                            \'no_found_rows\' => true,
                            \'post_status\' => \'publish\',
                            \'ignore_sticky_posts\' => true,
                            \'post__not_in\' => array(get_the_id())
                        ) ) );
                    }else{
                        $r = new WP_Query( apply_filters( \'widget_posts_args\', array(
                            \'posts_per_page\' => $number,
                            \'no_found_rows\' => true,
                            \'post_status\' => \'publish\',
                            \'ignore_sticky_posts\' => true,
                            \'post__not_in\' => array(get_the_id())
                        ) ) );
                    }
                    if ($r->have_posts()) : ?>
                    <?php echo $before_widget; ?>
                    <?php if ( $title ) echo $before_title . $title . $after_title; ?>

                    <?php while ( $r->have_posts() ) : $r->the_post(); ?>

                    <article class="hentry <?php echo $custom_class; ?>">
                        <a href="<?php the_permalink(); ?>" rel="bookmark" title=\'Den Beitrag "<?php the_title(); ?>" ansehen\'>

                            <?php if ( $show_img ) : ?>
                            <div class="entry-thumbnail">
                                <?php the_post_thumbnail( \'thumbnail\' ); ?>
                            </div><!-- .entry-thumbnail -->
                            <?php endif; ?>

                            <?php if ( $show_title ) : ?>
                            <header class="widget-entry-header">
                            <div class="inner">
                                <h2><?php the_title(); ?></h2>
                            </div>
                            </header><!-- .entry-header -->
                            <?php endif; ?>

                            <?php if ( $show_date ) : ?>
                                <span class="post-date"><?php echo get_the_date(); ?></span>
                            <?php endif; ?>
                        </a>
                    </article>
                    <?php endwhile; ?>

                    <?php echo $after_widget; ?>
                    <?php wp_reset_postdata();

                    endif;

                    $cache[$args[\'widget_id\']] = ob_get_flush();
                    wp_cache_set(\'nothing_recent_posts\', $cache, \'widget\');
                }

                function update( $new_instance, $old_instance ) {
                    $instance = $old_instance;
                    $instance[\'title\'] = strip_tags($new_instance[\'title\']);
                    $instance[\'number\'] = (int) $new_instance[\'number\'];
                    $instance[\'show_date\'] = isset( $new_instance[\'show_date\'] ) ? (bool) $new_instance[\'show_date\'] : false;
                    $instance[\'show_title\'] = isset( $new_instance[\'show_title\'] ) ? (bool) $new_instance[\'show_title\'] : false;
                    $instance[\'show_img\'] = isset( $new_instance[\'show_img\'] ) ? (bool) $new_instance[\'show_img\'] : false;
                    $instance[\'custom_class\'] = strip_tags($new_instance[\'custom_class\']);
                    $instance[\'query_by_cat\'] = isset( $new_instance[\'query_by_cat\'] ) ? (bool) $new_instance[\'query_by_cat\'] : false;
                    $this->flush_widget_cache();

                    $alloptions = wp_cache_get( \'alloptions\', \'options\' );
                    if ( isset($alloptions[\'nothing_recent_entries\']) )
                        delete_option(\'nothing_recent_entries\');

                    return $instance;
                }

                function flush_widget_cache() {
                    wp_cache_delete(\'nothing_recent_posts\', \'widget\');
                }

                function form( $instance ) {
                    $title     = isset( $instance[\'title\'] ) ? esc_attr( $instance[\'title\'] ) : \'\';
                    $number    = isset( $instance[\'number\'] ) ? absint( $instance[\'number\'] ) : 5;
                    $show_title = isset( $instance[\'show_title\'] ) ? (bool) $instance[\'show_title\'] : true;
                    $show_date = isset( $instance[\'show_date\'] ) ? (bool) $instance[\'show_date\'] : false;
                    $show_img = isset( $instance[\'show_img\'] ) ? (bool) $instance[\'show_img\'] : false;
                    $custom_class     = isset( $instance[\'custom_class\'] ) ? esc_attr( $instance[\'custom_class\'] ) : \'\';
                    $query_by_cat = isset( $instance[\'query_by_cat\'] ) ? (bool) $instance[\'query_by_cat\'] : false;
            ?>
                    <p><label for="<?php echo $this->get_field_id( \'title\' ); ?>"><?php _e( \'Title:\' ); ?></label>
                    <input class="widefat" id="<?php echo $this->get_field_id( \'title\' ); ?>" name="<?php echo $this->get_field_name( \'title\' ); ?>" type="text" value="<?php echo $title; ?>" /></p>

                    <p><label for="<?php echo $this->get_field_id( \'number\' ); ?>"><?php _e( \'Number of posts to show:\' ); ?></label>
                    <input id="<?php echo $this->get_field_id( \'number\' ); ?>" name="<?php echo $this->get_field_name( \'number\' ); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>

                    <p><input class="checkbox" type="checkbox" <?php checked( $show_title ); ?> id="<?php echo $this->get_field_id( \'show_title\' ); ?>" name="<?php echo $this->get_field_name( \'show_title\' ); ?>" />
                    <label for="<?php echo $this->get_field_id( \'show_title\' ); ?>"><?php _e( \'Display post title?\' ); ?></label></p>

                    <p><input class="checkbox" type="checkbox" <?php checked( $show_date ); ?> id="<?php echo $this->get_field_id( \'show_date\' ); ?>" name="<?php echo $this->get_field_name( \'show_date\' ); ?>" />
                    <label for="<?php echo $this->get_field_id( \'show_date\' ); ?>"><?php _e( \'Display post date?\' ); ?></label></p>

                    <p><input class="checkbox" type="checkbox" <?php checked( $show_img ); ?> id="<?php echo $this->get_field_id( \'show_img\' ); ?>" name="<?php echo $this->get_field_name( \'show_img\' ); ?>" />
                    <label for="<?php echo $this->get_field_id( \'show_img\' ); ?>"><?php _e( \'Display post image?\' ); ?></label></p>

                    <p><label for="<?php echo $this->get_field_id( \'custom_class\' ); ?>"><?php _e( \'Custom class:\' ); ?></label>
                    <input class="widefat" id="<?php echo $this->get_field_id( \'custom_class\' ); ?>" name="<?php echo $this->get_field_name( \'custom_class\' ); ?>" type="text" value="<?php echo $custom_class; ?>" /></p>

                    <p><input class="checkbox" type="checkbox" <?php checked( $query_by_cat ); ?> id="<?php echo $this->get_field_id( \'query_by_cat\' ); ?>" name="<?php echo $this->get_field_name( \'query_by_cat\' ); ?>" />
                    <label for="<?php echo $this->get_field_id( \'query_by_cat\' ); ?>"><?php _e( \'Filter by current category?\' ); ?></label></p>

            <?php
                }
            }

2 个回复
最合适的回答,由SO网友:Mark Kaplun 整理而成

如果您遵循了最近的页面小部件的代码,那么您想要实现的目标就不能像您想要实现的那样天真。最近发布的小部件缓存基于以下假设:相同的输出显示在任何地方,这就是为什么它可以在任何页面上计算缓存并在任何其他页面上使用缓存,但此假设不适用于您的情况,因为您希望在某些页面上有不同的输出。

最简单的方法是为每个帖子(如最近的帖子)使用不同的缓存键{post id}。

SO网友:Frederick Townes

您是否确保缓存密钥中没有可能的冲突;i、 e.确保在密钥命名中使用特定于post的哈希?

相关推荐

更新页面(update-core.php)和插件页面(plugins.php)恢复到主页

我在Wordpress网站的管理视图中收到通知,我有一个网站和插件的可用更新(在我的网络管理仪表板中,在“插件”和“更新”旁边的红色圆圈中有“1”)。。。但当我尝试同时转到“更新”页和;插件页面,是否显示主页?此时的URL为http:///wp-admin/network/update-core.php/和http:///wp-admin/plugins.php/分别地因此,我永远无法到达真正的更新页面,也无法更新我的Wordpress或插件。如何显示更新或插件页面?