The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End help with foreach loop Reply To: help with foreach loop


I don’t know about wordpress, so I couldn’t offer much advice on that front.

In fairness, I don’t quite understand what your objective is. However, the fact that you’ve been trying for so long without success would seem to indicate that it’s simply not something WP was designed to do, or is well-suited for. WP is a mature platform, with a well-established way of doing things. It is intended to be used in a particular way. It’s not really a proving ground for new ideas.

I can say that, if print_r( $template ) results in

template-discography.php template-page.php template-news.php template-shows.php template-videos.php template-band.php template-images.php default page-templates/front-page.php template-page.php template-contact.php default template-twitter.php template-widgets.php template-test.php

then $template is a string. foreach requires an array or iterable object, which is why you have the “invalid argument” error.

You could split that string on the spaces to create an array:

$array_of_templates = explode( ' ',$template );