最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - How to show Recent Page instead of Post?

programmeradmin3浏览0评论

I'm designing a WordPress website and would like to show up the pages that were created finally(much like the recent posts)

So, how can I modify the "Recent Posts" feature to show up Pages and not Posts?

I'm designing a WordPress website and would like to show up the pages that were created finally(much like the recent posts)

So, how can I modify the "Recent Posts" feature to show up Pages and not Posts?

Share Improve this question asked Jan 9, 2016 at 13:38 Snazzy SanojSnazzy Sanoj 1971 silver badge9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

I have a ready made custom widget for recent page it was made for recent posts .. So please change the post text to page in the below code.

View thoroughly , only change the text that is for display purpose.

  1. Add this code to your functions.php file
  2. Goto appearance->widgets you will see you newly created widget Custom Recent Pages
  3. Drag it to the active sidebar and make the changes as per requirement.
  4. Done !!! you can see that in the front-end sidebar.

    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;
    
      }
    }?>
    
发布评论

评论列表(0)

  1. 暂无评论