Home Forums General Issues Display a list of pages with post_object.


Display a list of pages with post_object.

  • Hello, I need some help to solve this issue.

    I need to display a list of posts from a specific category and below I need to display a list of selected pages, in basic HTML it would be something like this:

    <li><a href="post-url">Post 1 Title</a></li>
    <li><a href="post-url">Post 2 Title</a></li>
    <li><a href="post-url">Post 2 Title</a></li>
    <li><a href="page-url">Page 1 Title</a></li>
    <li><a href="page-url">Page 2 Title</a></li>

    The first part works just fine, I’m using the taxonomy field. The second part is what I can’t resolve. I’m using the post_object field to show the pages but I couldn’t create the right loop, this is what I have:

    <div class="travel-featured">
    <h3><?php echo get_field('featured_posts_title'); ?></h3>
    <!-- Display list of posts -->
    <?php $postcat = get_field('featured_posts');
    $catquery = new WP_Query( 'cat=' . $postcat . '&posts_per_page=-1' );
     if ($postcat) : ?>
         <?php while($catquery->have_posts()) : $catquery->the_post(); ?>
         <li><h6><span class="dashicons dashicons-minus"></span> <a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h6></li>
         <?php endwhile; ?> 
    <?php endif; 
    wp_reset_postdata(); ?>
    <!-- Display list of pages -->		
    <?php $post_objects = get_field('featured_pages');
    if ($post_objects) : ?>
       <?php foreach( $post_objects as $post_object): ?>
       <li><h6><span class="dashicons dashicons-marker"></span> <a href="<?php echo get_permalink($post_object->ID); ?>"><?php echo get_the_title($post_object->ID); ?></a></h6></li>
      <?php endforeach; ?>
    <?php endif; ?>

    This returns the current page URL and title for each selected page.

    You can see the page here:

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Display a list of pages with post_object.’ is closed to new replies.