Support » Plugins and Hacks » Hacks » Use Body class in page template to show a category

  • Ed Mac

    (@edoardo-melchiori)


    Hi everybody,
    on my site I want to divide every category of posts in different pages.
    Right now I am using a plugin to redirect the page “cars” to the “www.mysite.com/category/cars”

    this way I cannot change SEO for the page and I also can’t change the image (using super standard wordpress twenty ten theme) of the page “www.mysite.com/category/cars” so I would like to do this: create a custom page template that shows only posts from a single category using Body Classes.
    This way I could have a “cars” page with its own cars page template loading all posts in the cars category.

    Is there anybody that could help me with a code example? NB: I don’t want a list of posts (I could do this with a plugin)… I want a page that looks like the category page template!
    Thanx a lot

Viewing 10 replies - 1 through 10 (of 10 total)
  • It should be fairly easy to create a template that only loads one category, but I don’t know what you mean by “using Body Classes”. How are you hoping for that to work?

    Ed Mac

    (@edoardo-melchiori)

    Body class was an idea, something like “load category category-cars”. But I have no idea on how to write it down in code.
    If you have an easier way to achieve the result…

    Well, you know what the category is so there is no need for anything complicated. You’ll need to create your template file and add this part. Do you know how to create the template?

    Ed Mac

    (@edoardo-melchiori)

    Yes I know that, I already have my “car” template page up & working. I’ve done it duplicating another template and pasting inside a part of the standard page template. Actually now works as the standard page template.
    So you say I simply have to add query_posts( array ( 'category_name' => 'The Category Name', 'posts_per_page' => -1 ) ); to my template php file?
    Where do I have to put this code exactly?

    Ok. You are almost there.

    The call to query_posts should go before your Loop starts. Look for something like this, <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    Ed Mac

    (@edoardo-melchiori)

    Ok… my template looks like this:

    <?php
    /**
     * Template Name: Landing
     *
     * A custom page template for the landing page
     *
     * The "Template Name:" bit above allows this to be selectable
     * from a dropdown menu on the edit page screen.
     *
     * @package WordPress
     * @subpackage Twenty_Ten
     * @since Twenty Ten 1.0
     */
    
    get_header(); ?>
    
    		<div id="container">
    			<div id="content" role="main">
    
    			<?php
    			/* Run the loop to output the page.
    			 * If you want to overload this in a child theme then include a file
    			 * called loop-page.php and that will be used instead.
    			 */
    			get_template_part( 'loop', 'page' );
    			?>
    
    			</div><!-- #content -->
    		</div><!-- #container -->
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    but if I modify it into:

    <?php
    /**
     * Template Name: Landing
     *
     * A custom page template for the landing page
     *
     * The "Template Name:" bit above allows this to be selectable
     * from a dropdown menu on the edit page screen.
     *
     * @package WordPress
     * @subpackage Twenty_Ten
     * @since Twenty Ten 1.0
     */
    
    get_header(); ?>
    
    		<div id="container">
    			<div id="content" role="main">
    query_posts( array ( 'category_name' => 'landing', 'posts_per_page' => -1 ) );
    			<?php
    			/* Run the loop to output the page.
    			 * If you want to overload this in a child theme then include a file
    			 * called loop-page.php and that will be used instead.
    			 */
    			get_template_part( 'loop', 'page' );
    			?>
    
    			</div><!-- #content -->
    		</div><!-- #container -->
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    Doesn’t work…

    query_posts needs to be inside the php tags. That is, it needs to be after the <?php and before the get_template_part( 'loop', 'page' );. Try that.

    Ed Mac

    (@edoardo-melchiori)

    Awesome it works now. Thank you very much!!!
    This page is now perfect. What if in other pages I would like to show just the beginning of each post with the “Continue reading” link? Is there a way?

    Ed Mac

    (@edoardo-melchiori)

    A strange thing happens: the title of the post is not a link to the post page where you can comment it. It’s a simple text with no link… this is weird… any ideas?

    1) http://codex.wordpress.org/Function_Reference/the_content#Overriding_Archive.2FSingle_Page_Behavior

    2) That title issue is strange but the problem in probably in your loop-page.php file.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Use Body class in page template to show a category’ is closed to new replies.