WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to Exclude Category from foreach loop (5 posts)

  1. jr99
    Member
    Posted 1 year ago #

    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

  2. RoordaTim
    Member
    Posted 1 year ago #

    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; ?>
  3. jr99
    Member
    Posted 1 year ago #

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

  4. RoordaTim
    Member
    Posted 1 year ago #

    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.

  5. jr99
    Member
    Posted 1 year ago #

    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! :)

Topic Closed

This topic has been closed to new replies.

About this Topic