为什么价值'抵消'不保存在这个PHP小部件?

时间:2016-03-28 23:14:21

标签: php wordpress widget

为什么我的偏移'代码在wordpress中没有工作/保存在这段代码中?它最初是dazzling_tabbed_widget(但不再是)。如果我硬编码' offset' => 3它工作正常。

我似乎无法理解它。到目前为止,我已经编辑了这些内容:

<?php

/**
* Whats Trending Widget
* Dazzling Theme
*/

class dazzling_whats_trending extends WP_Widget {

/**
* Widget setup.
*/
function dazzling_whats_trending() {
/* Widget settings. */
$widget_ops = array( 'classname' => 'dazzling_tabbed_widget', 'description' => __( 'Dazzling Whats Trending Widget', 'dazzling' ) );

/* Widget control settings. */
$control_ops = array( 'width' => 250, 'height' => 350, 'id_base' => 'dazzling_tabbed_widget' );

/* Create the widget. */
parent::__construct( 'dazzling_tabbed_widget', __( 'Dazzling Whats Trending Widget', 'dazzling' ), $widget_ops, $control_ops );

}

/**
* How to display the widget on the screen.
*/
 function widget( $args, $instance ) {
extract( $args );

/* Our variables from the widget settings. */
    if ( isset( $instance[ 'number' ] ) )
        $number = $instance[ 'number' ];
    if ( isset( $instance[ 'offset' ] ) )
        $offset= $instance[ 'offset' ];
?>


       <div id="popular-posts" class="sidebar_content tab-pane active">


                <?php
                    $recent_posts = new WP_Query( array(
                        'showposts'           => $number,
                        'ignore_sticky_posts' => 1,
                        'post_status'         => 'publish',
                        'order'               => 'DESC',
                        'meta_key'            => 'post_views_count',
                        'orderby'             => 'meta_value_num',
                        'offset'              => $offset
                    ) );
                ?>

                <?php while($recent_posts->have_posts()): $recent_posts->the_post(); ?>
                    <li>
                        <?php the_post_thumbnail( array(336,210) ); ?>
              <h4 class="blacktext"><a href="<?php the_permalink(); ?>"><?php echo the_title(); ?></a></h4>
              <span class="orangetext">
            <?php 
              $post = get_post();
              $views = get_post_meta($post->ID, 'post_views_count', true);
              if($views > 1000){
                $views_count=$views *1/1000; 
                $views_k=round($views_count,PHP_ROUND_HALF_UP);
                echo $views_k.'K';
              }
              else {
                echo $views;
              }
            ?> VIEWS
          </span>
          <span class="graytext">| 
            <?php
              $category = get_the_category(); 
              $category_parent_id = $category[0]->category_parent;
              $category_link = get_category_link($category_parent_id);
              if ( $category_parent_id != 0 ) {
                $category_link = get_category_link($category_parent_id);
                    $category_parent = get_term( $category_parent_id, 'category' );
                    $catname = $category_parent->name;
              } else {
                $category_link = get_category_link($category_id);
                    $catname = $category[0]->name;
              }
            ?>
            <a href="<?php echo esc_url($category_link); ?>"><?php  echo $catname; ?></a>
          </span>
                    </li>
                <?php endwhile; ?>

            <?php wp_reset_postdata(); ?>

         </div>  


<?php
 }
 /**
 * Update the widget settings.
 */
function update( $new_instance, $old_instance ) {
$instance = array();
$instance = $old_instance;

/* Strip tags for title and name to remove HTML (important for text inputs). */
$instance['number'] = strip_tags( $new_instance['number'] );
$instance['offset'] = strip_tags( $new_instance['offset'] );
echo $instance;

return $instance;
}

function form( $instance=array() ) {

    /* Set up some default widget settings. */
    $defaults = array('offset' => 2, 'number' => 3);
    //$defaults = array('number' => 3);
    $instance = wp_parse_args( $instance, $defaults ); ?>

    <!-- Offset posts from -->
    <p>
      <label for="<?php echo $this->get_field_id( 'offset' ); ?>"><?php _e('Offset number of posts by','dazzling') ?>:</label>
      <input id="<?php echo $this->get_field_id( 'offset' ); ?>" name="<?php echo $this->get_field_name( 'offset' ); ?>" value="<?php echo $instance['offset']; ?>" size="3" />
    </p>

    <!-- Number of posts -->
    <p>
      <label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e('Number of posts to show','dazzling') ?>:</label>
      <input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" value="<?php echo $instance['number']; ?>" size="3" />
    </p>


  <?php
  }

}
 ?>

任何帮助都会很棒。

谢谢,

1 个答案:

答案 0 :(得分:0)

我使用https://pippinsplugins.com/simple-wordpress-widget-template/重新制作代码:

java.io.BufferedInputStream

无法设置默认数字,但是很高兴它可以工作:)