• Resolved rodrego

    (@rodrego)


    I have a post which is inside 2 categories, and those categories have different templates.

    When I click on this post, I need to view it in one template if I came from category 1 page, and in another template if I came from category 2 page.

    Is there any way todo that?

    I figured that maybe the only way is to get which page the user was before and use it as a variable. But I have no idea how to write a code for that, or if it’s even possible?

    Anybody know how to do it or has a different solution for the problem?

    Thanks you!

Viewing 2 replies - 1 through 2 (of 2 total)
  • I think this will work, but it is UNTESTED. First, modify archive.php (or category.php, if it exists) by adding a variable to the URL of each post title. The code below is from the default archive.php template. Change this:

    <div <?php post_class() ?>>
          <h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
          <small><?php the_time('l, F jS, Y') ?></small>

    to this:

    <div <?php post_class() ?>>
          <?php $permalink = get_permalink();
          $specialcat = get_query_var('cat');
          if (is_category() && ($specialcat == 1 || $specialcat == 2)) $permalink = add_query_arg('specialcat',$specialcat,$permalink);  ?>
          <h3 id="post-<?php the_ID(); ?>"><a href="<?php echo $permalink; ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
          <small><?php the_time('l, F jS, Y') ?></small>

    Next, rename single.php to single-default.php. Create a new single.php with this code:

    <?php
    $specialcat = $_GET['specialcat'];
    if ( $specialcat == 1 ) {
      include(TEMPLATEPATH . '/single-special-1.php');
    } elseif ( $specialcat == 2 ) {
      include(TEMPLATEPATH . '/single-special-2.php');
    } else {
      include(TEMPLATEPATH . '/single-default.php');
    }
    ?>

    Replace 1 and 2 with the category ids of your special categories, and single-special-n.php with the names of your templates.

    Thread Starter rodrego

    (@rodrego)

    Thanks vtxyzzy,

    it worked like charm! It’s great to know WordPress can handle this.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘2 different templates for the same post’ is closed to new replies.