0

I am a beginner and looking for some guidance please.

I have added a function so I can have Categories for my Wordpress Pages

function my_page_category() 
{     
  register_taxonomy_for_object_type('category', 'page');
  add_post_type_support('page', 'category');
}

add_action( 'init', 'my_page_category' );

Now I need to display 3 pages (title and excerpt with read more) for only one category, and I have the below so far, but it is only returning 1 page, when I have 5 pages in that category.

<?php
$splash_page_args = array(
  'post_type' => 'page',
  'posts_per_page'=> '3',
  'taxonomy' => 'category',
  'field' => 'slug',
  'term' => 'splash-homepage'
);

$splash_pages = new WP_Query($splash_page_args);

foreach ($splash_pages as $splash_page){  
?>

<h2><?php echo $splash_page->post_title; ?></h2>
<p><?php echo $splash_page->post_content; ?></p>
<?php
}
?>

I have searched on wordpress and also tried in the array, without success:

'number'=>'3',

Thank you for your help in advance.

0

few things here:

1) Your WP_Query args doesn't look good, the proper way of querying posts by taxonomy should be:

$splash_page_args = array(
    'post_type'      => 'page',
    'posts_per_page' => '3',
    'tax_query' => array(
         array(
             'taxonomy' => 'category',
             'field'    => 'slug',
             'terms'    => 'splash-homepage',
         ),
    ),
);

2) Is there any particular reason why you're using foreach instead of standard WordPress loop? If you want to keep using the foreach loop, then your posts objects are not in $splash_pages variable, look for them in $splash_pages->posts.

However, I'd recommend using WordPress loop like so:

if ( $splash_pages->have_posts() ) :
    while ( $splash_pages->have_posts() ) : $splash_pages->the_post();
    ?>
        <h2><?php the_title(); ?></h2>
        <p><?php the_content(); ?></p>
    <?php
    endwhile;
endif;
wp_reset_postdata();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.