Support » Fixing WordPress » How To Get Template of another Page?

  • Moderator Andrew Nevins

    (@anevins)


    I have the ID of a page,
    I just need to grab the template situated with this page.

    What function would do this for me?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator Jan Dembowski

    (@jdembowski)

    Brute Squad and Volunteer Moderator

    Moderator Andrew Nevins

    (@anevins)

    I’ve tried

    global $wp_query;
    
    $template_name = get_post_meta( $wp_query->post->ID, '_wp_page_template', true );

    $template_name holds the current page template, whereas I want to use another pages’ ID.

    So I’ve tried $post->ID (I can access this, it returns integer).

    $template_name = get_post_meta( $post->ID, '_wp_page_template', true );

    but $template_name returns an empty string.

    global $wpdb;
    $query = 'SELECT meta_value FROM '.$wpdb->prefix.'postmeta WHERE meta_key = '_wp_page_template' AND post_id = '.$post->ID;
    
    $template_name = $wpdb->get_var($wpdb->prepare($query));

    Give that a shot. It’s off the cuff so you may have to tweak it.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How To Get Template of another Page?’ is closed to new replies.