最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Fetch posts from category in custom page template

programmeradmin3浏览0评论

I have created custom template (Sell items) for pages. I want to display posts from specific category on page using that custom template. That is working perfectly but only if there are at least one post in the category. If there is nothing it will give following error.

What might be wrong?

Notice: Undefined offset: 0 in /home/usernamethis/public_html/5423565/wp-includes/class-wp-query.php on line 3152

Here is the code. Html ripped out for better reading:

<?php
/*
Template Name: Sell items
Template Post Type: post
*/
get_header(); 
global $post;
?>

<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post(); ?>

<!-- THIS IS WHERE PAGE CONTENT IS DISPLAYED -->
<?php the_content(); ?>
<!-- THIS IS WHERE PAGE CONTENT IS DISPLAYED -->

<?php
$args = array( 'numberposts' => 20, 'category_name' => 'sell-items' );                      
$posts = get_posts( $args );                        
?>

<?php if(!empty($posts) && count($posts)>0) : ?>

<?php
foreach( $posts as $post ): 
setup_postdata($post);?> 
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php 
endforeach; 
wp_reset_postdata();
?>
<?php endif; ?>
<?php endwhile; ?>

<?php else: ?>

Not found
<?php endif; ?>

I have created custom template (Sell items) for pages. I want to display posts from specific category on page using that custom template. That is working perfectly but only if there are at least one post in the category. If there is nothing it will give following error.

What might be wrong?

Notice: Undefined offset: 0 in /home/usernamethis/public_html/5423565/wp-includes/class-wp-query.php on line 3152

Here is the code. Html ripped out for better reading:

<?php
/*
Template Name: Sell items
Template Post Type: post
*/
get_header(); 
global $post;
?>

<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post(); ?>

<!-- THIS IS WHERE PAGE CONTENT IS DISPLAYED -->
<?php the_content(); ?>
<!-- THIS IS WHERE PAGE CONTENT IS DISPLAYED -->

<?php
$args = array( 'numberposts' => 20, 'category_name' => 'sell-items' );                      
$posts = get_posts( $args );                        
?>

<?php if(!empty($posts) && count($posts)>0) : ?>

<?php
foreach( $posts as $post ): 
setup_postdata($post);?> 
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php 
endforeach; 
wp_reset_postdata();
?>
<?php endif; ?>
<?php endwhile; ?>

<?php else: ?>

Not found
<?php endif; ?>
Share Improve this question asked Apr 13, 2017 at 15:24 Roger WayneRoger Wayne 2236 silver badges13 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

You can use WP_Query instead the get_posts() function:

  $args = array( 'numberposts' => 20, 'category_name' => 'sell-items' );  
  $wp_query = new WP_Query($args);

if ($wp_query->have_posts()) : while($wp_query->have_posts()) : $wp_query->the_post();

echo '<li><a href="'.get_the_permalink().'">'.get_the_title().'</a></li>';
    endwhile;
  endif;

It looks like your endif and endwhile are misplaced. Try this:

<?php
/*
Template Name: Sell items
Template Post Type: post
*/
get_header(); 
global $post;
?>

<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post(); ?>

<!-- THIS IS WHERE PAGE CONTENT IS DISPLAYED -->
<?php the_content(); ?>
<!-- THIS IS WHERE PAGE CONTENT IS DISPLAYED -->
<?php endwhile; ?>
<?php endif; ?>

<?php
$args = array( 'numberposts' => 20, 'category_name' => 'sell-items' );                      
$posts = get_posts( $args );                        
?>

<?php if(!empty($posts) && count($posts)>0) : ?>

<?php
foreach( $posts as $post ): 
setup_postdata($post);?> 
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php 
endforeach; 
wp_reset_postdata();
?>

<?php else: ?>

Not found
<?php endif; ?>
发布评论

评论列表(0)

  1. 暂无评论