Working with WordPress Arrays

Working with WordPress Arrays

Posted by Rainey

WordPress has many different powerful functions that will give us data in Arrays.  Learning to work with these can give us power when designing and developing new themes and even plugins.  This post will explore the anatomy of WordPress Arrays and offer tips on how to use them effectively.

First we need to grab an array to work with.  For this example I am going to use a taxonomy for a custom post type.

<?php
$taxonomy = 'chapters';
$tax_terms = get_terms($taxonomy);
?>

Now we have loaded all data for the chapters taxonomy loaded into our array. You can do this to see what it looks like:

print_r($tax_terms);

This should output something like this:

Array (

[0] => stdClass Object (

[term_id] => 5
[name] => Chapter 1
[slug] => chapter-1
[term_group] => 0
[term_taxonomy_id] => 5
[taxonomy] => chapters
[description] =>
[parent] => 0
[count] => 8

)

[1] => stdClass Object (

[term_id] => 6
[name] => Chapter 2
[slug] => chapter-2
[term_group] => 0
[term_taxonomy_id] => 6
[taxonomy] => chapters
[description] =>
[parent] => 0
[count] => 6

)

[2] => stdClass Object (

[term_id] => 7
[name] => Chapter 3
[slug] => chapter-3
[term_group] => 0
[term_taxonomy_id] => 7
[taxonomy] => chapters
[description] =>
[parent] => 0
[count] => 7

)

)

Note: each array object contains a key, in []s, and a value. This is handy for so many reasons.  Maybe we need to create a link to the first or last chapter. Or, maybe we want to display a list of the chapters. Navigating between the objects in the array can be done like this:

// ***********
// by default we are on the first object so current will work
print_r(current($tax_terms));

// we can also do
print_r($tax_terms[0]);

// get the next one
print_r(next($tax_terms));
reset($tax_terms); // make sure you reset your position back to the beginning when done

// get the last object
print_r(end($tax_terms));

// instead of using print_r() we can set them to variables
$LastChapter = end($tax_terms);

// now we have an the last chapter array and can grab data from it. Remember the key names from above?

echo $LastChapter->name

// or create a link to it that tells us how many pages are in the chapter
echo '<a href="' . get_term_link( $LastChapter ) . '">' . $LastChapter->name . ' (' . $LastChapter->count . ' pages)</a>';

// ****************

 

Bringing it all together we can create a linked list of all chapters.

<ul>
	<?php
	foreach ($tax_terms as $tax_term) {
		echo '<li>' . '<a href="' . get_term_link($tax_term, $taxonomy) . '>' . $tax_term->name.' (' . $tax_term->count . ' pages)</a></li>';
	}
	?>
</ul>

I hope this is helpful for you if you spend time working in WordPress regularly. Please share any tips you may have about this, or something I can do better.

Like what you are reading? Subscribe to receive monthly tips & updates!

A Monthly Roundup of Articles and a Sweet Deal

Get the latest tips, tricks, tutorials and freebies in your inbox once a monthly.

Join our growing community of front-end web developers and designers. We are constantly publishing new industry tips, tricks and tutorials to help you take your game to the next level. We are also going to include a freebie or deal on something cool each month. Enter your email below and you will only receive this email once a month, nothing else.

Subscribe to our mailing list

* indicates required


    Today in Web Design - PHP, Tutorials, WordPress