Support » Fixing WordPress » Custom Fields in Recent Posts widget
Custom Fields in Recent Posts widget
-
Hi,
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:
<li><?php $link = get_post_meta($post->id, 'alt_title', true); ?> <a href="<?php the_permalink() ?>"><?php echo $link; ?></a></li>
The problem is that this simply doesn’t return anything. The error must be in the returning of the custom field because if I enter a string in the space the list is returned correctly.
I have tried a few different configurations now but can’t get this to work. Where am I going wrong when it comes to retrieving my alternative post titles?
Thanks in advance for your help.
-
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>'; }
‘
Okay, thanks for the tip.
This is really my first time delving into the coding side of WordPress, so I’ll look into that.
- The topic ‘Custom Fields in Recent Posts widget’ is closed to new replies.