I am having trouble figuring out what is wrong with this code [closed]
<?php
if ( is_home() ) {
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();?>
<div id="post">
<?php the_content(); ?>
</div>
<?php endwhile;?>
<?php endif; ?>
} else if (is_page() ) {
$category = get_post_meta($posts[0]->ID, 'category', true);
}
if ($category) {
$cat = get_cat_ID($category);
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$post_per_page = 4; // -1 shows all posts
$do_not_show_stickies = 1; // 0 to show stickies
$args=array(
'category__in' => array($cat),
'orderby' => 'date',
'order' => 'DESC',
'paged' => $paged,
'posts_per_page' => $post_per_page,
'caller_get_posts' => $do_not_show_stickies
);
$temp = $wp_query; // assign orginal query to temp开发者_StackOverflow社区 variable for later use
$wp_query = null;
$wp_query = new WP_Query($args);
if( have_posts() ) :
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<div id="post">
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php endif;
$wp_query = $temp;
}
?>
On line #4 you are opening a new PHP block even though the old one hasn't been closed yet.
You'll want to delete the preceding <?php
on that line. You don't need it.
Additionally:
You have an extra endwhile;
in your else
block. The while loop is started and closed within the if ( is_home() ) {
block.
The code starting with } else if (is_page() ) {
is not in a PHP block. You should either open a <?php
block or not close the preceding one.
The first php tag is unclosed. Close that and move on from there. It would be easier to read and debug if you thought of it as primarily being a PHP file rather than an HTML file. That is, keep a php tag open until straight HTML is required. At that point, close the PHP tag, write your HTML, and reopen a PHP tag.
At the point you do this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();?>
you're still in "php mode" from the first <?php
at the top of the script, so this line is a syntax error. You cannot embed one <?php ?>
block inside another.
Remove the <?php
before the if
, or add a ?>
after the is_home()
stuff on earlier line:
<?php if (is_home()) { ?>
<?php if ( ...) ?>
or
<?php
if (is_home()) {
if (...) { ?>
As well, I'd STRONGLY suggest enabling display_errors
and error_reporting
in your PHP configuration. If they were on, you'd have see the syntax error caused by the extra <?php
:
PHP Parse error: syntax error, unexpected '<' in somefile.php on line XXX
精彩评论