WordPress.org

Ready to get started?Download WordPress

Forums

Posts on page by category (8 posts)

  1. borntonag
    Member
    Posted 2 years ago #

    Hi,

    My "problem" is: I have 4 pages with the same name of 4 categories I created before. I want that on each page appears only the posts of a certain category, but I want the posts to be display like in home page.

    Any suggestions?

    Greetings;)

  2. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    http://codex.wordpress.org/Pages#Page_Templates

    start with the code from index.php (depending on your theme) and use that as a starting point for a page template; then add a query before the start of the loop
    http://codex.wordpress.org/Function_Reference/query_posts

  3. Oleg Dudkin
    Member
    Posted 2 years ago #

    Why not to use WordPress built-in links to category pages?

  4. borntonag
    Member
    Posted 2 years ago #

    So alchymyth what I have to do is duplicate the index.php (page template) and then add a query before the loop?

    Oleg Dudkin what you mean by that? Sorry but I don't know what you're suggesting, it's the first time I work with wordpress.

  5. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    duplicate index.php and save as catposts-page-template.php (example only, use your own rememberable file name)

    then add the neccessary lines at the top of that file; example:

    <?php
    /*
    Template Name: Category Posts Template
    */
    ?>

    then create a query and add this before the line with 'if(have_posts())' (start of the loop http://codex.wordpress.org/The_Loop );
    example:

    <?php $args = array(
    'category_name' => $post->post_name,
    'paged' = get_query_var('paged')
    );
    query_posts( $args ); ?>

    full set of possible query parameters: http://codex.wordpress.org/Class_Reference/WP_Query#Parameters

  6. borntonag
    Member
    Posted 2 years ago #

    alchymyth thank you for your help.

    I got the idea but I can't make it work yet. The name of my category is Nutrição and the URL is nutricao.
    Can you give me a help on the code?
    This is my nutricao-page-template.php

    [code moderated - please use the pastebin for any code longer than 10 lines - see forum rules for posting code]

  7. borntonag
    Member
    Posted 2 years ago #

    Here is the code: http://pastebin.com/ZCKjXGjM
    Where and what exactly do I need do add about query?

  8. Oleg Dudkin
    Member
    Posted 2 years ago #

    You have 3 options:
    1. Try to access http://your.site.com/category/nutricao - there should be category page with list of posts.

    2. Add

    <?php $args = array(
    'category_name' => 'nutricao',
    'paged' = get_query_var('paged')
    );
    query_posts( $args ); ?>

    right after

    <?php
    /*
    Template Name: Nutricao Posts Template
    */
    ?>

    In this case you need 1 template for each category.

    3. Similar as above, but use this code to insert.

    <?php
    global $posts;
    $args = array(
    'category_name' => $posts[0]->post_name,
    'paged' = get_query_var('paged')
    );
    query_posts( $args ); ?>

    In last case you need to name your page exactly like category is named. In your example - 'nutricao'. But this template can be assigned to any number of pages.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.