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 trialPreben Frenning
Courses Plus Student 2,337 PointsI need to make one snippet of code trig once, then another to trig every time other than the first. H1 in a divider.
So I'm working on a site for a client using a custom Wordpress theme. There are several dividerlines on every page, and the first one is supposted to be a h1 for SEO purposes, while the rest should be h4's.
How can I do this with php?
I have tried:
$first = 1; if ($first == 1) { run code snippet with h1; $first++; } else { run code snippet with h4; }
All of this happens inside a function. Can that be why it doesn't work?
Here is the entire code block:
<?php
function get_divider_meta($postId, $add_space=false) {
$divider_meta = get_post_meta($postId, "metabox_divider", true);
if($divider_meta == ""){
if($add_space){
return "<span class=\"divider-invisible\"></span>";
}
return "";
}
if ( $first )
{
return "<h1 class=\"divider\"><span>" . $divider_meta . "</span></h1>";
$first = false;
}
else
{
return "<h4 class=\"divider\"><span>" . $divider_meta . "</span></h4>";
}
}
?>
1 Answer
Gareth Borcherds
9,372 Pointswhere is your function getting called? There is no loop in your function, so I'd assume that your function is getting called inside some other loop? Given that, the reason it's not working is because your variables inside your function reset to default each time it's called. So it's not persisting. You'd probably need to update the function to add another variable that you pass to it to tell it which type to return, but it's not going to work no matter what you do given the way you've built it. If you post how the function is getting called I can figure out the rest with you.
Preben Frenning
Courses Plus Student 2,337 PointsPreben Frenning
Courses Plus Student 2,337 PointsThank you for your answer!
Silly me. This is a function inside functions.php. It gets called from within all of the content templates.
Here is an example of a complete template it get's called in:
Your answer makes sense to me, but I'm still pretty clueless about how to fix it. I'm still quite a rookie in php.
Gareth Borcherds
9,372 PointsGareth Borcherds
9,372 PointsSo, I'm not sure where you are calling the get_divider_meta more than once in this code, that's your first problem. See my comments inline below.
Without seeing what you're trying to accomplish from a layout perspective, I can't recommend the right place to call these functions, but at this point your code will only provide one divider.