Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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, '/'))
            continue;

        $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
add comment

2 Answers

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.

Use

$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
add comment

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
add comment

Your Answer

 
discard

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.