Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8555763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:11:55+00:00 2026-06-11T15:11:55+00:00

I am trying to get some help in PHP so I can add a

  • 0

I am trying to get some help in PHP so I can add a class or ID to each individual item returned in a loop. I am using a piece of code (see below) to return taxonomies (locations). I have all my 32 locations returned perfectly and displayed on my site. However, I cannot individually style each location via css as there is no unique class associated with each of them.

Each block that is returned looks like this on the frontend

<div class="block">
<a href="http://example.com/location/new-york">
    <h2>
        <a href="http://example.com/location/new-york">
        Antrim
        <br>
        <span>(1 Farmer)</span>
        </a>
    </h2>

which repeats for all locations returns. As you can see there would be no way to actually style each individually which is what I require.

My code which returns the locations is as follows. I have tried googling and a few different ways to do it, but feel I am out of my league and thought I would ask the experts here for some help!

Thanks

<?php
global $woo_options;
$post_types_to_loop_through = array();
$posts_to_exclude = array();
$categories_panel_entries = $woo_options['woo_categories_panel_entries'];
$more_listings_setting = $woo_options['woo_more_listings_area'];
$wp_custom_post_types_args = array();
$wp_custom_post_types = get_post_types($wp_custom_post_types_args,'objects');
foreach ($wp_custom_post_types as $post_type_item) {
$cpt_test = get_option('woo_categories_panel_post_types_'.$post_type_item->name);
if ($cpt_test == 'true') {
    $cpt_nice_name = $post_type_item->labels->name;
    $cpt_has_archive = $post_type_item->has_archive;
    $post_types_to_loop_through[$post_type_item->name] = array('nice_name' => $cpt_nice_name, 'has_archive' => $cpt_has_archive);
}
}

$section_counter = 0;

foreach ($post_types_to_loop_through as $post_type_item => $post_type_item_nice_name) {
$taxonomies = array('location');
$block_counter = 0;
$args = array('orderby' => 'name', 'order' => 'ASC');
?>

<div id="location">

    <?php 
    // NEW AND IMPROVED QUERY
    $all_terms = get_terms( $taxonomies, $args );

    $block_counter = 0;
    foreach ( $all_terms as $all_term) {

        $tax_test = get_option('woo_categories_panel_taxonomies_'.$all_term->taxonomy);

        if ( ($tax_test == 'true') && ($block_counter < $categories_panel_entries) ) {

            $post_images = array();
            $posts_aray = array();

            $term_name = $all_term->name;
            $term_slug = $all_term->slug;
            $term_id = $all_term->term_id;
            $term_link = get_term_link( $all_term, $all_term->taxonomy );
            $counter_value = $all_term->count;
            ?>
            <div class="block">
                <a href="<?php echo $term_link; ?>">
                <?php
                    $block_counter++;



                    if ( $post_id > 0 ) {

                        // If a featured image is available, use it in priority over the "image" field.
                        if ( function_exists( 'has_post_thumbnail' ) && current_theme_supports( 'post-thumbnails' ) ) {

                            if ( has_post_thumbnail( $post_id ) ) {

                                $_id = 0;
                                $_id = get_post_thumbnail_id( $post_id );

                                if ( intval( $_id ) ) {

                                    $_image = array();
                                    $_image = wp_get_attachment_image_src( $_id, 'full' );

                                    // $_image should have 3 indexes: url, width and height.
                                    if ( count( $_image ) ) {

                                        $_image_url = $_image[0];

                                        woo_image('src=' . $_image_url . '&key=image&width=139&height=81&link=img');

                                    } // End IF Statement

                                } // End IF Statement

                            } else {

                                woo_image('id='.$post_id.'&key=image&width=139&height=81&link=img');

                            } // End IF Statement

                        } else {

                            woo_image('id='.$post_id.'&key=image&width=139&height=81&link=img');

                        } // End IF Statement

                    } else {
                        // Fallback
                        woo_taxonomy_image($post_images,$term_link);

                    } // End IF Statement
                    $php_formatting = "m\/d\/Y";
                    $post_item_date = strtotime($post_date_raw);
                ?>
                    </a>
                <h2><a href="<?php echo $term_link; ?>"><?php echo $term_name ?> <br/><span>(<?php echo $counter_value; ?> Farmers)</span></a></h2>
                <p><?php _e('Latest listing ', 'woothemes') ?><?php echo date($php_formatting,$post_item_date); ?></p>
            </div><!-- /.block -->
            <?php
            if ( $block_counter % 5 == 0 ) {
            ?>
                <div class="fix"></div>
            <?php
            } // End IF Statement   

        } // End IF Statement

        ?>
        <?php


    } // End For Loop

    ?>

    <div class="fix"></div>

    <?php if ( $block_counter > 0 ) { ?>




    <?php } else { ?>    
    <p class="woo-sc-box note" style="margin:20px 20px;"><?php _e('Please add some posts in order for this panel to work correctly. You must assign the post to these taxonomies for it to work.','woothemes'); ?></p>
    <?php } ?>

</div><!-- /.listings -->

<div class="fix"></div>

Update
Thanks to some of the help I have implemented this code, but still can’t get it to work!

I added this to the global variables on line 8
$i = 0; // initialise incrementing variable

I then added this just before the div as u can see with the extra class I am trying to echo

// I added this line as suggested just before my div
<?php foreach ( $all_terms as $all_term) { ?>

// I then added a new class but I was not sure how to write this
<div class="block" class="<?php echo "post-" . $i; ?>">

Just before the closing div tag (about 100 lines later) I add the closing code

 $i++; // increment it
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:11:56+00:00Added an answer on June 11, 2026 at 3:11 pm

    As @sushanth reddy said, it should really be as simple as

    $i = 0; // initialise incrementing variable
    foreach ( $all_terms as $all_term) {
        // rest of your code; use $i when you want your incrementing ID / class
    
        $i++; // increment it
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some help structuring my php. I am trying to get 3 different
I was wondering if i could get some help. What i am trying to
Just trying to get some rows out of a database and loop through but
Im trying to get some system information using WMI but the problem is when
I'm trying to get some data from a certain web-based api using the libcurl
I'm trying to get some data out my templates to, let's say, add an
I am trying to get a HTML/PHP form to submit properly. Some details: base
I am trying to add some php which show the time left for a
I am trying to get issue details from JIRA 3.13 using PHP SOAP. I
Im trying to get some auto complete to work. I have this URL i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.