2

I would like to know how I can loop through pages with a specific template name. I have several pages where the admin can select a template name of SALE. I want to display all content with this template name via the loop.

Any ideas are much appreciated!

4
  • Kindly refer : wordpress.stackexchange.com/questions/46582/… Dec 29, 2014 at 11:29
  • @HelpingHands thanks, but as far I can see its an example with page IDs. I want to display the pages via template! Could you still help me with this?
    – user49869
    Dec 29, 2014 at 12:03
  • Does that link not helping you? shared by @Bainternet?? Dec 29, 2014 at 12:05
  • 1
    @HelpingHands sorry but what link to you mean? Cannot see a user named Bainternet.
    – user49869
    Dec 29, 2014 at 14:53

2 Answers 2

4

Wordpress saves page templates that is assigned to a specific page in the db in the wp_postmeta table.

These are saved as follows:

'meta_key' => '_wp_page_template',
'meta_value' => 'NAME_ OF_TEMPLATE'

With this in mind, you can loop through pages which shares a specific page template, using get_pages (You can also make use of WP_Query)

Example:

$pages = get_pages(array(
    'meta_key' => '_wp_page_template',
    'meta_value' => 'page-special.php'
));
foreach($pages as $page){
    echo $page->ID.'<br />';
    echo apply_filters( 'the_content', $page->post_content );
}
3
  • thanks, this works well. Only one thing: if I would like to have every content of $page would I do echo $page->the_content? Because this gives me an error.
    – user49869
    Jan 1, 2015 at 17:28
  • Check my update. You have to make use of the WP_Post objects. You cannot use the normal template tags :-) Jan 1, 2015 at 17:31
  • My pleasure, glad it worked. Enjoy :-) Jan 1, 2015 at 17:35
0

Use get_template_part, for example if your template is named sale.php you can use:

get_template_part( 'sale' );  

http://codex.wordpress.org/Function_Reference/get_template_part

2
  • thanks but this does not really solve what I intend, or I dont understand it as I should.. basically what I want to do is do a new query and instead of selecting a specific post, I select a specific template: new WP_Query("template_name=sale"); something like this?
    – user49869
    Dec 31, 2014 at 12:54
  • That doesn't really make any sense, WordPress info is stored in a database and not a template.
    – Wyck
    Dec 31, 2014 at 18:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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