Site logo

Archived topic

How to list explicit posts on explicit pages ?

33 replies · Started by Tim on September 29, 2021

Viewing posts 1–15 of 34

Hello

I want a user to be able to either:

1. Pick a page to list the current post being edited.

or

2. Pick one or multiple posts to list on the current page being edited.

Result is an explicit page listing 1 or multiple explicit posts.

The case secario is that multilpe "client" pages may list multiple "project" posts that are unrelated by category. My first thought was that a tag representing the "client" could be added to the post, but perhaps this is not as user friendly as a dropdown list of pages to choose from.

Is there a standard WP or GP way of doing this, or do I need to use another method ?

Thanks

Hi there,

to display a list of posts on a static page or post requires either using the built in Latest Posts Block that has limited query options or better still the WP Show Posts plugin that will allow the client to construct their own list of posts, and add them to the post or page using a shortcode.

In the WP Show Posts plugin you can select explicit posts using their IDs or they could be set to display a number of posts from a specific category/tag. The latter may be the better option ie. simply create the lists by categorty/tag term and provide them the list of shortcodes they can choose from.

Hello David

Thank you for the support.

If I'm not mistaken that would require the user to:

1. Create a new WP Show Posts list for each new Client page they create.
2. Paste WP Show Posts list shorcode into the new Client page.
3. Create a new tag that is referenced in the new WP Show Posts list.
4. Append the new tag to each required post.

Each of those steps requires several actions.

That sounds a bit too complex for the user.

Perhaps there is another solution ?

Thats correct.
There may be alternative plugins for adding post lists to the page, although i have never come across one that allows you to select the explicit posts you want displayed without manually entering a list of IDs.

Generally most plugins are going to work using standard queries such as specific or related Taxonomy Terms.
Which is something that can be done with WP Show Posts.

For example using a single post list, you can any of the shortcode variations Elvin provides here to use the same list for multiple cases:

https://wpshowposts.com/support/topic/shortcode-parameters-documentation/#post-29167

Yeah i totally hear you on that. A custom meta box ( which can also be done with ACF if you want to reduce some of the custom work https://stackoverflow.com/a/23343728 ) to provide the UI and a method of storing the post IDs could then be used to generate a Hooked in WPSP list

Hello David

Thank you for the continued support.

I already had an ACF field group to display fields on the Client pages, so I just added another field to that group as described:

Field Label: Posts
Field Name: posts
Field Type: Post Object
Filter by Post Type: Post
Filter by Taxonomy: Experience
Select multiple values ?: Yes
Return format: Post ID

The option to pick multiple posts is now enabled and functioning in the Client admin pages as expected.

I already have a Hook to display previously created fields on the front end for these pages, so I assumed I would be able to add some code to the existing hook to get the posts and display their title, image and description.

In the past I had been able to display content from the hook like this:

<div class="entry-content" itemprop="text">
  <div class="client-header">
   <figure class="client-header-media">
    <?php
     $logo = get_field('logo');
     $size = 'full'; // (thumbnail, medium, large, full or custom size)
     if( $logo ) {
      echo wp_get_attachment_image( $logo, $size );
      }
    ?>
   </figure>
  </div>

  <?php if( get_field('description') ): ?>
   <?php the_field('description'); ?>
  <?php endif; ?>
	
  <?php if( get_field('website') ): ?>
   <p>
    <i class="fas fa-external-link-alt"></i>
    <a href="<?php the_field('website'); ?>"> <?php the_field('website'); ?></a>
   </p>
  <?php endif; ?>
	
</div>

But I'm not sure how to get at the selected posts from the new ACF field. I see in the cited link the example does this:

global $post; // Get the global post object

$sidebar_posts = get_field('posts', $post->ID); // Get the field using the post ID

foreach($sidebar_posts as $sidebar_post){ // Loop through posts
    echo $sidebar_post->post_title; // Echo the post title
}

But I'm not having any luck so far with modifying this and adding it to my Hook. However, I'm confident that this method, if written correctly, would be the optimum method.

Also, I don't see how employing a WPSP list would work in this situation because I don't see how the Post IDs would be passed to WPSP.

Can you shed further light ?

From a pure ACF perspective, maybe this doc will help:

https://www.advancedcustomfields.com/resources/post-object/

It seems to do what you're looking for - particularly the sections titled: Display list of posts

It may be simpler just to develop the template with ACF and forgo WP Show Posts.

Hello David

Thank you for confirmation, ACF does seem to be the simpler method. Sorry I didn't search and read that documentation you cited, it's a good example.

Following their example, Display list of posts (with setup_postdata), I changed my Field Name to featured_posts and its Return Format to Post Object.

Put the example into the Hook element:

<?php
$featured_posts = get_field('featured_posts');
if( $featured_posts ): ?>
    <ul>
    <?php foreach( $featured_posts as $post ): 

        // Setup this post for WP functions (variable must be named $post).
        setup_postdata($post); ?>
        <li>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        </li>
    <?php endforeach; ?>
    </ul>
    <?php 
    // Reset the global post object so that the rest of the page works correctly.
    wp_reset_postdata(); ?>
<?php endif; ?>

A list of links are displayed, but only to the current page rather than the intended array of selected posts.

Have I missed something blatantly obvious ?

Yeah because you coudl is just output the_title() and the_permalink() without any specific args so it will just grab the current object ID for displaying them aka. the current post.

If you check the example ACF:

<?php
$featured_posts = get_field('featured_posts');
if( $featured_posts ): ?>
    <ul>
    <?php foreach( $featured_posts as $featured_post ): 
        $permalink = get_permalink( $featured_post->ID );
        $title = get_the_title( $featured_post->ID );
        $custom_field = get_field( 'field_name', $featured_post->ID );
        ?>
        <li>
            <a href="<?php echo esc_url( $permalink ); ?>"><?php echo esc_html( $title ); ?></a>
            <span>A custom field from this post: <?php echo esc_html( $custom_field ); ?></span>
        </li>
    <?php endforeach; ?>
    </ul>
<?php endif; ?>

its including the unique IDS ie.

$permalink = get_permalink( $featured_post->ID );
$title = get_the_title( $featured_post->ID );
$custom_field = get_field( 'field_name', $featured_post->ID );

OK I guess the other example was never meant to explain how to display the multiple posts then, odd.

Right, so I am now using the other method and that's working. Hopefully I can figure out how to display and style all the other elements from here.

Thanks David.

Hello David

Yes, that does help.

But now I'm realising that if I were able to pipe the array of post id to a WPSP list, then I already have styling done for previous lists. So it might be a cleaner way to do this.

How can I pass the ACF array to a WPSP list ?

Hello Elvin

Thanks for jumping in.

So as I understand it, the ACF field is storing an array of Post IDs

I think earlier David was suggesting that I should be able to use those Post IDs inside a WPSP list, but I'm not sure what to edit to enable that.

I already have a hook setup displaying some ACF content, and I have also displayed ACF content with WPSP in the past, but not from an ACF array.

Do you have an example of how to do this ?

This archived topic is closed to new replies.