0

I'm have the 2 seperate block of codes as shown below and the one below doesn't work except if I cut off the first one. Any ideas what the problem could be?

Conditinal script

<?php { ?>

    <?php

        if ( is_front_page() ) {

        get_template_part('home-page-content');

                    } elseif ( is_page('archives')) {


           get_template_part('archives');

    }
    else

        get_template_part('secondary-page-content');


     ?> 
        <?php } ?>

Second block of script

 <div id="footer">
     <?php echo get_post_meta($post->ID, "biz_code", true); ?> 
        </div>
3
  • 3
    We need more information to assist. What is the problem exactly? You mention "doesn't work" which can be interpreted many ways. Commented Jul 10, 2014 at 19:56
  • Can you indent your code correctly? It's difficult to read Commented Jul 10, 2014 at 20:31
  • Also can you enable error logging and tell us what the error message PHP gave you was? Commented Jul 10, 2014 at 20:41

2 Answers 2

3

You have a lot of PHP tag spam, e.g.:

?><?php

This is bad, confusing, makes your code difficult to read, and a waste of your time to type. It's the difficulty reading your code that has lead to your problem, including the lack of indentation. These are important, and any good editor will do them for you effortlessly.

If we remove the PHP tag spam, and indent correctly, we get this:

<?php
{
    if ( is_front_page() ) {
        get_template_part('home-page-content');
    } else if ( is_page('archives') ) {
        get_template_part('archives');
    } else
        get_template_part('secondary-page-content');
}
?>

Firstly, there is a set of braces surrounding the whole thing, serving no purpose ( except maybe to confuse ). Remove these, giving us:

<?php
if ( is_front_page() ) {
    get_template_part('home-page-content');
} else if ( is_page('archives') ) {
    get_template_part('archives');
} else
    get_template_part('secondary-page-content');
?>

Finally, we have coding standards, so lets put braces around the final else statement:

<?php
if ( is_front_page() ) {
    get_template_part('home-page-content');
} else if ( is_page('archives') ) {
    get_template_part('archives');
} else {
    get_template_part('secondary-page-content');
}
?>

We can go further, those are a lot of calls to get_template_part, what if we only called that function once?

<?php
$template = 'secondary-page-content';
if ( is_front_page() ) {
    $template = 'home-page-content';
} else if ( is_page('archives') ) {
    $template = 'archives';
}
get_template_part( $template );
?>

However, all of this implies that you've overloaded one template with too much responsibility. I suggest you look at the template hierarchy, that way we can use the frontpage.php template to remove half of that if statement.

2
  • Nowell I've amended the script with your corections but the problem I'm facing is different. The uppr block of code works but the one below doesn't work untill I cut off the upper one. Commented Jul 10, 2014 at 20:56
  • Then you haven't given us enough information, please activate error logging and tell us what the PHP error message is, also what file is this in? What's inside those template files you're including? Commented Jul 11, 2014 at 10:52
0

Looks like a PHP error to me with random braces:

<?php { ?>
<?php } ?>

This should be:

<?php
..code here..
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.