• Resolved thoughtwell

    (@thoughtwell)


    On one of my custom page templates, I am replacing the default loop with a list of terms from one of my custom taxonomies. While outputting these terms, I want to wrap them in an article element that I can designate a class to by each’s respective id so that I can have the div pull a background image from the css to associate w/ each listing. Basically, I want my html to output all terms associated with my custom taxonomy in the form of article blocks that contain the term name:

    <article class="stage_feature bkgd_(id#)">....</article>
    <article class="stage_feature bkgd_(id#)">....</article>

    …until all taxonomy terms have been listed in the form of articles running one after the other.

    My function is as follows:

    function smt_product_blox() {
    	 $terms = get_terms('smt_equipment_family');
     	if ( !empty( $terms ) && !is_wp_error( $terms ) ){
         foreach ( $terms as $term ) {
           echo "<article class='stage_feature bkgd_". $term_id ."'><h1>". $term->name . "</h1></article>";
         }
     	}
    }

    I cannot figure out how to get the term id integer injected into the class name. This function doesn’t work, as the rendered html just shows a class of “stage_feature bkgd_” for the article element.

Viewing 5 replies - 1 through 5 (of 5 total)
  • You don’t have the variable $term_id defined anywhere in the code that you posted. Did you mean to use $term->term_id instead?

    Thread Starter thoughtwell

    (@thoughtwell)

    Thanks, stephencottontail. That did the trick. Can you tell me what the difference is between these usages? it looks like you are assigning the variable w/ a value, but what is the difference between the way it’s done here and the way it’s done at the beginning of a function w/ the ‘=’ sign? I also sometimes see arrays with ‘=>’ and wonder what the difference is with that usage, as well.

    I’m marking as resolved, and I appreciate your help on this. Thanks again.

    Thread Starter thoughtwell

    (@thoughtwell)

    … forgot to tick the ‘resolved’ box…

    Basically, what’s happening is that $term is an associative array of strings. An associative array is created by using the => operator like this: $foo = array( 'key' => 'value', 'key_two' => 'value_two', etc. ). PHP won’t echo an array of strings, but PHP can echo an individual string from the array, if I use the -> operator to tell PHP which key to use. So to continue with my example, echo $foo->key will give me value, while echo $foo->key_two will give me value_two. You can use var_dump() on an associative array to see what keys you can use.

    To sum up, => creates an associative array, while -> gets individual values from an associative array.

    Thread Starter thoughtwell

    (@thoughtwell)

    Thanks for taking the time to clear that up. This is very helpful as I move forward with other functions.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘get term by ID for css output’ is closed to new replies.