【问题标题】:Wordpress page content and posts in the same pageWordpress 页面内容和帖子在同一页面中
【发布时间】:2019-03-11 11:29:56
【问题描述】:

我是 wordpress 领域的新手,我正在尝试将 HTML 页面调整为 wordpress 主题。 我需要首先在页面上显示页面内容,然后在该页面下显示帖子。但我得到的只是在页面上显示两次的帖子(页面内容应该在哪里)。有没有可能克服这个问题?

还有一个问题,如何根据帖子类别过滤帖子?我试过query_posts('cat=Small'),但它似乎不能正常工作。

index.php 的代码如下:

<?php get_header(); ?>
<?php
    wp_reset_query();
    while ( have_posts() ) : the_post();
        the_content();
    endwhile; 
    wp_reset_query();
?>


<section>
  <header class="major">
    <h2>Erat lacinia</h2>
  </header>
  <div class="features">
    <?php query_posts('cat=Small'); ?>
      <?php if(have_posts()) : while(have_posts()) : the_post(); ?>
        <article>
         <span class="icon fa-diamond"></span>
           <div class="content">
              <h3><?php the_title(); ?></h3>
              <p><?php the_content('Read More'); ?></p>
           </div>
        </article>
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>
<?php get_footer(); ?>

【问题讨论】:

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


    【解决方案1】:

    试试下面的代码。这可能对你有帮助

    <section>
    <div class="major">
    <h2>Erat lacinia</h2>
    </div>
    <div class="features">
    <?php $args = array(
        'posts_per_page'   => -1,
        'offset'           => 0,
        'category'         => '',
        'category_name'    => '',
        'orderby'          => 'date',
        'order'            => 'ASC',
        'include'          => '',
        'exclude'          => '',
        'meta_key'         => '',
        'meta_value'       => '',
        'post_type'        => 'post',
        'post_mime_type'   => '',
        'post_parent'      => '',
        'author'       => '',
        'post_status'      => 'publish',
        'suppress_filters' => true 
        );?>
    <?php query_posts( $args ); ?>
    <?php while ( have_posts() ) : the_post(); ?>
    <article>
         <span class="icon fa-diamond"></span>
           <div class="content">
              <h3><?php the_title(); ?></h3>
              <p><?php the_content('Read More'); ?></p>
           </div>
        </article>
    <?php endwhile; wp_reset_query(); ?> 
    </div>
    </section>
    

    【讨论】:

    • 不幸的是,它不起作用。它只显示没有页面内容的帖子:(
    【解决方案2】:

    你可以使用两个循环。

    在你的php页面模板中,首先执行正则循环来获取实际页面的内容,像这样:

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
       //output page content here
    <?php endif; ?>
    

    然后为所需的帖子定义一个新查询:

    $args = array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => 3,
        'orderby' => 'date',
        'order' => 'ASC',
        )
    );
    //(Add and change arguments as desired in the array above)
    $loop1 = new WP_Query($args);
    
    if ( $loop1->have_posts() ) : while ( $loop1->have_posts() ) : $loop1->the_post(); 
       //Output the post contents in a loop here    
    <?php endif;
    wp_reset_postdata();?>
    

    然后添加页面模板的其余部分(页脚等)

    【讨论】:

      【解决方案3】:
      <?php 
      /*
       *Template name: test
       */
      
       get_header(); 
      if ( have_posts() ) :
          while ( have_posts() ) : the_post();
              $attrs = array(
                'numberposts' => 10,
                'post_type'   => 'post',
                'tax_query' => array(
                      array(
                          'taxonomy' => 'category',
                          'field'    => 'slug',
                          'terms'    => array( 'small' )
                      )
                  )
              );
              $my_posts = get_posts( $attrs );
              the_content();
              ?>
      
      
                        <?php if ($my_posts): ?>
                          <section>
                            <header class="major">
                              <h2>Erat lacinia</h2>
                            </header>
                            <div class="features">
                              <?php foreach ($my_posts as $key => $value): ?>
                                   <article>
                                       <span class="icon fa-diamond"></span>
                                         <div class="content">
                                            <h3><?= $value->post_title; ?></h3>
                                            <p><?= $value->post_content ?></p>
                                         </div>
                                      </article>
                              <?php endforeach ?>
                            </div>
                            </section>
                        <?php endif ?>
              <?php
          endwhile;
      else :
          echo wpautop( 'Sorry, no posts were found' );
      endif;
      
      get_footer(); ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-05-26
        • 2012-08-22
        • 1970-01-01
        • 2018-03-22
        • 2015-11-24
        • 1970-01-01
        相关资源
        最近更新 更多