Содержание страницы Wordpress и посты на одной странице - PullRequest
0 голосов
/ 11 марта 2019

Я довольно новичок в мире 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(); ?>

Ответы [ 3 ]

0 голосов
/ 11 марта 2019

Вы можете использовать два цикла.

В вашем шаблоне страницы 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();?>

А затем добавьте оставшуюся часть шаблона страницы (нижний колонтитул и т. д.)

0 голосов
/ 11 марта 2019
<?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(); ?>
0 голосов
/ 11 марта 2019

Попробуйте приведенный ниже код.Это может помочь вам

<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>
...