1

I'm using the following page template to display the posts in a single category and plan to format the first post differently than the others. This works as desired but I have the category_name hard coded in the template. I want to use this template for several different categories and would like to learn how to pass the category_name to the template from a link.

For example, the link to the desired page using the special template is http://wildcatweb.net/5th/ and '5th' is also the category_name. How do I tell the template to get the category_name from the URL and use it in the template?

<?php
/*
Template Name: pageAssignments
*/ ?>

<?php get_header(); ?>

<div class="small-12 large-8 columns" id="content" role="main">
<header>
                <h1 class="entry-title"><?php the_title(); ?></h1>

            </header>

  <!-- show latest post FULL -->

 <?php query_posts('showposts=1&category_name=5th'); ?>

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    <div class="lastest-post">



     <h2 class="pagetitle"><?php the_title(); ?></h2>

                        <?php the_content(); ?>

</div><!--close .latest-post -->

 <?php endwhile; endif; ?><!-- end lastest post -->

 <!-- show older post excerpts -->

<?php query_posts('showposts=5&offset=1&category_name=5th'); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
        <div class="older-post">

         <h3 class="pagetitle"><a href="<?php the_permalink() ?>"     rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
        <?php the_excerpt(); ?>

        </div><!--.older-post -->

<?php endwhile; endif; ?><!-- end past-entry -->

</div>

<?php get_sidebar(); ?>

<?php get_footer(); ?>
1

Rather than creating pages and assigning a template, I would use the category_template filter to load a specific template for all those particular categories. In this example I use a hardcoded array, but this could be adapted to load an option which stores the category slugs you want the template applied to.

function wpa_category_template( $templates = '' ){
    $special_categories = array(
        'one',
        'another',
        'more'
    );
    $this_category = get_queried_object();
    if( in_array( $this_category->slug, $special_categories ) ){
        $templates = locate_template( array( 'special_category.php', $templates ), false );
    }   
    return $templates;
}
add_filter( 'category_template', 'wpa_category_template' );

You then no longer have to query for these posts in the template, as the posts are already in the main query. (also, as an aside, never use query_posts ).

Within the template you can use single_cat_title to output the name.

You also don't have to use two queries and loops to style the first post differently, just check the current_post var in the loop to know what post you're currently outputting.

if (have_posts()):
    while (have_posts()):
        the_post();

        if( 0 == $wp_query->current_post ):
            echo 'this is the first post';
        else:
            echo 'this is post > 1';
        endif;

    endwhile;
endif;
| improve this answer | |
  • Milo, thank you for your help. Your concise solution was perfect and much simpler than my first attempt. I'm quickly learning a lot about Wordpress and your reply was exactly what I needed. Thanks, again. – user3154076 Jan 3 '14 at 18:55
0

If you wish to use this template for multiple categories, presumably you have named it category.php?

To get the name of the Term that is currently being displayed, use this (before you enter The Loop) -

$taxonomy_slug = $wp_query->tax_query->queries[0]['taxonomy'];
$term_slug = $wp_query->tax_query->queries[0]['terms'][0];
$term = get_term_by('slug', $term_slug, $taxonomy_slug);
$term_name = $term->name;

And then replace -

<h1 class="entry-title"><?php the_title(); ?></h1>

With -

<h1 class="entry-title"><?php echo $term_name ?></h1>
| improve this answer | |
  • I didn't name the file category.php because I only want to use it for a few category since I am planning to add some special formatting for only 5-6 pages only. Thanks for showing me how to echo the query. I think that will be helpful for a variety of things. Will look at it more when I have a chance later today. – user3154076 Jan 2 '14 at 16:49
0

WordPress will use the first Template file it finds in your current Theme's directory from the following list:

  1. category-slug.php
  2. category-ID.php
  3. category.php
  4. archive.php
  5. index.php

Source http://codex.wordpress.org/Category_Templates

You can add the category name by adding this code to the template before The Loop:

<p>Category: <?php single_cat_title(); ?></p>
| improve this answer | |
  • I don't want this template to apply to all category pages, only the 5-6 that need special formatting. That is why I'm creating a different template. Outside of duplicating the template for each category-ID or category-slug do have an idea how I could pass the category name to the template? Thanks for your help – user3154076 Jan 2 '14 at 16:55
  • Updated my answer with the code. – Brad Dalton Jan 2 '14 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.