将小工具更改为显示摘录而不是内容

时间:2011-11-02 作者:Ayman

就我个人而言,我不知道如何让这个小部件(内置在主题中)显示摘录而不是帖子内容。你们能帮我看看吗。任何帮助都将不胜感激。

// =============================== Latest news posts Widget (particular category) ======================================

class latest_post1 extends WP_Widget {
    function latest_post1() {
    //Constructor
        $widget_ops = array(\'classname\' => \'widget Latest News\', \'description\' => \'List of latest posts in particular category\' );
        $this->WP_Widget(\'latest_post1\', \'PT → Latest News\', $widget_ops);
    }

    function widget($args, $instance) {
    // prints the widget

        extract($args, EXTR_SKIP);
        echo $before_widget;
        $title = empty($instance[\'title\']) ? \'\' : apply_filters(\'widget_title\', $instance[\'title\']);
        $category = empty($instance[\'category\']) ? \' \' : apply_filters(\'widget_category\', $instance[\'category\']);
        $post_number = empty($instance[\'post_number\']) ? \' \' : apply_filters(\'widget_post_number\', $instance[\'post_number\']);
        $post_link = empty($instance[\'post_link\']) ? \' \' : apply_filters(\'widget_post_link\', $instance[\'post_link\']);

        // if ( !empty( $title ) ) { echo $before_title . $title . $after_title; };
        echo \'\';
         ?>

          <?php if($title){?> 
                <h3><?php echo $title; ?></h3>          
                <?php }  ?>

          <div class="latestpost">
                <?php 
                    global $post;
                    $latest_menus = get_posts(\'numberposts=\'.$post_number.\'postlink=\'.$post_link.\'&category=\'.$category.\'\');
                    foreach($latest_menus as $post) :
                    setup_postdata($post);
                ?>
                 <?php $post_images = bdw_get_images($post->ID,\'large\');?>




            <div class="post_list">
                <p><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
                <cite>By <?php the_author_posts_link(); ?></cite>
                <div class="date"> <?php the_time(\'jS F\')  ?></div>
            </div>            






<?php endforeach; ?>
<?php

        echo \' </div>\';

        echo $after_widget;
    }

    function update($new_instance, $old_instance) {
    //save the widget
        $instance = $old_instance;
        $instance[\'title\'] = strip_tags($new_instance[\'title\']);
        $instance[\'category\'] = strip_tags($new_instance[\'category\']);
        $instance[\'post_number\'] = strip_tags($new_instance[\'post_number\']);
        $instance[\'post_link\'] = strip_tags($new_instance[\'post_link\']);
        return $instance;

    }

    function form($instance) {
    //widgetform in backend
        $instance = wp_parse_args( (array) $instance, array( \'title\' => \'\', \'category\' => \'\', \'post_number\' => \'\' ) );
        $title = strip_tags($instance[\'title\']);
        $category = strip_tags($instance[\'category\']);
        $post_number = strip_tags($instance[\'post_number\']);
        $post_link = strip_tags($instance[\'post_link\']);

?>
<p>
  <label for="<?php echo $this->get_field_id(\'title\'); ?>">Title:
    <input class="widefat" id="<?php echo $this->get_field_id(\'title\'); ?>" name="<?php echo $this->get_field_name(\'title\'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" />
  </label>
</p>
<p>
  <label for="<?php echo $this->get_field_id(\'category\'); ?>">Categories (<code>IDs</code> separated by commas):
  <input class="widefat" id="<?php echo $this->get_field_id(\'category\'); ?>" name="<?php echo $this->get_field_name(\'category\'); ?>" type="text" value="<?php echo attribute_escape($category); ?>" />
  </label>
</p>
<p>
  <label for="<?php echo $this->get_field_id(\'post_number\'); ?>">Number of posts:
  <input class="widefat" id="<?php echo $this->get_field_id(\'post_number\'); ?>" name="<?php echo $this->get_field_name(\'post_number\'); ?>" type="text" value="<?php echo attribute_escape($post_number); ?>" />
  </label>
</p>
<?php
    }

}

register_widget(\'latest_post1\');

1 个回复
SO网友:Andy James

我在代码中没有看到任何地方添加了全文或摘录。所以我决定测试这个小部件,这正是我得到的。没有输出帖子内容。

所以我的建议是

<?php the_excerpt(); ?>
无论您希望它输出到哪里。。。就在拇指后面。

<?php $post_images = bdw_get_images($post->ID,\'large\');?>
<?php the_excerpt(); ?>
我测试了一下,效果很好。

结束

相关推荐

Why use widgets?

我对使用WordPress很陌生,我想知道使用小部件的好处是什么?看here 这听起来像是为那些不是程序员的人准备的,他们想在他们的网站上添加插件。对吗?或者小部件是否在某种程度上使站点更加健壮?