V
V
Valera Udav2013-12-20 19:55:58
CMS
Valera Udav, 2013-12-20 19:55:58

How to display both posts from a certain category and the text of the page itself in a WP template on one page?

I'm making a template in WP. It is required on one page to display both records from a certain heading and the text of the page itself. I form queries as follows:
Output of records from the rubric

<?php $posts = get_posts ("category=3&orderby=date&numberposts=3"); ?> 
<?php if ($posts) : ?>
<?php foreach ($posts as $post) : setup_postdata ($post); ?>
  <a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
  <?php the_excerpt(); ?>
  <?php the_time('d.m.y') ?>
<?php endforeach; ?>
<?php endif; ?>

Page text output
<?php if (have_posts()) : while (have_posts()) : the_post();?>
  <?php the_content(); ?>
<?php endwhile; endif; ?>

What I get at the output: entries from the rubric are displayed as they should. Page text displays the text of the last entry in the category, not the page text. If you remove the code for displaying posts from the heading, then the text of the page is displayed as it should. How to treat?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Zelenin, 2013-12-20
@TARAKANhoy

get_posts is an add-on to WP_QUERY, so always use a loop:

<?php
  $custom_query = 'posts_query';
  $args = array(
    'post_type' => 'post',
    'posts_per_page' => -1,
  );
  ${$custom_query} = new WP_Query( $args );
  while ( ${$custom_query}->have_posts() ) : ${$custom_query}->the_post();
?>
    
<?php
  endwhile;
  wp_reset_postdata();
?>

V
Victor, 2013-12-20
@v_decadence

I don’t use this way to display posts, but maybe wp_reset_postdata() after displaying category posts will help?

J
Jonh Doe, 2014-02-16
@CodeByZen

get the text first, then the notes. All because of what you have in $posts

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question