WordPress.org

Ready to get started?Download WordPress

Forums

Use Body class in page template to show a category (11 posts)

  1. Ed Mac
    Member
    Posted 3 years ago #

    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

  2. s_ha_dum
    Member
    Posted 3 years ago #

    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?

  3. Ed Mac
    Member
    Posted 3 years ago #

    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...

  4. s_ha_dum
    Member
    Posted 3 years ago #

    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?

  5. Ed Mac
    Member
    Posted 3 years ago #

    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?

  6. s_ha_dum
    Member
    Posted 3 years ago #

    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(); ?>

  7. Ed Mac
    Member
    Posted 3 years ago #

    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...

  8. s_ha_dum
    Member
    Posted 3 years ago #

    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.

  9. Ed Mac
    Member
    Posted 3 years ago #

    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?

  10. Ed Mac
    Member
    Posted 3 years ago #

    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?

  11. s_ha_dum
    Member
    Posted 3 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic