• I needed to serve up the post content only.
    Everything else, including the header, footer and the sidebars needed to be stripped out.
    I needed this ‘post content only’ version to be always available, for every post, but for the original fully styled posts to be available as well.

    And I found this!!
    http://scratch99.com/wordpress/development/how-to-change-post-template-via-url-parameter/

    I added the following code to my theme’s functions.php file:

    function sjc_add_query_vars($vars) {
        return array('template') + $vars;
    }
    add_filter('query_vars', 'sjc_add_query_vars');
    
    function sjc_template($template) {
      global $wp;
      if ($wp->query_vars['template']=='basic') {
        return dirname( __FILE__ ) . '/single-basic.php';
      }
      else {
        return $template;
      }
    }
    add_filter('single_template', 'sjc_template');

    And I created a file called single-basic.php, then added the following code.

    <div id="content">
        <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
              <h2 class="item"><span class="fn"><?php the_title(); ?></span></h2>
            <?php the_content(); ?>
        <?php endwhile; else: ?>
        <?php endif; ?>
    </div>

    But It didn’t work!
    (http://domain.com/postname/?template=basic) is still blank page.
    Please help me…

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

    (@bcworkz)

    Your code as posted works fine on my site. Try defining WP_DEBUG as true in wp-config.php. You should then get error messages instead of a blank white screen. What those messages are should be a clue to what you need to fix.

    If you get no messages with WP_DEBUG as true, you may have a conflict with your theme or one of your plugins that prevents your code from working.

    Thread Starter keh3084

    (@keh3084)

    Warning: include(/home/hosting_users/keh3084/wp-content/themes/dobp/single-basic.php): failed to open stream: No such file or directory in /home/hosting_users/keh3084/wp-includes/template-loader.php on line 74 Warning: include(): Failed opening ‘/home/hosting_users/keh3084/wp-content/themes/dobp/single-basic.php’ for inclusion (include_path=’.:/usr/local/php53/lib/php’) in /home/hosting_users/keh3084/wp-includes/template-loader.php on line 74

    Here is my error messages. What should I do?

    Thread Starter keh3084

    (@keh3084)

    Oh, Thank you so much. I found my problem.
    (I entered the codes in functions.php (parent theme not child), then It works!)

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Change post template via url parameter’ is closed to new replies.