Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

I 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
Gareth Borcherds
9,372 Points

where 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.

Thank 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:

<?php
/*
Template Name: 4columns
*/
?>
<?php global $template_loaded;?>
<?php if($template_loaded == false):?>
<?php get_header(); ?>
<?php endif;?>
<!-- 4 columns begin -->
<div class="grid-centered cf">

    <?php echo get_divider_meta($post->ID); ?>

    <?php
    $page4columns = get_pages(array("child_of" => get_the_ID(), "sort_column" => "menu_order"));
    foreach($page4columns as $post) : setup_postdata($post);
    ?><div class="one-fourth">
        <a id="<?php echo page_section_id(); ?>" class="scroll-anchor-inside"></a>

        <?php echo get_the_post_thumbnail_img($post->ID, '4columns_highres'); ?>

        <?php the_content(); ?>

        <?php all_btns($post->ID); ?>

    </div><?php endforeach; ?>

</div>
<!-- 4 columns end -->
<?php if($template_loaded == false):?>
<?php get_footer(); ?>
<?php endif;?>

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
Gareth Borcherds
9,372 Points

So, 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.

<?php
/*
Template Name: 4columns
*/
?>
<?php global $template_loaded;?>
<?php if($template_loaded == false):?>
<?php get_header(); ?>
<?php endif;?>
<!-- 4 columns begin -->
<div class="grid-centered cf">

    <?php echo get_divider_meta($post->ID); ?> //This function call is not inside any kind of loop (while, foreach, for) so it gets run once and that it.

    <?php
    $page4columns = get_pages(array("child_of" => get_the_ID(), "sort_column" => "menu_order"));
    foreach($page4columns as $post) :  //this is where you start your loop, placing something inside of this will get run more than once
setup_postdata($post);
    ?><div class="one-fourth">
        <a id="<?php echo page_section_id(); ?>" class="scroll-anchor-inside"></a>

        <?php echo get_the_post_thumbnail_img($post->ID, '4columns_highres'); ?>

        <?php the_content(); ?>

        <?php all_btns($post->ID); ?>

    </div><?php endforeach; //this is the end of the foreach so anything after this will only get run once ?>

</div>
<!-- 4 columns end -->
<?php if($template_loaded == false):?>
<?php get_footer(); ?>
<?php endif;?>

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.