0

カスタム投稿タイプ - ポートフォリオ用の WordPress ループを作成しましたが、機能します。ただし、投稿が表示されないときにメッセージを追加する必要がありますが、試した方法でエラーが返されます。

これが私の作業中のWordPressループです:

            $gallery = new WP_Query($args);

        while($gallery->have_posts()): $gallery->the_post();
            if(has_post_thumbnail()):

            $data_types = '';
            $item_cats = get_the_terms($post->ID, 'portfolio_category');
            if($item_cats):
            foreach($item_cats as $item_cat) {
                $data_types .= $item_cat->slug . ' ';
            }
            endif;

            $full_image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'portfolio-full'); ?>

              <li data-id="<?php echo $post->ID;?>" class="portfolio_item" data-type="<?php echo $data_types;?>">
                <a href="<?php the_permalink(); ?>" rel="prettyPhoto" title="">
                    <span class="pic"><?php the_post_thumbnail('portfolio-medium'); ?><div class="img_overlay"></div></span>
                    <h4><?php the_title(); ?></h4>
                </a>
              </li>         

        <?php endif; endwhile; ?>

そして、私は次のようにループを閉じようとしました:

<?php endwhile; ?>
<?php else : ?>
<p>My Text Here</p>
<?php endif; ?>

現在の代わりに:

<?php endif; endwhile; ?>

「構文エラー、予期しない T_ENDWHILE」というエラーが表示されます

ここで重要な何かが欠けていますか?

4

1 に答える 1

0

ループをifステートメントでラップします。これを使用して、投稿があるかどうかを確認できます-次のように:

<?php
$gallery = new WP_Query($args); // initialize query
if( $gallery->have_posts() ): // if there are posts
    while( $gallery->have_posts() ) : $gallery->the_post(); // the loop
    // do stuff with the post here
    endwhile; // end of the loop
else: // if there aren't any posts
    echo "<p>no posts found!</p>";
endif; // end of the if statement
?>
于 2013-02-17T22:59:15.537 に答える