Ready to get started?Download WordPress


[resolved] How to show only one category on front page (7 posts)

  1. ga kvarud
    Posted 5 years ago #

    I have what is probably a rather simple blog, sat up on my own domain.
    The front page shows all the posts, then the sidebar shows a list of categories, and by clicking on this, only post from that category is shown.
    I would like to have everything like this, except that the default page should show only posts from one category. Is this possible?
    (That is, I want http://mysite.com to show the same result as http://mysite.com/category/frontpageposts)


  2. MichaelH
    Posted 5 years ago #

    Place this just before your loop in index.php to show Posts in Category ID = 20:

    <?php query_posts ($query_string . '&cat=20'); ?>

    Stepping Into Template Tags
    Stepping Into Templates
    Template Hierarchy
    Template Tags

  3. ga kvarud
    Posted 5 years ago #

    Thank you so much. I struggled a bit until I found which index.php it was, and where the loop was in the file, but i did find it, and it did work. (For other newbies: It was the index.php in the tarski (my theme) directory, and in the else-clause of the first if.)

    Again: Thank you so much for your answer.


  4. baraz
    Posted 5 years ago #

    My question is a bit related with your question so I decided to put my question here instead of making new topic. I have read The Loop, but I found it a lil bit confusing. Maybe some explanation from wp user more understandable for me :)

    What should I do to make multiple loops for the front page with different condition on each loops.
    for instance, I want to make first loop containing posts from child of category A, and the second loop shows posts from category B ?


  5. lmilesw
    Posted 4 years ago #

    I am somewhat of a WordPress newbe as well and don't understnd exactly where to put this code. I have read several places where you put some code "just before the loop" but when I do that in this case every category I go to only the category I selected shows not just on the front page. Can you show me an example bit of code as to where in the in the index.php file this code goes. Thanks.

  6. Mark / t31os
    Posted 4 years ago #

    Regarding the above, this part is a little pointless.

    <?php $posts=query_posts($query_string . '&cat=8'); ?>

    In might aswell be..

    <?php query_posts($query_string . '&cat=8'); ?>

    As there's absolutely no benefit or reasoning to adding the query_posts line onto a variable as you have done...


    Pagination will work if you setup the postdata and/or add the paging variable into the query_posts parameters (depending on the type of query you use, query_posts, foreach loop, etc).... Complicated really is subjective, you either understand the instructions given or you don't.

    These "How do i limit to one category" questions have been asked numerous times on the forum, so amongst the pages of instructions and threads on this forum i do find it a little hard to understand how even a non-coder struggles.

    1. Determine which file handles the page you want to effect posts on.
    http://codex.wordpress.org/images/1/18/Template_Hierarchy.png - Nice easy way to determine which file handles what.
    2. Open said file, and add a conditional
    3. Example

    If index.php handles various cases for you site (let's say it handles the home/front page, but also handles other areas to)..

    You'd find the loop first...

    If you don't have a query_posts line, you can it in, else add to it as Michael showed by preserving the existing query string.

    Then add a conditional before or around it..

    ..here's some examples (much like Michaels initial one).

    if(is_home()) {
    query_posts( $query_string . '&cat=1' );
    if(is_front_page()) {

    If you have a query posts line already in your file, then you just add an else condition to the above examples..

    else {
    // If you had a query posts call
    // query_posts( $query_string );
    // or

    I could go much more in-depth, but i feel i'd only be repeating what's already been discussed numerous times in various other threads.

  7. alqamardesigns
    Posted 4 years ago #

    Hi, I have the following code in my footer.php

    <?php recent_posts('limit=4'); ?>

    displaying 4 of my most recent posts.

    But I only want it to show from Category 1. Please advice, what changes to the above php code do I need to do?

    Many thanks.


Topic Closed

This topic has been closed to new replies.

About this Topic