There’s 3 types of functions which use array() out of the dozens of different array functions you can use in PHP.

This tutorial explains the simplest & most common type of array() function which is called a indexed array.

A indexed array uses a variable to hold more than 1 value.

You need to understand what a variable is, to understand what a array is and how to use arrays in WordPress and Genesis.

There’s also 2 ways to code indexed arrays :

  1. You can code a single variable which automatically holds all values.
  2. You can assign the index automatically starting at 0 or manually for each value.

In this 1st example, the variable named $colors stores 3 values which are red, white and blue. The index is automatic so the 1st value red = 0, white = 1 and blue = 2.

Square or Round Brackets

You can code the array using rounded brackets or square brackets.

Using round brackets :

$colors = array( 'red', 'white', 'blue' );

Using square brackets :

$colors = [ 'red', 'white', 'blue' ];

Working Code Snippets

Here’s a fully coded example which you can paste in your child themes functions file to test how a array works in Genesis child themes.

add_action( 'genesis_after_header', 'test_array_1' );

function test_array_1() {

$colors = array( 'red', 'white', 'blue' ); 

echo '<p> I like ' . $colors[2] . ', ' . $colors[0] . ' and ' . $colors[1] . '. </p>';

}

The following example shows how you can manually assign the index number to each value then print each value in any order using the index number for each value.

add_action( 'genesis_after_header', 'test_array_3' );

function test_array_3() {

$colors[0] = 'red';
$colors[1] = 'white';
$colors[2] = 'blue';

echo '<p> My favorite colors are : ' . $colors[2] . ', ' . $colors[0] . ' and ' . $colors[1] . '. </p>';

}

Using Arrays in Genesis

There’s different ways to use arrays in WordPress themes. A very common way is with a comma separated array of post i.d’s within a conditional tag like this :

is_page(array( 1, 2, 3 ) )

Test code :

add_action( 'genesis_after_header', 'test_array_4' );

function test_array_4() {

if ( is_page(array( 1, 2, 3 ) ) ) {

echo "Hello World";

    }
    
}

The above conditional tag, when used within a function, will only execute on a page with the post id of 1, 2 or 3.

Array of key => value pairs

In previous examples we used a indexed array of values without keys however you can also use comma-separated key => value pairs as arguments within the array like this.

array( 'format' => 'html', size  => 'full' );

Example usage of key => value pairs in Genesis

$image = genesis_get_image( array(
    'format'     => 'html',
    'size'	 => 'full',
    'attr'	 => array ( 'alt' => the_title_attribute( 'echo=0' ) ),
) );

Here’s a full working code example which uses the above array of key => value pairs in the genesis_get_image function for the image arguments.

Array In WordPress

Here’s a classic example of an array of arguments in WordPress :

$args = array(
    'orderby' => 'comment_count',
    'posts_per_page' => 3,
    'post__not_in' => $sticky
);

In the above case, the comma-separated key => value arguments are used with WP_Query. The parameter being the key which accepts different values. Here’s the Full working code example.

Join 5000+ Followers

Get The Latest Free & Premium Tutorials Delivered The Second They’re Published.