Display custom fields too in recent posts widget
-
Hi everyone,
I am an absolute beginner with WordPress and coding in general so please forgive me if I am asking something obvious for you.
I have a number of posts in my site which have two custom fields. In need to display a widget in the homepage with just the title and the two custom fields of the last five posts.
Now I am using a widget called WP recent posts which I found in my theme but it displays just a list of titles and I don’t know how to add the two custom fields.
I searched the web for a way to do this without coding but I couldn’t find it (I didn’t find a way to do this with coding too….).
I hope someone can help me!
Thank you
-
You could write your own code for the top of your sidebar.
This is untested, but it should be a good start:
<aside id="custom-recent-posts" class="widget"> <h1 class="widget-title">Recent Posts</h1> <ul> <?php $postslist = get_posts('numberposts=5'); foreach ($postslist as $post) { setup_postdata($post); ?> <li> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br/> <?php echo get_post_meta($post,'your_first_field;, false') . '<br/>'; echo get_post_meta($post,'your_first_field;, false'); ?> </li> <?php } ?> </ul> </aside>You may need to adjust it for styling – also, I assumed you’d want the post titles to output as links.
Thank you so much Chad!
I’m ok with HTML and CSS and a little bit of PHP, so I think that I will be able to style the widget, and yes, I want the title as a link!
Can I write this code directly in the Text Widget or the PHP code will be filtered?
Now I’m calling it a day, but tomorrow I will test your code!It won’t work in a text widget.
You’ll want to add it into your sidebar.php file, right before your widgets are called.
Just make sure you’re doing all of this in a child theme so it’s update proof! 🙂
Hi Chad,
I can’t find any sidebar.php file, I am using a premium theme called <a href=”
http://themeforest.net/item/glory-multipurpose-woocommerce-wordpress-theme/10853615″>Glory and still working on localhost, so I don’t know how to show you my site.Here you find the files and folders I have in my theme, which one of them you think is substituting the sidebar.php file?
Plus, I was thinking maybe I should use a plug in like PHP Text Widget or PHP Code Widget and avoid embarking on the study of all these files….
What do you think?
Try framework/functions/theme_sidebar.php but please note that usually youre asked to contact the place you bought the theme from (in this case themeforest) for support for paid themes.
Allowing php in widgets is not something I (or many others) would recommend, instead why dont you take this opportunity to learn some basic plugin programming to make your own widget/widebar plugin?
Hi rollingWolf,
I tried putting the code at the top of the framework/functions/theme_sidebar.php but now I have the
asideat the top of every page and titles are not displaying.While I am writing to you I am in contact with the support team and they already helped me a lot, but for some customizations they deservedly require a fee and my office manager doesn’t want to pay!
I am trying my best to understand how all this works, I finished an education course on PHP a month ago but I have to admit that real coding is not at all easy as it was at school!
I know that the PHP widget is not a good idea, but I nedd to think up a way of solving the problem.
Then I suggest you write your own recentposts plugin. Information and a basic example is available at:
http://codex.wordpress.org/Widgets_APIWhile it’s probably more involved than you were hoping, rollingWolf’s suggestion is probably best for that type of theme.
The only alternative is going to be using the developer tools to identify the container that holds your widgets (where you want your recent posts displayed) and then locate the theme file that generates that element – maybe the file you’ve already edited.
Once you know what element holds your widgets, you can try adding the code you have just inside that element, before the theme displays the other widgets you’ve added to the sidebar.
As always, if you go this route, a child theme is the safest option.
Hi guys,
while I was trying to make my widget I realized that I was wrong, what I need isn’t a widget, although they call it in this way.
Here and here you can see that my page is built with Visual Composer and those pieces of content are called widgets but they are different from the ones that I find in the appearence settings.
In fact I installed the plugin that allows inserting PHP code in the text widget but it doesn’t take effect on the text widget that I find in the Visual Composer.
In the end I am turning to the support team and opening a ticket.
But this won’t prevent me from learning how to build a widget which displays the custom fields of the last five posts and as soon as I find out I’ll post it here because you never know.
The topic ‘Display custom fields too in recent posts widget’ is closed to new replies.