I am implementing a “Recently Post” rendering logic with WordPress recently. Based on @helenhousandi’s example, I did the task through WP_Query()
to pull out my posts.
However, I am facing a architecture issue right now. In WordPress, there are 3 ways to include this loop rendering snippets inside a single.php
file:
1. Put the rendering logic directly in single.php
single.php
<div id="header-announcements">
<h3>Announcements</h3>
<?php
$queryObject = new WP_Query( 'post_type=announcements&posts_per_page=5' );
// The Loop!
if ($queryObject->have_posts()) {
?>
<ul>
<?php
while ($queryObject->have_posts()) {
$queryObject->the_post();
?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php
}
?>
</ul>
<div><a href="#">View More</a></div>
<?php
}
?>
</div>
This is the easiest way, but hard to reuse for other custom post type.
2. Using get_template_url()
to include the loop rendering logic
conctent-recently-post.php
<?php
$queryObject = new WP_Query( 'post_type=announcements&posts_per_page=5' );
// The Loop!
if ($queryObject->have_posts()) {
?>
<ul>
<?php
while ($queryObject->have_posts()) {
$queryObject->the_post();
?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php
}
?>
</ul>
<div><a href="#">View More</a></div>
<?php
}
?>
single.php
<div id="header-announcements">
<h3>Announcements</h3>
<?php get_template_url( 'content', 'recently-post'); ?>
</div>
Put the rendering logic in a separate template file, say content-recently-post.php
, and then include that in single.php
. This should be better, since it could be reused in other template files.
What fall short here is that, the post_type
and posts_per_page
are tightly coupled with the rendering logic, so it’s still hard to reuse.
3. Register a function in functions.php
, and call the function in single.php
functions.php
<?php
if(!function_exists('ka_show_recently_post')) :
function ka_show_recently_post($post_type, $num) {
$queryObject = new WP_Query( 'post_type=' . $post_type . '&posts_per_page=' . $num );
if ($queryObject->have_posts()) :
echo '<ul>';
while ($queryObject->have_posts()) :
$queryObject->the_post();
echo '<li><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></li>';
endwhile;
echo '</ul>';
endif;
}
endif;
?>
single.php
<div id="header-announcements">
<h3>Announcements</h3>
<?php ka_show_recently_post('announcements', 5) ?>
</div>
The good part for this approach is that it allows you to reuse it according to the post_type
and posts_per_page
that you would like to, but I think it’s a little weird to put these kinds of rendering logic in functions.php
. We should put all these template relating logic in separate template files, which forms a better structure for future maintenance, shouldn’t we?
I am wondering is there any other better ways to solve the rendering
logic in WordPress like that in this example?
You can combine 2&3.
Use a function that accepts $posttype as an argument.
Extract the template part to a template file
and include the template file in the function.