Forever Theme: Add 4 recent posts/images to non posts page
-
Hello! New to building WordPress sites…
I like the look of the 4 recent posts on the blog page in the demo for the Forever theme: https://foreverdemo.wordpress.com/. How can I add the 4 recent posts and design to any page on my site? If possible, I’d like to only include the image thumbnail (same size as demo), title, and date.
Thanks in advance for your help!
-
Edit: Oh, I completely misread your request. My apologies.
What kind of page would you like to be able to add the four recent posts to: an archive page, a static page (such as About Me, Contact, etc.), or some other type of page?
A static page that will likely be my home page. Thanks!
Hi annakronzer,
Are your blog post in a certain category?
You can paste the following code onto your template of where you want them to appear:
<?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <div class="postsingle" id="post-<?php the_ID(); ?>"> <div class="entry"> <!-- ////// THIS IS TO CALL THE POST FROM THAT CATEGORY, ENTER THE CATEGORY ID NUMBER BELOW & THE NUMBER OF POSTS YOU WANT TO SHOW /////// --> <?php $recent = new WP_Query("cat=4&showposts=4"); while($recent->have_posts()) : $recent->the_post();?> <!-- ////// THEN HERE ADD THE CODE SNIPPETS FOR WHAT YOU WANT TO APPEAR DEPENDING ON WHERE YOUR IMAGE ARE ETC /////// --> <!-- the date--> <?php the_date(); ?> <!-- the title --> <h3><?php echo get_the_title(); ?></h3> <?php endwhile; ?> </div> </div> <?php endwhile; ?> <?php else : ?> <h2 class="center">Not Found</h2> <p class="center">Sorry, but you are looking for something that isn't here.</p> <?php endif; ?>
You would need to add a snippet of code to add the image in there too. I could give you this if you can tell me where the image appears in the post admin.
hope that helps
The category would be “Featured” and the image would be the featured image in the post. Does that change the code that you provided above? Do you need more information? Thanks!
what category id is “featured”? go to categories in posts and hover over the featured category and the link that appears in the bottom of the browser would show eg. “category&tagID=22” so if the category id was “22” and show the featured image then try:
<?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <div class="postsingle" id="post-<?php the_ID(); ?>"> <div class="entry"> <?php $recent = new WP_Query("cat=22&showposts=4"); while($recent->have_posts()) : $recent->the_post();?> <!-- thumbnail image --> <?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it. the_post_thumbnail(); } ?> <!-- the date--> <?php the_date(); ?> <!-- the title --> <h3><?php echo get_the_title(); ?></h3> <?php endwhile; ?> </div> </div> <?php endwhile; ?> <?php else : ?> <h2 class="center">Not Found</h2> <p class="center">Sorry, but you are looking for something that isn't here.</p> <?php endif; ?>
let me know if that works for you. You can wrap divs around the title, date and image and then style them using CSS.
Thanks for the step-by-step explanation! The category id for featured is “4”. I entered the code you provided in the page editor for where I want the posts to appear. This is what is displayed:
have_posts()) : $recent->the_post();?>
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
}
?>Not Found
Sorry, but you are looking for something that isn’t here.
—
Am I doing something wrong?
hmmm, perhaps try:
<?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <div class="postsingle" id="post-<?php the_ID(); ?>"> <div class="entry"> <?php $recent = new WP_Query("cat=4&showposts=4"); while($recent->have_posts()) : $recent->the_post();?> <?php if ( has_post_thumbnail() ) : ?> <?php the_post_thumbnail(); ?> <?php endif; ?> <?php the_date(); ?> <h3><?php echo get_the_title(); ?></h3> <?php endwhile; ?> </div> </div> <?php endwhile; ?> <?php else : ?> <h2 class="center">Not Found</h2> <p class="center">Sorry, but you are looking for something that isn't here.</p> <?php endif; ?>
Now it displays:
have_posts()) : $recent->the_post();?>
Not Found
Sorry, but you are looking for something that isn’t here.
If you could try the code below and so I can identify if it is the featured image causing the issue.
<?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <div class="postsingle" id="post-<?php the_ID(); ?>"> <div class="entry"> <?php $recent = new WP_Query("cat=4&showposts=4"); while($recent->have_posts()) : $recent->the_post();?> <?php the_date(); ?> <h3><?php echo get_the_title(); ?></h3> <?php endwhile; ?> </div> </div> <?php endwhile; ?> <?php else : ?> <h2 class="center">Not Found</h2> <p class="center">Sorry, but you are looking for something that isn't here.</p> <?php endif; ?>
Sure. Here is what I see now:
have_posts()) : $recent->the_post();?>
Not Found
Sorry, but you are looking for something that isn’t here.
Have you definitely created some posts assigned to the category ‘featured’ which ID is definitely 4?
With all due respect to MrYawn, there’s a much better and easier way to accomplish what you’re trying to achieve. You should make a child theme before following these steps, so that these changes will not be lost if the theme is updated later, to fix bugs or security issues or to add new features.
1) Using FTP or whatever file management application your host provides, create a file (in your child theme’s folder) named
recent-page.php
.2) Paste this code into that file: https://pastebin.com/D9hnuCV8
3) Find the static page that you would like to use as your static home page (Dashboard > Pages > All Pages) and click “Quick Edit”. In the dropdown box labeled “Template”, select “Recent Posts”.
4) Select that page as your static home page as normal (Dashboard > Appearance > Customize > Static Front Page).
hi stephancottontail,
I’m up for any suggestions and am surprised I was able to complete steps 1 and 2! ??
After completing all steps, I saved changes and the content in the page shifted from full-width to the default but the 4 recent posts still don’t display. Guessing this is because I changed the template from “Full width, no sidebar” to “recent posts”.
Just to confirm, I like the 4 posts in the grid style like the demo (horizontally). Is this what your code will help me to display? Thanks!
hi mrYawn,
I am certain I have at least 4 posts (with a featured image) with the category id that is 4. Thanks!
- The topic ‘Forever Theme: Add 4 recent posts/images to non posts page’ is closed to new replies.