Support » Fixing WordPress » Custom Fields in Recent Posts widget

Viewing 2 replies - 1 through 2 (of 2 total)
  • I’m customising the default Recent Posts widget so that it displays short post titles from my custom field ‘alt_title’ instead of the longer standard post titles.

    After editing deafult-widgets.php, here’s what I have for the list item:

    Don’t hack core!

    There is really never ever any reason to change core files. Create your own widget that shows recent posts how you want them to display. Read up on the Widgets API .

    Here is a sample function widget to get you started. This of course would be inside your class that extends WP_Widget

    function widget() {
          $posts = get_posts( array( 'posts_per_page' => 5) );
          echo $before_title;
          echo $title;
          echo '<ul>';
          foreach ( $posts as $post ) {
              echo '<li><a href="'. get_the_permalink(). '" title="'. esc_attr( get_the_title() ). '">'. esc_html( get_post_meta( $post->ID, 'alt_title', true ) ). '</a></li>';
          }
         echo '</ul>';
    
     }

    Thread Starter mattcasmith

    (@mattcasmith)

    Okay, thanks for the tip.

    This is really my first time delving into the coding side of WordPress, so I’ll look into that.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom Fields in Recent Posts widget’ is closed to new replies.