Forums

Multiple conditional statements to follow? (13 posts)

  1. Kahil
    Member
    Posted 2 years ago #

    OK... How can I make a loop follow two conditional statements. More specifically the is_category and is_tag. I want the loop to just see if it is a category archive and then only show posts for a specific tag. I know how to make it check for one or the other when it comes to conditional statements...I just can't figure out how to make it follow both at the same time.

    Basically, I am using multiple loops in the archive template. Each loop is set to show posts for a specific tag. When I go to a category archive, I want it to show only the posts in that category while each loop only shows those specific tags in that category.

    Hope that makes sense...

  2. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

    How about <?php if( is_category() && in_array( 'foo', get_the_tags() ) ) ) )?> where "foo" is the tag that you want to display?

    http://codex.wordpress.org/Function_Reference/get_the_tags

  3. Kahil
    Member
    Posted 2 years ago #

    I'll give it a try

  4. Kahil
    Member
    Posted 2 years ago #

    That didn't work...

  5. Kahil
    Member
    Posted 2 years ago #

    I know that with php, I can use the syntax of || between to pieces to tell it to do one OR the other. I can't seem to find one that signifies telling it to do this AND that.

  6. xdesi
    Member
    Posted 2 years ago #

    && is the PHP equivalant

  7. Mark / t31os
    Moderator
    Posted 2 years ago #

    You can use OR or || for "Or"..
    You can also use AND or && for "And"..

    || and && are ever so slightly faster though, which is why they are more common.

  8. Kahil
    Member
    Posted 2 years ago #

    well... i thought that too, but couldn't find a reputable online source that explicitly said that.

    Here is what I have that isn't working...

    <?php if (is_category() && is_tag('30-sheets')); ?>

    and for the following loop...

    <?php rewind_posts(); ?>
     <?php if (is_category() && is_tag('mall-kiosks')); ?>

    for the particular category i am in, only one loop should show a post, the first one. right now it is showing the same post in both loops despite having different tags.

  9. Mark / t31os
    Moderator
    Posted 2 years ago #

    Test the conditions...
    Example:

    if( is_category() ) echo 'cat condition met';
    else echo 'cat condition failed';
    
    if( is_tag('30-sheets') ) echo 'tag condition met';
    else echo 'tag condition failed';

    Something totally obvious..

    ---

    RE: The php operators..
    http://www.php.net/manual/en/language.operators.logical.php
    http://www.php.net/manual/en/language.operators.precedence.php

  10. xdesi
    Member
    Posted 2 years ago #

    You need to have a parameter in is_category(), the cat id number or cat slug e.g

    is_category('2')
    or
    is_category('cat-name')

    EDIT: Sorry you don't if it's on the category page. Ref

  11. Kahil
    Member
    Posted 2 years ago #

    you don't have to define a category. according to your reference, if you leave that empty it will go for any category archive.

    The conditions will work, but only for one loop for some reason. despite declaring a specific tag in the first loop, it still shows the post with the other tag.

  12. Kahil
    Member
    Posted 2 years ago #

    here is what i have for my first loop...

    <?php if (is_category() && is_tag('30-sheets')); ?>
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); $do_not_duplicate = $post->ID; ?>

    and here is what i have for my second loop...

    <?php rewind_posts(); ?>
    <?php if (is_category() && is_tag('mall-kiosks')); ?>
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>
  13. Kahil
    Member
    Posted 2 years ago #

    I figured it out on my own. If anyone is interested, here is a sample of the solution.

    At the top of my template, before any loops, I have the following code to check to see what category the page I am on is and return its ID.

    <?php foreach(get_the_category() as $category)
    { $thecat = $category->cat_ID; } ?>

    Then, at the start of each loop I have the following. This code is set to call specific, constant post tags in respect to the particular category archive I am on.

    <h2>30 Sheets</h2>
        <?php query_posts('cat=' . $thecat . '&tag=30-sheets&order=ASC'); ?>
        <?php if (have_posts()) : ?>
        <?php while (have_posts()) : the_post(); ?>
    
        ---- THE CONTENT YOU WANT TO SHOW GOES HERE ----
    
        <?php endwhile; ?>
    
        <?php else : ?>
        <div id="page">
        <p class="center">There are no records for this media.</p>
        </div>
    
        <?php endif; ?>
    
        <h2>Mall Kiosks</h2>
        <?php query_posts('cat=' . $thecat . '&tag=mall-kiosks&order=ASC'); ?>
        <?php if (have_posts()) : ?>
        <?php while (have_posts()) : the_post(); ?>
    
        ---- THE CONTENT YOU WANT TO SHOW GOES HERE ----
    
        <?php endwhile; ?>
    
        <?php else : ?>
        <div id="page">
        <p class="center">There are no records for this media.</p>
        </div>
    
        <?php endif; ?>

    You can do this for as many loops and tags as you like.

Topic Closed

This topic has been closed to new replies.

About this Topic