wordpress - Where the view rendering logic belongs to, functions.php or content-partial template? -


i implementing "recently post" rendering logic wordpress recently. based on @helenhousandi's example, did task through wp_query() pull out posts.

however, facing architecture issue right now. in wordpress, there 3 ways include loop rendering snippets inside single.php file:


1. put 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' );     // 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 easiest way, hard reuse other custom post type.


2. using get_template_url() include loop rendering logic

conctent-recently-post.php

<?php $queryobject = new wp_query( 'post_type=announcements&posts_per_page=5' ); // 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 rendering logic in separate template file, content-recently-post.php, , include in single.php. should better, since reused in other template files.

what fall short here that, post_type , posts_per_page tightly coupled rendering logic, it's still hard reuse.


3. register function in functions.php, , call 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 part approach allows reuse according post_type , posts_per_page to, think it's little weird put these kinds of rendering logic in functions.php. should put these template relating logic in separate template files, forms better structure future maintenance, shouldn't we?

i wondering there other better ways solve rendering logic in wordpress in example?

you can combine 2&3.

use function accepts $posttype argument.

extract template part template file , include template file in function.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -