Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Any ideas, how I should do this:

Recent post (with comments) shown in page? I need fixed url example.com/question that shows the most recent post, style should be same as in normal one post? Can I use somehow single.php and loop.php?

EDIT: Simple example(?): I want single, recent post same way as single post, but with url that provides always the most recent post.

share|improve this question
    
Can you add more details to your question, now it is not clear. Adding details with examples will also help. –  Maruti Mohanty Apr 24 at 7:31
    
What additional info is needed, I can provide if needed. –  guestar Apr 24 at 7:54

1 Answer 1

Here is a start. This is how I display related posts from the category the post are in. You will just need to add your own loop and CSS

Create a template called content-related.php

Inside that paste the following code

<?php
/**
 * The default template for displaying realted posts
 *
 * @package WordPress
 * @subpackage Pieter Goosen
 * @since pietergoosen 1.0
 */



wp_reset_postdata();
global $post;

// Define shared post arguments
$categories = get_the_category($post->ID);
if ($categories) :
$category_ids = array();
foreach($categories as $individual_category) 
$category_ids[] = $individual_category->term_id;

$args=array(
'category__in' => $category_ids,
'post__not_in' => array($post->ID),
'posts_per_page'=>3, // Number of related posts that will be shown.
'ignore_sticky_posts'=>1
);

$query = new wp_query($args);

if ( $query->have_posts() ): ?>

<div class="related-posts block">

<h4 class="heading">
<?php _e('You may also like &hellip;','pietergoosen'); ?>
</h4>

<div class="related-posts group">

<?php while ( $query->have_posts() ) : $query->the_post(); ?>

 <--- YOUR LOOP GOES HERE--->

<?php endwhile; ?>

This code checks for the category (using WP_Query)of the post that is displayed using get_the_category($post->ID). From this, posts from this category is fetched. In this code, three posts ('posts_per_page'=>3,) will be displayed from the category of the post currently displayed. You can customize the query to suite your needs, also, you can customize your loop just the way you want to.

You can now call this in your single.php where you need to display your related posts using

get_template_part('content', 'related');

As for the styling, you will need to sort that out yourself. CSS related stuff is off-topic here BTW

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.