dph19153 2015-09-16 02:44
浏览 35

WP静态页面上的get_posts

This is my first time asking a question on this forum. I hope i can be specific enough. I am trying to replace the front page posts section of a WP template. I have the code below running fine on my index.php page (it gets the posts) when the WP theme is set to settings>reading>your latest posts BUT the way the WP theme "the thinker" is setup reading is set to static page and the posts are gotten through a blog template page (in same location as the index.php file) which gets the posts from loops in separately included files. I'd like to keep it set to static for a few reasons. My question is "Is there a reason why the code below would only work in an index.php file and not out of a blog-template file which is is in the same location as the index.php file. I've checked and the the template-parts called in the code are being called. It seems like there's just no posts to get (which there are).

Thank you for your time,

Dave

<!-- blog content -->
    <div class="container">
        <div class="row" id="primary">
            <main id="content" class="col-sm-8" role="main">

                <?php if ( have_posts() ) : ?>


                <?php /* Start the Loop */ ?>
                <?php while ( have_posts() ) : the_post(); ?>

                    <?php

                        get_template_part( 'template-parts/content', get_post_format() );
                    ?>

                <?php endwhile; ?>

                <?php the_posts_navigation(); ?> 

                <?php else : ?>

                    <?php get_template_part( 'template-parts/content', 'none' ); ?>

                <?php endif; ?>

        </main><!-- content -->


        <!-- sidebar -->

        <aside class="col-sm-4">
            <?php get_sidebar(); ?>
        </aside>

    </div><!-- primary -->
</div><!-- container -->
  • 写回答

1条回答 默认 最新

  • dongzong3053 2015-09-16 02:51
    关注

    have_posts() functions only allowed you to check whether the page have the post or not and as it is your static pages, there will be no posts assign to this page.

    You need to query first at the start of the page to show posts. Here is the example.

    EDITED::

    $args = array(
        'post_type' => 'post',
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'movie_genre',
                'field'    => 'slug',
                'terms'    => array( 'action', 'comedy' ),
            ),
            array(
                'taxonomy' => 'actor',
                'field'    => 'term_id',
                'terms'    => array( 103, 115, 206 ),
                'operator' => 'NOT IN',
            ),
        ),
    );
    $query = new WP_Query( $args );
    
    if ( $the_query->have_posts() ) : ?>
    
        <!-- pagination here -->
    
        <!-- the loop -->
        <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
            <h2><?php the_title(); ?></h2>
        <?php endwhile; ?>
        <!-- end of the loop -->
    
        <!-- pagination here -->
    
        <?php wp_reset_postdata(); ?>
    
    <?php else : ?>
        <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    <?php endif; ?>
    

    You can find more about WP_Query in below link.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。