【问题标题】:Loop inside the Loop Wordpress在循环 Wordpress 中循环
【发布时间】:2018-07-26 09:43:12
【问题描述】:

我在 Wordpress 的主循环之外创建循环时遇到问题。基本上我想为页面上的所有帖子获取the_excerpt()。代码看起来像这样,但它仍然只为第一篇文章生成摘录。

 <?php $i= 0; ?>
        <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
            <div class="content-post-wrapper">
                    <p class="postmetadata"><?php the_category( ', ' ); ?></p>
                    <h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
                    <small><?php the_author_posts_link(); ?> <?php echo meks_time_ago(); ?></small>
                    <?php if ( in_category( '16') ) : ?>
                            <div class="entry-promo">
                                <div class="img-wrapper-large">
                                    <?php the_post_thumbnail( 'large'); ?> 
                                </div>
                                <?php echo awesome_excerpt(wp_trim_excerpt(),1); ?>
                            </div>
                    <?php else : ?>
                        <div class="entry">
                            <div class="img-wrapper-medium">
                                <?php the_post_thumbnail( 'thumbnail'); ?>
                                <?php comments_number(); ?>
                            </div>
                            
                            <?php if ( $i<99 ) : ?>  
                                    <?php the_excerpt(); ?>
                            <?php endif; ?>
                            <?php $i=$i+1; ?>
                            
                            <!--<?php echo awesome_excerpt(wp_trim_excerpt(),1); ?>-->
                        </div>
                    <?php endif; ?>
                </div>
        <?php endwhile; else : ?>
            <p><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></p>
        <?php endif; ?>

我也试过<?php if ( !$post->post_excerpt ) :?> <!-- Display explicit excerpt--> <?php the_excerpt(); ?> <?php endif; ?>,但结果是一样的。 :(

【问题讨论】:

    标签: php wordpress wordpress-theming custom-wordpress-pages


    【解决方案1】:

    如果您还没有打开WordPress debugging,首先要做的就是打开。检查这是否为您提供任何错误。

    另一件事要尝试;现在忽略其余的逻辑,并在循环开始后直接添加摘录代码。这将有助于简化事情并查看返回的内容(如果有的话)。见下文:

    <?php if (have_posts()) : while (have_posts()) : the_post(); 
        the_excerpt();
    endwhile; endif; ?>
    

    让我知道你过得怎么样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-09
      • 1970-01-01
      • 1970-01-01
      • 2021-04-06
      • 2018-07-28
      • 1970-01-01
      相关资源
      最近更新 更多