• borntonag

    (@borntonag)


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

Viewing 7 replies - 1 through 7 (of 7 total)
  • Michael

    (@alchymyth)

    https://codex.www.ads-software.com/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
    https://codex.www.ads-software.com/Function_Reference/query_posts

    Oleg Dudkin

    (@olegdudkin)

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

    Thread Starter borntonag

    (@borntonag)

    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.

    Michael

    (@alchymyth)

    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 https://codex.www.ads-software.com/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: https://codex.www.ads-software.com/Class_Reference/WP_Query#Parameters

    Thread Starter borntonag

    (@borntonag)

    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]

    Thread Starter borntonag

    (@borntonag)

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

    You have 3 options:
    1. Try to access https://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.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Posts on page by category’ is closed to new replies.