WordPress.org

Forums

Tell to a query to select only the current category in the archive template (7 posts)

  1. Pixelboss
    Member
    Posted 8 years ago #

    It must be something obvious but I can't find it ! So I call some help. Sometimes you are searching your glasses and they are on your nose, isn't it ?

    I have made a dropdownmenu witch list each category on my blog with :

    <? wp_list_cats('sort_column=name&exclude=7'); ?>

    When you click on "Books", you arrive on the category page. This one obeys to the archive template and so display the list of all the posts in this particular category. Easy until this point.

    My problem is that I want too to display 2 posts (with title, texte and all stuff) of this category below this list... randomly !

    So I have installed this wonderfull plugin (works like a charm) : http://wordpress.org/support/topic/70359?replies=3

    And I have put like it is advised this line in my archive template :

    <? query_posts('cat=&showposts=2&random=true'); ?>

    2 posts are displayed randomly... but I can't find how to indicate to the query that she must take these posts in the current category... and I don't want to make 11 different templates... There must be a solution.

    I have try is_category() whithout success, some pieces of code about current category in the codex and it doesn't work. I have search on the forum and found nothing about my problem.

    Is there someone to save me and the world ?

    THanks in advance for your attention.

  2. Kafkaesqui

    Posted 8 years ago #

    Try:

    <?php query_posts('cat= ' . $cat . '&showposts=2&random=true'); ?>

    $cat is a global variable in WordPress that holds the 'current' category's id (when on a category query page).

  3. Pixelboss
    Member
    Posted 8 years ago #

    Thanks for your answer but it doesn't make the job. It returns always the posts randomly but whithout take care of the current category... ??!

    You can see the structure of the page here : http://www.jaideuxamours.fr/categorie/ateliers/

  4. Kafkaesqui

    Posted 8 years ago #

    Well I'd have to look closer at what the code for that plugin is doing, but are you able to restrict to a category when setting one statically? As in:

    <?php query_posts('cat=2&showposts=2&random=true'); ?>

    Does that only then show 2 random posts from category 2?

  5. Pixelboss
    Member
    Posted 8 years ago #

    Yes it does ! I don't think it's the plugin action because I understand what you say but it makes the job. When I put cat=2, only posts of the category 2 are displayed.... ???

  6. Kafkaesqui

    Posted 8 years ago #

    "Yes it does !"

    Well, that's good then. Of course the question is why $cat is not returning the current category's ID (which it should).

    First, try scoping it to global, that is add this:

    global $cat;

    before your query_posts().

    Another thing to try (especially if the suggestion above still fails to help) is to echo $cat to see if it holds a value:

    <?php echo $cat; ?>

  7. Pixelboss
    Member
    Posted 8 years ago #

    1000000000000000000 thanks !! The first solutions is the good one : "global $cat; before your query_posts()."
    Great now :-)) !!

Topic Closed

This topic has been closed to new replies.

About this Topic