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 trialGreg Schudel
4,090 Pointsdon't understand line by line WP Includes file snippet
I am familiar with JS and PHP. But this top part of the include file doesn't make sense to me. Lemme specify what I mean and then use comments to where I'm foggy. I see what it does in the video, I just don't follow the syntax completely nor the order of the code:
code placed on top of content-portfolio.php
<?php
$ num_posts = ( is_front_page() ) ? 4 : -1;
// is this section asking "is there a front_page? If there is, put 4 images or 1?"
// Does the question mark '?' mean "we are asking something going forward"
// Does the ':' (colon) mean 'or'?
$ args = array (
'post_type' => 'portfolio', // why is this even here?
'posts_per_page' => $num_posts; // this looks/reads and executes the num_posts automatically through the tertiary loop above right?
);
$query = new WP_Query ($args);
// why isn't this declared at the top of the PHP code block? Does it matter where it's here
// or at the bottom?
?>
1 Answer
Joel Bardsley
31,249 PointsGoing through the code step-by-step:
$num_posts = ( is_front_page() ) ? 4 : -1;
This is a ternary operator. All this does is set a value of $num_posts depending on whether the condition inside the parentheses evaluates to true or false. If it's true, the code after the question mark will execute, which sets the value of $num_posts to 4. Otherwise it'll skip to the code after the colon, which sets $num_posts to -1.
The is_front_page() function does what it says - returns true or false whether or not you're on your site's front page (either the main blog page or a static page, depending on your theme settings.)
More information on is_front_page()
$args = array (
'post_type' => 'portfolio', // why is this even here?
'posts_per_page' => $num_posts; // this looks/reads and executes the num_posts automatically through the tertiary loop above right?
);
This assigns the arguments for your custom WordPress query. Assigning 'post_type' to 'portfolio' allows the query to make use of the 'portfolio' custom post type that's been set up. If this isn't here, it'll only attempt retrieve posts that are the default post type.
'posts_per_page' => $num_posts takes the value of $num_posts that was defined above and assigns it to the 'posts_per_page' argument. Without this argument, the query would default to 10 posts per page.
More information on arguments/parameters you can use
$query = new WP_Query ($args);
Now that the arguments have been defined, the query can be created. If this was at the top, it wouldn't know what $args would be referring to and would lead to unexpected and undesired results.
Hope that helps. Let me know if anything needs further clarification.
David Evans
10,490 PointsDavid Evans
10,490 PointsWordpress has some really good documentation which is available here: https://codex.wordpress.org/
Most of your comments can probably be answered by reading through there.