【问题标题】:Wordpress loop displays not only the postsWordpress 循环不仅显示帖子
【发布时间】:2023-03-10 00:23:01
【问题描述】:

我有以下代码:

    <div id="container" class="activiteiten_singleview">

        <div id="content_activiteiten">
            <h1 id="pagetitle"><?php echo get_the_title($ID);?></h1>

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

                    if ( is_page('48') ) {
                        query_posts( 'cat=4' );
                    }
                    if ( is_page('44') ) {
                        query_posts( 'cat=6' );
                    }
                    if ( is_page('46') ) {
                        query_posts( 'cat=9' );
                    }
                    if ( is_page('50') ) {
                        query_posts( 'cat=8' );
                    }
                    if ( is_page('52') ) {
                        query_posts( 'cat=7' );
                    }?>


                    <div class="postwrapper">
                        <h3><?php the_title(); ?></h3>  
                        <p><?php the_excerpt(); ?></p>
                    </div>

                 <?php endwhile; endif;?>


        </div>
    </div><!-- #container -->

问题是不仅帖子是在 postwrapper-div 中创建的,而且页面标题也是空摘录。我只想将所有帖子显示为 postwrapper。

【问题讨论】:

  • 尝试回显 the_excerpt();

标签: wordpress loops post while-loop posts


【解决方案1】:

试试这个:

<?php 

if ( is_page('48') ) {
    query_posts( 'cat=4' );
}
if ( is_page('44') ) {
    query_posts( 'cat=6' );
}
if ( is_page('46') ) {
    query_posts( 'cat=9' );
}
if ( is_page('50') ) {
    query_posts( 'cat=8' );
}
if ( is_page('52') ) {
    query_posts( 'cat=7' );
}

if (have_posts()) : while (have_posts()) : the_post(); ?>

    <div id="postwrapper">
        <h3><?php the_title(); ?></h3>  
        <p><?php the_excerpt(); ?></p>
    </div>

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

【讨论】:

  • 这没有任何改变。带有页面标题但没有内容的 postwrapper 仍然存在。
  • 也许我不清楚你想要实现什么。您想在 一个 postwrapper 中显示所有查询的帖子吗?
  • 我想在不同的 postwrapper 中显示所有查询的帖子,这行得通。但是循环创建的第一个 postwrapper 不是使用帖子中的内容,而是使用 h3 中的 pagetitle 并且 p 为空。
  • 所以我希望只创建帖子。
  • 从您的代码中删除 &lt;h1 id="pagetitle"&gt;&lt;?php echo get_the_title($ID);?&gt;&lt;/h1&gt;
【解决方案2】:

如果您在页面模板中使用它,请尝试添加 query_posts

<div id="container" class="activiteiten_singleview">

        <div id="content_activiteiten">
            <h1 id="pagetitle"><?php echo get_the_title($ID);?></h1>

             <?php query_posts('post_type=post'); if (have_posts()) : ?>
                <?php while (have_posts()) : the_post(); 

                    if ( is_page('48') ) {
                        query_posts( 'cat=4' );
                    }
                    if ( is_page('44') ) {
                        query_posts( 'cat=6' );
                    }
                    if ( is_page('46') ) {
                        query_posts( 'cat=9' );
                    }
                    if ( is_page('50') ) {
                        query_posts( 'cat=8' );
                    }
                    if ( is_page('52') ) {
                        query_posts( 'cat=7' );
                    }?>


                    <div class="postwrapper">
                        <h3><?php the_title(); ?></h3>  
                        <p><?php the_excerpt(); ?></p>
                    </div>

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


        </div>
    </div><!-- #container -->

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-24
    • 2016-08-24
    • 1970-01-01
    • 2021-05-05
    相关资源
    最近更新 更多