简体   繁体   中英

WordPress look - Can't get else to work


Have been trying now for some time to solve this but i don't see where the problem is.


This is my code

<?php
//get all post IDs for posts start with letter A, in title order,
//display posts
global $wpdb;
$char_k = 'A';


$postids = $wpdb->get_col($wpdb->prepare("
SELECT      ID
FROM        $wpdb->posts
WHERE       SUBSTR($wpdb->posts.post_title,1,1) = %s
ORDER BY    $wpdb->posts.post_title",$char_a)); 

if ($postids) {
$args=array(
'post__in' => $postids,
'post_type' => 'encyklopedi',
'post_status' => 'publish',
'posts_per_page' => -1,
// 'caller_get_posts'=> 1
);

$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
// echo 'List of Posts Titles beginning with the letter '. $char_a;
while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>



<?php endwhile; }
wp_reset_query(); } ?>



This is what i tried to change in the end of the code

    <?php } endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>



I also tried to change to this

<?php endwhile; } else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>

I keep on getting this error message

Parse error: syntax error, unexpected 'else' (T_ELSE)

How can i solve this ?

You cannot mix if { } else { } blocks and if: else: endif; blocks in PHP. Since the if uses curly braces, your else must also use curly braces. Try this:

<?php endwhile; } else { ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php }
wp_reset_query(); } ?>

It is beacuase you are mixing the two different syntax with each other.

use either:

<?php if (condition): ?>
    <?php else: ?>
<?php endif ?>

or

if (condition) {
    # code...
} else {
    # code...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM