WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using Post templates plugin. But its very outdated.

I'm getting Warning: Illegal offset type at this line

$templates = $themes[$theme]['Template Files'];

Here is my full function code.

function get_post_templates() {
    $themes = wp_get_themes();
    $theme = wp_get_theme();
    $templates = $themes[$theme]['Template Files'];
    $post_templates = array();

    $base = array(trailingslashit(get_template_directory()), trailingslashit(get_stylesheet_directory()));

    foreach ((array)$templates as $template) {
        $template = WP_CONTENT_DIR . str_replace(WP_CONTENT_DIR, '', $template); 
        $basename = str_replace($base, '', $template);

        // don't allow template files in subdirectories
        if (false !== strpos($basename, '/'))

        $template_data = implode('', file( $template ));

        $name = '';
        if (preg_match( '|Single Post Template:(.*)$|mi', $template_data, $name))
            $name = _cleanup_header_comment($name[1]);

        if (!empty($name)) {
            if(basename($template) != basename(__FILE__))
                $post_templates[trim($name)] = $basename;

    return $post_templates;


Can someone tell me how to fix that?

share|improve this question
up vote 1 down vote accepted

The Line

$theme = wp_get_theme();

returns a WP_Theme Object, and you cannot use an Object in an Array like this.


$templates = $themes[$theme->template]['Template Files'];

To get the Array of your template Files. In Order to get this function running again, you also have to change Single Post Template to Template Name:

if (preg_match( '|Template Name:(.*)$|mi', $template_data, $name))

As you did not specify what you want to do with the results, I can't help you any further, but you should be okay this way for this function.

share|improve this answer
Thankyou........ – Giri Mar 9 '13 at 12:08

wp_get_theme() returns an object. An object cannot be a key for an array.

Use $theme->get_stylesheet() for the key if you want to get the templates from the child theme and $theme->get_template() if you want the files from the parent theme.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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