Create html from function

This all boils down to what needs to go into your functions.php and what not. This might just be personal preference here, but I would strongly advice you to not add elements from your loop to a function. Yes, you don’t like the repetition. Things like custom pagination functions, add these to your functions.php inside a function and then call that function inside your template.

I would suggest to have a look at other alternatives to tackle this problem. In a case like this, the most useful alternative to use here is to create a content.php type of template that you can call into your page template via get_template_part.

As an example:

Create a file in the root of your theme and call it something like content-custom.php. Add all the above code in this template. You can now just simply call this template where needed in your page templates as follows

<?php get_template_part( 'content', 'custom' ); ?>

From my comment that I deleted now

Just a thought here, I would stay with the structure that you have. I would try to reduce the amount of templates though, try to optimize them rather. One or two conditional statement in a page template can reduce the need for a couple of extra templates. I would also have a look at creating a dynamic page template. Seriously, I would not do what you are trying to do

As I stated above, it seems that you are using quite a lot of page templates on your site, you need to have a look at and see where you can optimize (if possible) a couple of page templates with the use of conditional statements/tags to cut out the need for such a big amount of templates.

I would also look at dynamic page templates here. Actually I call them dynamic page templates, as this involves one page template that can be reused many times. Options on what exactly to display on the page is selectable from a metabox in the page editor screen. You can check this post to see what I mean.


I forgot one thing regarding your code, you don’t need to echo when you use the_title() or the_excerpt()