【问题标题】:Code shows nothing on wordpress: while (have_posts()) {the_post();?>代码在 wordpress 上什么也不显示:while (have_posts()) {the_post();?>
【发布时间】:2020-07-28 16:59:21
【问题描述】:

我已将此代码放在 wordpress 上的 index.php 中,但它没有显示任何内容。我认为它应该显示所有最新到最新的帖子。任何人都可以解决这个问题?

        <div class="card">
            <div class="card-image">
                <a href="<?php echo the_permalink(); ?>">
                    <img src="<?php echo get_the_post_thumbnail_url(get_the_ID); ?>" alt="Card Image">
                </a>
            </div>
            
            <div class="card-description">
                <a href="<?php the_permalink(); ?>">
                    <h3><?php the_title(); ?></h3>
                </a>
                <div class="card-meta">
                    Đăng bởi <?php the_author(); ?> vào <?php the_time('j F, Y') ?> trong  <a href="#"><?php echo get_the_category_list(',') ?></a>
                </div>
                <p>
                    <?php echo wp_trim_words(get_the_excerpt(), 30); ?>
                </p>
                <a href="<?php the_permalink(); ?>" class="btn-readmore">Tìm hiểu thêm</a>
            </div>
        </div>
        
        <?php }
            wp_reset_query(); 
        ?>

非常感谢。

【问题讨论】:

  • 您在代码中使用while (have_posts()) {the_post();?&gt; 吗?您的问题标题中有它,但代码本身没有。另外,您确定index.php 是显示您正在尝试的任何页面的模板吗?许多主题都有专门用于页面、帖子、档案等的模板。

标签: php wordpress


【解决方案1】:

你几乎错过了一半的循环......

如果您想使用问题中的帖子模板,则可以执行以下操作:

<?php if ( have_posts() ):
    $i = 0;
    while ( have_posts() ):
        $i++;
        if ( $i > 1 ):
        echo '<hr>';
        endif; ?>

        <div class="card">
            <div class="card-image">
                <a href="<?php echo the_permalink(); ?>">
                    <img src="<?php echo get_the_post_thumbnail_url(get_the_ID); ?>" alt="Card Image">
                </a>
            </div>

            <div class="card-description">
                <a href="<?php the_permalink(); ?>">
                    <h3><?php the_title(); ?></h3>
                </a>
                <div class="card-meta">
                    Đăng bởi <?php the_author(); ?> vào <?php the_time('j F, Y') ?> trong  <a href="#"><?php echo get_the_category_list(',') ?></a>
                </div>
                <p>
                    <?php echo wp_trim_words(get_the_excerpt(), 30); ?>
                </p>
                <a href="<?php the_permalink(); ?>" class="btn-readmore">Tìm hiểu thêm</a>
            </div>
        </div>

<?php
    endwhile;
endif; ?>

应该可以工作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多