Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress WordPress Theme Development Custom Post Type Templates in WordPress The Portfolio Homepage

$args = array( 'post_type' => 'portfolio' );

Can someone please explain to me what does this code means? How does post_type' => 'portfolio' work? and where does it pulling from? I know that it pulling from portfolio page but why does "portfolio" are in lowercase instead of uppercase?

2 Answers

$args = array( 'post_type' => 'portfolio' );

This is a php array. An array allows you to find data based on a key. You can define arrays in 2 different ways.

$args = array("value1","value2","value3","value4","value5","value6","value7");
$argsTwo = array( 'post_type' => 'portfolio', 'post_id' => 1, 'post_title' => "My test post" );

$args is a standard array, where values are separated by a comma delimited list. The key is based on it's position in the list starting from 0.

$args[0] would give you the value of "value1" $args[5] would give you the value of "value6"

You can set new values by doing. $args[6] = "My great new value"; // Would set the value of $args[6] to "My great new value" $args[] = "New value"; // adds a new key/value to the end of the array

$argsTwo is a dictionary array. It too is based on a comma delimited list but each delimited section is 2 part. a key and a value. the => delineates the key from the value. "KEY" => "VALUE"

Therefore...

$argsTwo['post_type'] has a value of "portfolio" $argsTwo['post_title'] has a value of "My test portfolio"

Similar to the first array you can set a value by doing $argsTwo['post_title'] = "My new test portfolio title";

And you can add a new key value by doing $argsTwo['post_content'] = "My happy new post content"

Does that make sense?

To go further, he was additionally asking where "portfolio" comes from;
If I understand correctly how this is working....remember when we were first creating the custom post type (via the CPT plugin) how we had to complete the slug / singular label / plural label? well "portfolio" is the slug we entered. Arrays it seems are how WP keeps tracks of page/post templates; so in our case, it saved the fact that we created a custom post type template w/ the slug portfolio in an array where the key is "post_type" and the value is "portfolio".
Someone please correct me if I'm wrong.

Hi Nijad Kifayeh, you are correct. I've been doing some researching online and found the same answer as you were given in previous comment. Thanks both. (Y)