我有一个现成的自定义小部件recent page
它是为recent posts
.. 因此,请将帖子文本更改为以下代码中的第页。
彻底查看,仅更改用于显示的文本。
将此代码添加到函数中。php文件转到外观->小部件您将看到新创建的小部件Custom Recent Pages
将其拖动到活动侧栏,并根据需要进行更改完成!!!您可以在前端侧栏中看到这一点。
add_action( \'widgets_init\', \'register_widgets_func\' );
class cust_recentpage_widget extends WP_Widget {
function __construct() {
parent::__construct(
\'cust_recentpage_widget\', // Base ID
__( \'Custom Recent Pages\', \'text_domain\' ), // Name
array( \'description\' => __( \'A Widget to display Recent Pages In the Website\', \'text_domain\' ), ) // Args
);
}
public function widget( $args, $instance ) {
if($instance[\'only_title\'] == \'\'){
//echo $args[\'before_widget\'];
echo \'<aside class="col-md-4 block">\';
if ( ! empty( $instance[\'title\'] ) ) {
echo $args[\'before_title\'] . apply_filters( \'widget_title\', $instance[\'title\'] ). $args[\'after_title\'];
}
if($instance[\'no_of_post\'] == 0 || $instance[\'no_of_post\'] == \'\'){
$num_of_post = 3;
} else{
$num_of_post = $instance[\'no_of_post\'];
}
// The Query
$args = array(
\'post_type\' => \'page\',
\'post_status\' => \'publish\',
\'posts_per_page\'=> $num_of_post,
\'orderby\' => \'ID\',
\'order\' => \'DESC\'
);
//p($args);
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
$loop = \'<ul class="data">\';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$strip_c = strip_tags(get_the_content());
$loop .=\'<li class="press-box no-list-icon">\';
if(has_post_thumbnail()){
$padd_left = \'\';
$strlimit = 70;
$loop .= \'<div class="img">\'.get_the_post_thumbnail(get_the_id(),array(78,60)).\'</div>\';
}else{
$padd_left = \'style="padding-left: 0;"\';
$strlimit = 80;
}
$content = substr($strip_c,0,$strlimit);
if(strlen(get_the_title()) > 25){
$tittle = substr(get_the_title(),0,26).\'...\';
}else{
$tittle = get_the_title();
}
$loop .= \'<div class="details" \'.$padd_left.\'>
<h5 title="\'.get_the_title().\'">\'.$tittle.\'</h5>
<p>\'.$content.\'...</p>
</div>
</li>\';
}
$loop .=\'</ul>\';
} else {
$loop = \'No pages Yet\';
}
/* Restore original Post Data */
echo $loop;
echo \'</aside>\';
} else{
//echo $args[\'before_widget\'];
echo \'<div class="panel panel-default">\';
if ( ! empty( $instance[\'title\'] ) ) {
echo $args[\'before_title\'] .\'<a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo" class="collapsed">\'. apply_filters( \'widget_title\', $instance[\'title\'] ). \'</a>\' .$args[\'after_title\'];
}
$num_of_post = $instance[\'no_of_post\'];
if($num_of_post == \'\'){
$num_of_post = 3;
}
// The Query
$args = array(
\'post_type\' => \'page\',
\'post_status\' => \'publish\',
\'posts_per_page\'=> $num_of_post,
\'orderby\' => \'ID\',
\'order\' => \'DESC\'
);
$the_query = new WP_Query( $args );
?>
<div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
<div class="panel-body">
<div class="links">
<?php
while ( $the_query->have_posts() ) {
$the_query->the_post();
?>
<a href="<?php echo get_the_permalink();?>"><?php
if(strlen(get_the_title()) > 15) {
echo substr(get_the_title(),0,16).\'...\';
} else{
echo get_the_title();
}?></a>
<?php
} wp_reset_postdata(); ?>
</div>
</div>
</div>
<?php
echo \'</div>\';
//echo $args[\'after_widget\'];
}
}
public function form( $instance ) {
$title = ! empty( $instance[\'title\'] ) ? $instance[\'title\'] : __( \'Recent Posts\', \'text_domain\' );
?>
<p>
<label for="<?php echo $this->get_field_id( \'title\' ); ?>"><?php _e( \'Header Title:\' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( \'title\' ); ?>" name="<?php echo $this->get_field_name( \'title\' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
</p>
<?php
$no_of_post = ! empty( $instance[\'no_of_post\'] ) ? $instance[\'no_of_post\'] : __( \'3\', \'text_domain\' );
?>
<p>
<label for="<?php echo $this->get_field_id( \'no_of_post\' ); ?>"><?php _e( \'Number of Posts:\' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( \'no_of_post\' ); ?>" name="<?php echo $this->get_field_name( \'no_of_post\' ); ?>" type="number" value="<?php echo esc_attr( $no_of_post ); ?>">
</p>
<?php
$checked = ! empty( $instance[\'only_title\'] ) ? 1 : 0;
if($checked == 1)
$check_me = \'checked="checked"\';
else
$check_me = \'\';
?>
<p>
<label for="<?php echo $this->get_field_id( \'only_title\' ); ?>"><?php _e( \'Show in sidebar in Community News Page as Toggled Slider:\' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( \'only_title\' ); ?>" name="<?php echo $this->get_field_name( \'only_title\' ); ?>" type="checkbox" <?php echo esc_attr( $check_me );?> >
</p>
<?php
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance[\'title\'] = ( ! empty( $new_instance[\'title\'] ) ) ? strip_tags( $new_instance[\'title\'] ) : \'\';
$instance[\'no_of_post\'] = ( ! empty( $new_instance[\'no_of_post\'] ) ) ? strip_tags( $new_instance[\'no_of_post\'] ) : \'\';
$instance[\'only_title\'] = ( ! empty( $new_instance[\'only_title\'] ) ) ? strip_tags( $new_instance[\'only_title\'] ) : \'\';
return $instance;
}
}?>