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

PHP

php if, elseif code

Hi guys,

Im working on a website with multiple signupforms, depending on the wp-post the user is on, I want to display different signup forms. Im using bootstrap with wordpress and setup the following code in my footer.php:

            <div class="modal fade" id="inschrijfformulier">
          <div class="modal-dialog">
            <div class="modal-content">
              <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h2 style="text-align:center;" class="modal-title">Schrijf u hier in</h2>
              </div>
              <div class="modal-body">

                <?php
                    if ($is_single( 'de-franse-keuken' ) ) {
                      echo do_shortcode("[ninja_forms_display_form id=3]");
                    } 

                    elseif ($is_single( 'de-mediterraanse-keuken' ) ) {
                      echo do_shortcode("[ninja_forms_display_form id=4]");
                    } 

                    else {
                      echo "error!";
                    }
                ?>

              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Sluiten</button>
              </div>
            </div><!-- /.modal-content -->
          </div><!-- /.modal-dialog -->
        </div><!-- /.modal -->

I included only 2 forms so far, but need to work to ten different forms. So the situation is: one modal called "#inschrijfformulier" and the body content is decided by the wp-post the user is currently on! I use this button on each post that requires the form:

            <button class="btn btn-success btn-fullwidth" data-toggle="modal" data-target="#inschrijfformulier">Schrijf U nu in</button>

But it gives me a empty modal window when the button is clicked, what am I missing?

Thanks!

2 Answers

Tyler Armstrong
Tyler Armstrong
13,887 Points

First, try removing the $ from the is_single() function. See if that works.

That did it! thanks for the help Tyler!

Tyler Armstrong
Tyler Armstrong
13,887 Points

I'm guessing the modal is empty because $post is empty at the time it's called.

Try adding the WP_Query object in each page to set your $post to the current page for each post when it's called. Make sure you reset your post data. I hope this helps.

<?php

  // query for the page
  $your_query = new WP_Query( 'pagename=your_page_goes_here' ); 

  // "loop" through query (even though it's just one page) ?>
  <?php while ( $your_query->have_posts() ) : $your_query->the_post(); ?>

     Enter code here

<?php endwhile; 
// reset post data (important!)
wp_reset_postdata(); ?>

Thanks for your help! can you please elaborate a little bit more? Im do not understand where to add this code, should this be placed in the "if" statement for each if?

Thanks!