Answer the question
In order to leave comments, you need to log in
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; ?>
<?php if (have_posts()) : while (have_posts()) : the_post();?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
Answer the question
In order to leave comments, you need to log in
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();
?>
I don’t use this way to display posts, but maybe wp_reset_postdata() after displaying category posts will help?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question