• Resolved shawmutsteve

    (@shawmutsteve)


    Hi

    I wrote a plugin that displays dynamic, non-WP (real estate) content in pages and posts. I allow the admin to select which theme template to use so that they can decide if my dynamic content will be full page or have a sidebar.

    My plugin renders a few different types of dynamic content. Real Estate listing pages display correctly using the specified template. However, the Property Detail page is not displaying with the specified template – always defaulting to the theme default template. My debug clearly shows that I am correctly specifying the template to use.

    I was originally using this to tell WP which template to use:

    update_post_meta($posts[0]->ID, '_wp_page_template', $roverTemplate);

    I saw this problem, and wondered if it wasn’t working because I was using a bogus page_id. So I created a record in wp_posts, got the page_id (ID) and used it. Same results. Hmmh.

    I was just wondering if anyone had any suggestions on how to troubleshoot this.

    Thank you
    Steve

Viewing 3 replies - 16 through 18 (of 18 total)
  • smarie33

    (@smarie33)

    So I figured out the problem by comparing it to a template page I created a long time ago that worked when I added it dynamically. Turns out that the description of the template has to be between its own php brackets, then the header!

    CORRECT:

    <?php
    /**
     * Template Name: Thank You
     * Description: A Page Template that adds the code for the editor to process the posts
     */
     ?>
    
    <?php get_header(); ?>

    INCORRECT

    <?php
    /**
     * Template Name: Thank You
     * Description: A Page Template that adds the code for the editor to process the posts
     */
    
     get_header(); ?>

    even though the “incorrect” way works just fine if you’re creating a template!

    smarie33

    (@smarie33)

    I also found out that the update_post_meta function needs to have double quotes in its parameters… weird that I’m not forced to use double quotes anywhere else.

    so this:

    update_post_meta($post_id, "_wp_page_template", "page-response.php");

    NOT this:

    update_post_meta($post_id, '_wp_page_template', 'page-response.php');

    Sometimes it helps to refresh the permalinks if something seems stuck as well.

Viewing 3 replies - 16 through 18 (of 18 total)
  • The topic ‘update_post_meta( $id, '_wp_page_template', '_myTemplate') working inconsistentl’ is closed to new replies.