• Resolved jr99

    (@jr99)


    Hi,

    I’ve been looking around Google and experimenting myself for hours but I can’t figure it out.

    Please can someone show me how to exclude a category from this exact loop:

    <?php $i = 1; ?>
    <?php foreach ( $categories as $category ) : ?>
    	<input type="radio" name="category" value="<?php echo $category->term_id; ?>" id="<?php echo $category->slug; ?>"<?php echo ( ! isset( $_POST['category'] ) && 1 == $i ? ' checked' : '' ); echo ( isset( $_POST['category'] ) && $_POST['category'] == $category->term_id ? ' checked' : '' ); ?> /><label for="<?php echo $category->slug; ?>"><?php echo $category->name; ?></label>
    
    <?php $i++; ?>
    <?php endforeach; ?>

    What it’s doing is creating a check box and label from all my categories but I want to exclude one.

    Thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi jr99,

    Maybe you can use the query_posts() function as described on this page. However you could also use the following, assuming you know the ID of the category you would like to exclude, say this ID is 4. Then you could use this:

    <?php $i = 1; ?>
    <?php foreach ( $categories as $category ) : ?>
        <?php if ( $category->term_id != 4 ) : ?>
    	<input type="radio" name="category" value="<?php echo $category->term_id; ?>" id="<?php echo $category->slug; ?>"<?php echo ( ! isset( $_POST['category'] ) && 1 == $i ? ' checked' : '' ); echo ( isset( $_POST['category'] ) && $_POST['category'] == $category->term_id ? ' checked' : '' ); ?> /><label for="<?php echo $category->slug; ?>"><?php echo $category->name; ?></label>
        <?php endif; ?>
    <?php $i++; ?>
    <?php endforeach; ?>

    Thread Starter jr99

    (@jr99)

    Wow Thanks RoordaTim, your code work out perfect!! ??

    In the near future I might want to exclude multiple cat IDs, I tried putting a comma and the next ID number after the first number but it didn’t work!

    How would I write it for multiple IDs?

    Thank you so much ??

    I suppose the following will work:

    <?php $i = 1; ?>
    <?php foreach ( $categories as $category ) : ?>
        <?php if ( in_array( $category->term_id, array( 4, 5, 6 ) ) ) : ?>
    	<input type="radio" name="category" value="<?php echo $category->term_id; ?>" id="<?php echo $category->slug; ?>"<?php echo ( ! isset( $_POST['category'] ) && 1 == $i ? ' checked' : '' ); echo ( isset( $_POST['category'] ) && $_POST['category'] == $category->term_id ? ' checked' : '' ); ?> /><label for="<?php echo $category->slug; ?>"><?php echo $category->name; ?></label>
        <?php endif; ?>
    <?php $i++; ?>
    <?php endforeach; ?>

    This would then exclude categories 4, 5 and 6. For more information about the php in_array() function, check the documentation.

    Thread Starter jr99

    (@jr99)

    Cool thanks for the response RoordaTim, I had to play around with it a little and this works for me:

    <?php $i = 1; ?>
    <?php foreach ( $categories as $category ) : ?>
             <?php if ( in_array( $category->term_id,  array ( 16, -33, -1, -28 ) ) ) : ?>
    	<input type="radio" name="category" value="<?php echo $category->term_id; ?>" id="<?php echo $category->slug; ?>"<?php echo ( ! isset( $_POST['category'] ) && 1 == $i ? ' checked' : '' ); echo ( isset( $_POST['category'] ) && $_POST['category'] == $category->term_id ? ' checked' : '' ); ?> /><label for="<?php echo $category->slug; ?>"><?php echo $category->name; ?></label>
              <?php endif; ?>
    <?php $i++; ?>
    <?php endforeach; ?>

    Thanks so much! ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to Exclude Category from foreach loop’ is closed to new replies.