我正在为练习创建一个 Wordpress 最近的帖子小部件。但是,我遇到了一些我不知道该怎么做的事情。首先,小部件必须显示帖子的日期和时间、帖子标题、带有智能截断字符限制的文本和阅读更多链接。它还必须允许管理员指定标题、类别、要显示的帖子数、要在帖子上显示的字符数,最后更改“阅读更多”链接的措辞。
我已经创建了小部件并弄清楚了如何做所有事情,但将类别选择设置为下拉选择,其中已经存在类别(现在它只是管理员的一个文本块,您可以在其中键入要显示的类别) ,如何在the_excerpt
制作时限制字符,以便在单词末尾巧妙地切断并允许管理员指定如何允许我的字符,以及如何创建阅读更多链接,同时允许管理员指定它所说的内容。
我将在下面发布到目前为止的代码以及指向包含侧栏中小部件的站点的链接。我是 Wordpress Widget 设计的新手,因此将不胜感激。
<?php
/*
Plugin Name: News Recent Posts Widget
Plugin URI:
Description: A recent post widget with extra functions for client management
Author: Kevin Ullyott
Version: 1.0
Author URI: http://modmacro.com/
*/
class recentpost extends WP_Widget {
public function __construct() {
parent::WP_Widget(
// or parent::__construct(
false,
'Kevin - Recent Posts Widget',
array(
'description' => __('A recent post widget with extra functions for client management')
)
);
;
}
public function widget( $args, $instance ) {
extract( $args );
$headline = $instance['headline'];
$category = $instance['category'];
$numberposts = $instance['numberposts'];
$readmore = $instance['readmore'];
echo $before_widget;
echo $before_title;
echo "<p class=\"headline\">$headline</p>";
echo $after_title;
$args = array( 'numberposts' => $numberposts, 'category_name' => $category );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
setup_postdata(get_post($recent['ID']));
echo '<a href="' . get_permalink() . '" title=" '.esc_attr(get_the_title()).'" >' . get_the_title().'</a> ';
echo get_the_time('F j, Y', $recent['ID']);
the_excerpt();
}
wp_reset_postdata();
echo $after_widget;
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['headline'] = ( $new_instance['headline'] );
$instance['category'] = ( $new_instance['category'] );
$instance['numberposts'] = ( $new_instance['numberposts'] );
$instance['readmore'] = ( $new_instance['readmore'] );
return $instance;
}
public function form( $instance ) {
$headline = $instance[ 'headline' ];
$category = $instance[ 'category' ];
$numberposts = $instance[ 'numberposts' ];
$readmore = $instance[ 'readmore' ];
?>
<p>
<label for="<?php echo $this->get_field_id( 'headline' ); ?>">
<?php _e( 'Headline:' ); ?>
</label>
<input class="widefat" id="<?php echo $this->get_field_id( 'headline' ); ?>" name="<?php echo $this->get_field_name( 'headline' ); ?>" type="text" value="<?php echo esc_attr( $headline ); ?>" />
</p>
<label for="<?php echo $this->get_field_id( 'category' ); ?>">
<?php _e( 'Category:' ); ?>
</label>
<input class="widefat" id="<?php echo $this->get_field_id( 'category' ); ?>" name="<?php echo $this->get_field_name( 'category' ); ?>" type="text" value="<?php echo esc_attr( $category ); ?>" />
</p>
<label for="<?php echo $this->get_field_id( 'numberposts' ); ?>">
<?php _e( 'Number of posts:' ); ?>
</label>
<input class="widefat" id="<?php echo $this->get_field_id( 'numberposts' ); ?>" name="<?php echo $this->get_field_name( 'numberposts' ); ?>" type="text" value="<?php echo esc_attr( $numberposts ); ?>" />
</p>
<label for="<?php echo $this->get_field_id( 'readmore' ); ?>">
<?php _e( 'Read More:' ); ?>
</label>
<input class="widefat" id="<?php echo $this->get_field_id( 'readmore' ); ?>" name="<?php echo $this->get_field_name( 'readmore' ); ?>" type="text" value="<?php echo esc_attr( $readmore ); ?>" />
</p>
<?php
}
}
add_action( 'widgets_init', create_function('', 'return register_widget("recentpost");') );
?>