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.

Basically, I want to allow the user to select which template files he or she wants to use in a post:

single-asc.php:

<?php $custom_posts = new WP_Query(); ?>
            <?php $custom_posts->query('post_type=bbp_topic&order=ASC'); ?>
            <?php while ($custom_posts->have_posts()) : $custom_posts->the_post(); ?>
                <div class="content-block-2">
                    <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
                </div>
            <?php endwhile; ?>

single-desc.php:

        <?php $custom_posts = new WP_Query(); ?>
        <?php $custom_posts->query('post_type=bbp_topic&order=DESC'); ?>
        <?php while ($custom_posts->have_posts()) : $custom_posts->the_post(); ?>
            <div class="content-block-2">
                <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
            </div>
        <?php endwhile; ?>

In this example, the user can choose (maybe by clicking a link) to see a post with the single-asc.php template file or single-desc.php template file.

(the ASC and DESC example is just for reference, these two templates will have different code)

Any suggestions to accomplish this?

share|improve this question
    
By "user", do you mean the post author or the visitor viewing the page? –  goldenapples Mar 25 '11 at 22:01
    
@goldenapples the visitor. –  janoChen Mar 26 '11 at 5:09
add comment

1 Answer

up vote 2 down vote accepted

If the differences in the pages are just query arguments the you can add query vars and use them in the same template:

//add your arguments to query vars
add_filter('query_vars', 'my_query_vars');

function my_query_vars($vars) {
    // add my_sortand ptype to the valid list of variables you can add as many as you want
    $new_vars = array('my_sort','ptype');
    $vars = $new_vars + $vars;
    return $vars;
}

your query should look like this:

<?php $custom_posts = new WP_Query(); ?>
<?php $custom_posts->query(array('post_type' => get_query_var('ptype'), 'order' => get_query_var('my_sort'))); ?>
<?php while ($custom_posts->have_posts()) : $custom_posts->the_post(); ?>
    <div class="content-block-2">
        <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
    </div>
<?php endwhile; ?>

and you links for the user should be:

  • ASC: url + ?ptype=bbp_topic&my_sort=ASC
  • DESC: url + ?ptype=bbp_topic&my_sort=DESC

Now if your differences in the pages are more then query arguments you can change the template page using template_redirect hook:

same as before add your query arg

   //add your arguments to query vars
    add_filter('query_vars', 'my_query_vars');

    function my_query_vars($vars) {
        // add my_sort to the valid list of variables 
        $new_vars = array('my_sort');
        $vars = $new_vars + $vars;
        return $vars;
    }

then add a function to the template_redirect hook and create the redirection based of that argument:

add_action("template_redirect", 'sort_template_redirect');
// Template selection
function sort_template_redirect()
{
    global $wp;
    global $wp_query;
    if (isset($wp->query_vars["my_sort"]))
    {
        // Let's look for the template file in the current theme
        if (array_key_exists('my_sort', $wp->query_vars) && $wp->query_vars['my_sort'] == 'ASC'){
            include(TEMPLATEPATH . '/single-asc.php');
            die();
        }
        if (array_key_exists('my_sort', $wp->query_vars) && $wp->query_vars['my_sort'] == 'DESC'){
            include(TEMPLATEPATH . '/single-desc.php:');
            die();
        }
    }
}

and once again you will need to add the args to the link so:

  • ASC: url + ?my_sort=ASC
  • DESC: url + ?my_sort=DESC
share|improve this answer
    
Thanks again! I will try that. –  janoChen Mar 26 '11 at 5:10
    
One question, In the second example, the value after the = has to be a valid Wordpress value? or I can place anything there? For example, url + ?my_sort=most-voted (In this case, I want to sort posts by vote). –  janoChen Mar 26 '11 at 5:23
    
@janoChen: anything you like because you are just redirecting by it. Your template page would however have to be a valid WordPress page. –  Bainternet Mar 26 '11 at 8:23
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.