• Resolved markwill

    (@markwill)


    This plugin could be about to save the day! I’ve been searching for a way to show a filtered list of posts, for a category for example, but have some of the posts “decorated” in the UI, depending on presence of some data associated with the post.

    For example, the category might have 20 posts. They are all displayed with the same format (perhaps the featured image, title and a link, etc) but for SOME of these posts, flagged in some way, I want to display a little indicator.

    Providing it’s easy to manage, I am pretty open as to how I specify which posts are to have this visual indicator, but thinking that just applying a tag to the post should do the trick.

    So, with this in mind, I have two

    . Retrieve the query, including the field that indicates whether they are to be flagged.

    . Display the posts in some form, with the little visual indicator displayed conditional on whether the flag exists.

    . For extra points, I’d really like to be able to use some form of designer, such as the Elementor posts widget, to design the presentation, but that might be a stretch.

    I’m thinking this plugin can go a long way towards helping me here, but before I get stuck into it in detail just want to make sure I am not going down a dark alley! Is this generally looking pretty feasible?

    PS: I am an “occasional” programmer and can add to a custom plugin I maintain. My biggest weakness is in the presentation layer, particularly CSS and ensuring a responsive UI, hence my desire to use something like Elementor, if possible.

Viewing 1 replies (of 1 total)
  • Plugin Author Bill Erickson

    (@billerickson)

    I think the simplest approach would be to add unique classes to the individual listing item based on its tags, then use CSS to style those items using those classes.

    Here’s a tutorial on how to add category classes to the listing items: https://displayposts.com/2019/01/03/add-category-classes/

    To do that with tags, change this line:

    
      $categories = get_the_terms( get_the_ID(), 'category' );
    

    To this:

    
      $categories = get_the_terms( get_the_ID(), 'post_tag' );
    

    This will add every tag as a class on the post. It might be better to maintain a whitelist of tags you’d like to add. Something like this:

    
    /**
     * Display Posts Shortcode, add tag classes 
     * @see https://displayposts.com/2019/01/03/add-category-classes/
     */
    function be_dps_add_tag_classes( $classes ) {
      $include = array( 'tag-1', 'tag-2' );
      $terms = get_the_terms( get_the_ID(), 'post_tag' );
      if( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
        foreach( $terms as $term ) {
          if( in_array( $term->slug, $include ) ) {
            $classes[] = 'term-' . $term->slug;
        }
      }
      return $classes;
    }
    add_filter( 'display_posts_shortcode_post_class', 'be_dps_add_tag_classes' );
    
Viewing 1 replies (of 1 total)

The topic ‘Conditional display of post metafields’ is closed to new replies.