这是一个可以接受的wordpress循环吗?


Is this an acceptable wordpress loop?

php和循环过程的新手。循环中有 html 标记可以吗?此外,我经常在其他主题中看到类似<?php get_template_part( 'content', get_post_format() ); ?>或更复杂的版本。我应该将循环放在一个单独的 php 文件中并在我的帖子页面中调用它吗?

我只是想确保我的循环遵循"wordpress规则",并且看起来不正常。这是我当前的循环代码,它确实可以正常运行:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> 
    <h2 id="post-<?php the_ID(); ?>">
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
    <?php the_title(); ?></a></h2>
    <p>
        Published on <?php the_time('M j, Y'); ?> <br>
    </p>
    <p><em>
        by <?php the_author(', '); ?> in <?php the_category(', '); ?> | <?php comments_number(); ?><br>
    </em></p>
    <?php echo get_the_post_thumbnail($page->ID, 'home-thumb'); ?>
    <br>
    <p><?php the_content(); ?></p>
    <hr>
<?php endwhile; ?>
    <div class="navigation">
        <div class="alignleft"><?php previous_posts_link('&laquo; Previous Entries') ?></div>
        <div class="alignright"><?php next_posts_link('Next Entries &raquo;','') ?></div>
    </div>
<?php else: ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>

在循环中使用标记是可以的,事实上大多数主题都是这样完成的。

像这样的函数仅用于可重用的代码。 get_template_part( 'content', get_post_format() );

如果要在其他地方使用该循环,请将其分离到另一个文件中。如果没有,请将其保留在同一文件中。