WordPress.org

Ready to get started?Download WordPress

Forums

How To Get Template of another Page? (4 posts)

  1. Andrew Nevins
    Spam zapper & Volunteer Moderator
    Posted 1 year ago #

    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?

  2. Andrew Nevins
    Spam zapper & Volunteer Moderator
    Posted 1 year ago #

    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.

  3. Andrew Bartel
    Member
    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.