WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Loop through array (4 posts)

  1. Archie22is
    Member
    Posted 1 year ago #

    Hi guys,

    I have the following code which returns the followig array:

    5 =>
        object(stdClass)[14]
          public 'term_id' => string '62' (length=2)
          public 'name' => string 'IT & Telecommunications' (length=27)
          public 'slug' => string 'it-and-telecommunications' (length=25)
          public 'term_group' => string '0' (length=1)
          public 'term_taxonomy_id' => string '151' (length=3)
          public 'taxonomy' => string 'services' (length=8)
          public 'description' => string '' (length=0)
          public 'parent' => string '0' (length=1)
          public 'count' => string '0' (length=1)

    However whenever I loop through the returned array list using the following code:

    $mycateg = get_terms( 'services', array(
    							'parent' => 0,
    							'oderby' => 'name',
    							'hide_empty' => 0
    							)
    						);
    					echo "test";
    
    					foreach ($mycateg as $categ) {
    						echo $mycateg->name;
    					}

    I get no output at all - only a null. what is the correct way to create output?

  2. vtxyzzy
    Member
    Posted 1 year ago #

    I think you need to change this:

    echo $mycateg->name;

    to this:

    echo $categ->name;
  3. Archie22is
    Member
    Posted 1 year ago #

    Thank you!!

    What a silly mistake... #lack of coffee

  4. Archie22is
    Member
    Posted 1 year ago #

    #Resolved

Topic Closed

This topic has been closed to new replies.

About this Topic