2017-04-22 18 views
0

我正在使用特定的小部件编写自己的代码,并在主页上使用此小部件将近18次,以显示不同的类别帖子。它会导致主页面加载不良?!如果我使用普通编码而不是小部件,那是更好还是不同?!你可以查我的网站加载http://akhbartop.com/在我的系统和一些朋友加载的主页面不好。我想知道更多有关加载或不加载主页的小部件?你有什么建议,而不是使用小部件?! 这是我的代码在小部件主页中的更多小部件会导致wordpress加载失败?

<?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代码窗口小部件,但仍然有问题部件的更新!!!当帖子发布小工具将不会更新!

+1

自定义代码总是比较简单的比较stuf。 –

+0

所以你建议编写自定义代码而不是widget.in这种情况下,我可以从小部件的主页page.it更好的? –

+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一个独特的东西,如$link1变量,代表类别ID:

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'); 

希望能对大家有所帮助。

+0

有删除缓存中的问题。当新的职位发布部件将不会更新! –

1

从代码的角度来看,代码所在的位置并不重要。您的代码以相同的方式执行,无论它是一个窗口小部件还是一个模板部件,或者是一个挂钩某个动作的插件函数。

重要的是代码实际上在做什么以及代码如何(并且代码的位置并不重要)。

在你的情况下,我想,小部件正在获取一些RSS提要,从而向外部资源发出http请求。这些请求很慢,必须缓存结果以避免在每次加载页面时重复请求。您可以为小部件的每个实例设置不同的到期时间(即使是30秒差异),以尝试同时阻止它们全部更新。或者甚至更好,你应该在后台更新它们,从不让前端访问者获取这些提要。

Transients API是你应该使用它来快速。

+0

谢谢你的answer.no小部件不提取只是显示来自特定category.fetching post由wp_automatic plugin.posts保存在我们的数据库和网站从我的数据库显示不是外部database.but缓存主页我需要帮助来解决这个问题:) –

+0

我使用wp-rocket插件缓存website.do你认为在24小时或无限的时间缓存(从来没有缓存,直到需要这个)?!!我如何使用Transients API on主页?! –

+0

我编辑的主要问题,你可以看到我的代码,我用在每个特定类别的帖子的小部件 –

相关问题