我正在使用一个特定的小部件来编码自己,并在主页面上使用此小部件几乎18次,以显示不同的类别帖子。它会导致主页加载不好吗?!如果我使用普通编码代替小部件是更好还是没有差异?!您可以检查我的网站加载http://akhbartop.com/在我的系统中,一些朋友加载主页面并不好。我想知道主页中有关加载的更多小部件吗?!您建议使用小部件做什么?! 这是我在widget中的代码
<?php
// Creating the widget
class wpb_box extends WP_Widget {
function __construct() {
parent::__construct(
// Base ID of your widget
'wpb_box',
// Widget name will appear in UI
__('ابزارک اختصاصی باکس مطالب خبرخوان', 'bigtheme'),
// Widget description
array( 'description' => __( 'ابزارک نمایش باکس مطالب از سایت های مختلف توسط آدرس خوراک یا فید سایت', 'bigtheme' ), )
);
add_action('save_post', array( $this, 'delete_query_caches') );
}
// Creating widget front-end
// This is where the action happens
/**
* Delete transients
*/
function delete_query_caches( $post_id ){
if( !isset( $_POST['post_type'] ) || $_POST['post_type'] !== 'post' ) return;
$categories = wp_get_post_terms( $post_id, 'category' );
if( $categories && ! is_WP_Error( $categories ) ) {
foreach( $categories as $cat ) {
delete_transient('post'.$cat->term_id.'_query');
}
}
}
public function widget( $args, $instance ) {
$name = apply_filters( 'widget_title', $instance['name'] );
$category = apply_filters( 'widget_title', $instance['category'] );
$id = apply_filters( 'widget_title', $instance['id'] );
$link = apply_filters( 'widget_title', $instance['link'] );
$display = apply_filters( 'widget_title', $instance['display'] );
$color = apply_filters( 'widget_title', $instance['color'] );
// This is where you run the code and display the output
?>
<div class="col-sm-6 col-xs-12 padding5">
<div class="article">
<div class="title">
<h3><a href="<?php echo $link ?>" target="_blank"><?php echo $name ?></a></h3>
<div class="clear"></div>
<div class="line"></div>
</div>
<?php
if ( false === ( $slider = get_transient( 'post'.$id.'_query' ) ) ) {
$slider = new WP_Query(array(
'post_status' =>'publish',
'post_type' =>'post',
'cat' =>''.$id.'',
'posts_per_page' =>'9'
));
// Make a new query cache for 1 week
set_transient( 'post'.$id.'_query', $slider, 168 * HOUR_IN_SECONDS );
}
if( !$slider->have_posts() ) return; ?>
<ul>
<?php while( $slider->have_posts() ) : $slider->the_post(); ?>
<li>
<a href="<?php the_permalink() ?>" target="_blank"><?php the_title(); ?></a>
<div style="display:<?php echo $display;?>" class="tooltiptext hidden-xs"><?php the_excerpt(); ?></div>
</li>
<?php endwhile; $slider->rewind_posts(); ?>
</ul>
<?php wp_reset_postdata(); ?>
<div class="list"><a href="<?php echo $link ?>" target="_blank">مشاهده آرشیو کامل</a><a href="<?php echo $link ?>/feed" target="_blank"><img src="<?php bloginfo('template_url'); ?>/images/rss.png" width="18" height="18" alt="خوراک سایت" ></a></div>
</div>
</div>
<?php
echo $args['after_widget'];
}
public function form( $instance ) {
$name = ( isset( $instance[ 'name' ] ) ) ? $instance[ 'name' ] : '';
$category = ( isset( $instance[ 'category' ] ) ) ? $instance[ 'category' ] : '';
$link = ( isset( $instance[ 'link' ] ) ) ? $instance[ 'link' ] : '';
$color = ( isset( $instance[ 'color' ] ) ) ? $instance[ 'color' ] : '';
$id = ( isset( $instance[ 'id' ] ) ) ? $instance[ 'id' ] : '';
$display = ( isset( $instance[ 'display' ] ) ) ? $instance[ 'display' ] : '';
?>
<p>
<label for="<?php echo $this->get_field_id( 'color' ); ?>"><?php _e( 'رنگ باکس مطالب:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'color' ); ?>" name="<?php echo $this->get_field_name( 'color' ); ?>" type="text" value="<?php echo esc_attr( $color ); ?>" placeholder="مثال : #CCC , #dd3333 , black , blue" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'name' ); ?>"><?php _e( 'عنوان باکس:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'name' ); ?>" name="<?php echo $this->get_field_name( 'name' ); ?>" type="text" value="<?php echo esc_attr( $name ); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'id' ); ?>"><?php _e( 'آی دی دسته بندی' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'id' ); ?>" name="<?php echo $this->get_field_name( 'id' ); ?>" type="text" value="<?php echo esc_attr( $id ); ?>" />
</p>
<select id="<?php echo $this->get_field_id('category'); ?>" name="<?php echo $this->get_field_name('category'); ?>" class="widefat" style="width:100%;">
<?php foreach(get_terms('category','parent=0&hide_empty=0') as $term) { ?>
<option <?php selected( $instance['category'], $term->term_id ); ?> value="<?php echo $term->term_id; ?>"><?php echo $term->name; ?></option>
<?php } ?>
</select>
<p>
<label for="<?php echo $this->get_field_id( 'link' ); ?>"><?php _e( 'لینک آرشیو' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'link' ); ?>" name="<?php echo $this->get_field_name( 'link' ); ?>" type="text" value="<?php echo esc_attr( $link ); ?>" />
</p>
<p>
<label><?php _e( 'نمایش توضیحات مطالب' ); ?></label>
<select class="widefat" id="<?php echo $this->get_field_id( 'display' ); ?>" name="<?php echo $this->get_field_name( 'display' ); ?>">
<option <?php selected( $instance['display'], 'block'); ?> value="block">بله</option>
<option <?php selected( $instance['display'], 'none'); ?> value="none">خیر</option>
</select>
</p>
<?php
}
// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['name'] = ( ! empty( $new_instance['name'] ) ) ? strip_tags( $new_instance['name'] ) : '';
$instance['category'] = ( ! empty( $new_instance['category'] ) ) ? strip_tags( $new_instance['category'] ) : '';
$instance['link'] = ( ! empty( $new_instance['link'] ) ) ? strip_tags( $new_instance['link'] ) : '';
$instance['link2'] = ( ! empty( $new_instance['link2'] ) ) ? strip_tags( $new_instance['link2'] ) : '';
$instance['id'] = ( ! empty( $new_instance['id'] ) ) ? strip_tags( $new_instance['id'] ) : '';
$instance['link3'] = ( ! empty( $new_instance['link3'] ) ) ? strip_tags( $new_instance['link3'] ) : '';
$instance['link4'] = ( ! empty( $new_instance['link4'] ) ) ? strip_tags( $new_instance['link4'] ) : '';
$instance['link5'] = ( ! empty( $new_instance['link5'] ) ) ? strip_tags( $new_instance['link5'] ) : '';
$instance['color'] = ( ! empty( $new_instance['color'] ) ) ? strip_tags( $new_instance['color'] ) : '';
$instance['display'] = ( ! empty( $new_instance['display'] ) ) ? strip_tags( $new_instance['display'] ) : '';
$instance['source'] = ( ! empty( $new_instance['source'] ) ) ? strip_tags( $new_instance['source'] ) : '';
$instance['time'] = ( ! empty( $new_instance['time'] ) ) ? strip_tags( $new_instance['time'] ) : '';
return $instance;
}
} // Class wpb_box ends here
// Register and load the widget
function wpb_box() {
register_widget( 'wpb_box' );
}
add_action( 'widgets_init', 'wpb_box' );
?>
我做了一些更改,并在窗口小部件中放置了瞬态api代码,但仍然有小部件更新的问题!!!当帖子发布小部件不会更新!!
答案 0 :(得分:1)
从代码的角度来看,代码所在的位置并不重要。您的代码以相同的方式执行,无论它是小部件还是模板部分,还是连接到某个动作的插件函数。
重要的是代码实际在做什么以及如何(再次,代码在哪里无关紧要)。
在您的情况下,我想,小部件正在获取一些RSS源,从而向外部资源发出http请求。这些请求很慢,必须缓存结果以避免在每个页面加载时重复请求。您可以为窗口小部件的每个实例设置不同的到期时间(甚至30秒差异),以防止它们同时更新。或者更好的是,您应该在后台更新它们,而不是让前端访问者获取这些Feed。
Transients API是您应该用来加快速度的。
答案 1 :(得分:1)
如果您使用的查询太多,缓存查询可能会有所帮助。 假设您的自定义小部件如下所示:
<?php
/**
* My widget class
*/
class dw_myWidget extends WP_Widget {
function __construct() {
// Instantiate the parent object
parent::__construct( false, __('My widget') );
}
function widget( $args, $instance ) {
$title = apply_filters( 'title', $instance['title'] );
$link1 = apply_filters( 'category', $instance['category'] ); // category id
$portfolio = new WP_Query(array(
'post_status' =>'publish',
'post_type' =>'post',
'cat' =>''.$link1.'',
'posts_per_page' =>'9'
));
// And the rest of the code
}
function update( $new_instance, $old_instance ) {
// update stuff
}
function form( $instance ) {
// widget form stuff
}
}
function dw_MyWidget_register() {
register_widget( 'dw_myWidget' );
}
add_action( 'widgets_init', 'dw_MyWidget_register' );
我们使用Wordpress的临时密钥缓存查询,但由于我们不知道有多少小部件,我们必须在瞬态API中包含一个独特的东西,例如代表类别ID的$link1
变量:
function widget( $args, $instance ) {
$title = apply_filters( 'title', $instance['title'] );
$link1 = apply_filters( 'category', $instance['category'] ); // category id
if ( false === ( $portfolio = get_transient( 'post'.$link1.'_query' ) ) ) {
$portfolio = new WP_Query(array(
'post_status' =>'publish',
'post_type' =>'post',
'cat' =>''.$link1.'',
'posts_per_page' =>'9'
));
set_transient( 'post'.$link1.'_query', $portfolio, 168 * HOUR_IN_SECONDS );
}
// And the rest of the code
}
这不是全部,我们需要在创建新帖子时删除瞬态,因此我们在构造函数中注册save_post
动作的挂钩
function __construct() {
// Instantiate the parent object
parent::__construct( false, __('My widget') );
add_action('save_post', array( $this, 'delete_query_caches') );
}
/**
* Delete transients
*/
function delete_query_caches( $post_id ){
if( !isset( $_POST['post_type'] ) || $_POST['post_type'] !== 'post' ) return;
$categories = wp_get_post_terms( $post_id, 'category' );
if( $categories && ! is_WP_Error( $categories ) ) {
foreach( $categories as $cat ) {
delete_transient('post'.$cat->term_id.'_query');
}
}
}
所以最终的代码看起来像这样:
<?php
/**
* My widget class
*/
class dw_myWidget extends WP_Widget {
function __construct() {
// Instantiate the parent object
parent::__construct( false, __('My widget') );
add_action('save_post', array( $this, 'delete_query_caches') );
}
/**
* Delete transients
*/
function delete_query_caches( $post_id ){
if( !isset( $_POST['post_type'] ) || $_POST['post_type'] !== 'post' ) return;
$categories = wp_get_post_terms( $post_id, 'category' );
if( $categories && ! is_WP_Error( $categories ) ) {
foreach( $categories as $cat ) {
delete_transient('post'.$cat->term_id.'_query');
}
}
}
function widget( $args, $instance ) {
$title = apply_filters( 'title', $instance['title'] );
$link1 = apply_filters( 'category', $instance['category'] ); // category id
if ( false === ( $portfolio = get_transient( 'post'.$link1.'_query' ) ) ) {
$portfolio = new WP_Query(array(
'post_status' =>'publish',
'post_type' =>'post',
'cat' =>''.$link1.'',
'posts_per_page' =>'9'
));
set_transient( 'post'.$link1.'_query', $portfolio, 168 * HOUR_IN_SECONDS );
}
// And the rest of the code
}
function update( $new_instance, $old_instance ) {
// update stuff
}
function form( $instance ) {
// widget form stuff
}
}
function dw_MyWidget_register() {
register_widget( 'dw_myWidget' );
}
add_action( 'widgets_init', 'dw_MyWidget_register' );
希望它会有所帮助。