需要在最近发布的WP小部件中添加摘录

时间:2017-08-01 10:48:31

标签: wordpress

我在我的wp网站上使用了高级主题,但我正在尝试在主页侧边栏上添加最近的帖子小部件,这个小部件只显示帖子标题和日期但没有摘录。我是初学者,不知道如何自定义这个小部件,我只是认为这些是最近发布的小部件的代码。

<?php



/*-----------------------------------------------------------------------------------



    Plugin Name: Custom Blog Widget

    Plugin URI: http://www.premiumpixels.com

    Description: A widget that allows the display of blog posts.

    Version: 1.0

    Author: Orman Clark

    Author URI: http://www.premiumpixels.com



-----------------------------------------------------------------------------------*/





// Add function to widgets_init that'll load our widget.

add_action( 'widgets_init', 'tz_blog_widgets' );





// Register widget.

function tz_blog_widgets() {

    register_widget( 'TZ_Blog_Widget' );

}



// Widget class.

class tz_blog_widget extends WP_Widget {





/*-----------------------------------------------------------------------------------*/

/*  Widget Setup

/*-----------------------------------------------------------------------------------*/



    function __construct() {



        /* Widget settings. */

        $widget_ops = array( 'classname' => 'tz_blog_widget', 'description' => __('A widget that displays your latest posts with a short excerpt.', 'Creativo') );



        /* Widget control settings. */

        $control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'tz_blog_widget' );



        /* Create the widget. */

        parent::__construct( 'tz_blog_widget', __('Custom Recent Posts Widget', 'Creativo'), $widget_ops, $control_ops );

        //$this->WP_Widget( 'tz_blog_widget', __('Custom Recent Posts Widget', 'Creativo'), $widget_ops, $control_ops );

    }





/*-----------------------------------------------------------------------------------*/

/*  Display Widget

/*-----------------------------------------------------------------------------------*/



    function widget( $args, $instance ) {

        extract( $args );



        $title = apply_filters('widget_title', $instance['title'] );



        /* Our variables from the widget settings. */

        $number = $instance['number'];



        /* Before widget (defined by themes). */

        echo $before_widget;



        /* Display Widget */

        ?> 

        <?php /* Display the widget title if one was input (before and after defined by themes). */

                if ( $title )

                    echo $before_title . $title . $after_title;

                ?>



                    <?php 

                    $query = new WP_Query();

                    $query->query( array(

                        'posts_per_page' => $number,

                        'ignore_sticky_posts' => 1

                    ));

                    ?>

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

                            <div class="latest-posts">

                            <?php 

                                    if ( (function_exists('has_post_thumbnail')) && (has_post_thumbnail()) ) { ?>

                                        <div class="latest-posts-thumb"><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('related-img'); ?></a></div>

                                        <a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>

                                        <span><?php the_time( get_option('date_format') ); ?></span>

                                         <div class="clr"></div>                         

                                     <?php 

                                    } 

                                    else{?>

                                        <div class="latest-posts-thumb"><a href="<?php the_permalink(); ?>"><img src="<?php bloginfo( 'template_url' ); ?>/images/no-image.jpg" /></a></div>

                                        <a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>

                                        <span><?php the_time( get_option('date_format') ); ?></span>

                                        <div class="clr"></div> 

                                    <?php

                                    }

                            ?>

                            </div>  



                    <?php endwhile; endif; ?>



                    <?php wp_reset_query(); ?>





        <?php



        /* After widget (defined by themes). */

        echo $after_widget;

    }





/*-----------------------------------------------------------------------------------*/

/*  Update Widget

/*-----------------------------------------------------------------------------------*/



    function update( $new_instance, $old_instance ) {



        $instance = $old_instance;



        /* Strip tags to remove HTML (important for text inputs). */

        $instance['title'] = strip_tags( $new_instance['title'] );

        $instance['number'] = strip_tags( $new_instance['number'] );



        /* No need to strip tags for.. */



        return $instance;

    }





/*-----------------------------------------------------------------------------------*/

/*  Widget Settings

/*-----------------------------------------------------------------------------------*/



    function form( $instance ) {



        /* Set up some default widget settings. */

        $defaults = array(

        'title' => __('Take a look behind the scenes.','Creativo'),



        'number' => 4



        );

        $instance = wp_parse_args( (array) $instance, $defaults ); ?>



        <!-- Widget Title: Text Input -->

        <p>

            <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'Creativo') ?></label>

            <input type="text" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" />

        </p>



        <!-- Widget Title: Text Input -->

        <p>

            <label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e('Amount to show:', 'Creativo') ?></label>

            <input type="text" class="widefat" id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" value="<?php echo $instance['number']; ?>" />

        </p>





    <?php

    }

}

?>

1 个答案:

答案 0 :(得分:0)

//change this 
if ((function_exists('has_post_thumbnail')) && (has_post_thumbnail())) { ?>

    <div class="latest-posts-thumb"><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('related-img'); ?></a></div>

    <a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>

    <span><?php the_time(get_option('date_format')); ?></span>

    <div class="clr"></div>                         

    <?php
}

//to 

if ((function_exists('has_post_thumbnail')) && (has_post_thumbnail())) { ?>

    <div class="latest-posts-thumb"><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('related-img'); ?></a></div>

    <a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>

    <p class="post-excerpt"><?php the_excerpt(); ?></p>

    <span><?php the_time(get_option('date_format')); ?></span>

    <div class="clr"></div>                         

    <?php
}

the_excerpt()用于获取摘录后的内容