Navigation
This is archived content. Visit our new forum.
  • Author
    Posts
  • #249106
    Zsolt D
    Participant

    Hi,

    I use the recent post shortcode (with category), but the recent post list includes the active post too, which is unneccessary and confusing. How can I exclude the active post from the list?
    Sample:

    A meddőség immunológiai okairól


    X and X-shortcode are up-to date.
    Thx

    Zsolt

    #249157
    Thai
    Moderator

    Hello There,

    Thanks for posting in!

    This is only possible if you override the recent_post shortcode by customizing your own. Please add this following code below into functions.php file locates in child theme folder:

    
    function x_shortcode_recent_posts_v2( $atts ) {
      extract( shortcode_atts( array(
        'id'          => '',
        'class'       => '',
        'style'       => '',
        'type'        => '',
        'count'       => '',
        'category'    => '',
        'offset'      => '',
        'orientation' => '',
        'no_image'    => '',
        'fade'        => ''
      ), $atts, 'recent_posts' ) );
    
      $id            = ( $id          != ''          ) ? 'id="' . esc_attr( $id ) . '"' : '';
      $class         = ( $class       != ''          ) ? 'x-recent-posts cf ' . esc_attr( $class ) : 'x-recent-posts cf';
      $style         = ( $style       != ''          ) ? 'style="' . $style . '"' : '';
      $type          = ( $type        == 'portfolio' ) ? 'x-portfolio' : 'post';
      $count         = ( $count       != ''          ) ? $count : 3;
      $category      = ( $category    != ''          ) ? $category : '';
      $category_type = ( $type        == 'post'      ) ? 'category_name' : 'portfolio-category';
      $offset        = ( $offset      != ''          ) ? $offset : 0;
      $orientation   = ( $orientation != ''          ) ? ' ' . $orientation : ' horizontal';
      $no_image      = ( $no_image    == 'true'      ) ? $no_image : '';
      $fade          = ( $fade        == 'true'      ) ? $fade : 'false';
    
      $js_params = array(
        'fade' => ( $fade == 'true' )
      );
    
      $data = x_generate_data_attributes( 'recent_posts', $js_params );
    
      $output = "<div {$id} class=\"{$class}{$orientation}\" {$style} {$data} data-fade=\"{$fade}\" >";
        $args = array(
          'orderby'          => 'date',
          'post_type'        => "{$type}",
          'posts_per_page'   => "{$count}",
          'offset'           => "{$offset}",
          "{$category_type}" => "{$category}"
        );
        if(is_single()){
          $args['post__not_in'] = array(get_the_ID());
        }
        $q = new WP_Query( $args );
    
        if ( $q->have_posts() ) : while ( $q->have_posts() ) : $q->the_post();
    
          if ( $no_image == 'true' ) {
            $image_output       = '';
            $image_output_class = 'no-image';
          } else {
            $image_output       = '<div class="x-recent-posts-img">' . get_the_post_thumbnail( get_the_ID(), 'entry-cropped', NULL ) . '</div>';
            $image_output_class = 'with-image';
          }
    
          $output .= '<a class="x-recent-post' . $count . ' ' . $image_output_class . '" href="' . get_permalink( get_the_ID() ) . '" title="' . esc_attr( sprintf( __( 'Permalink to: "%s"', '__x__' ), the_title_attribute( 'echo=0' ) ) ) . '">'
                     . '<article id="post-' . get_the_ID() . '" class="' . implode( ' ', get_post_class() ) . '">'
                       . '<div class="entry-wrap">'
                         . $image_output
                         . '<div class="x-recent-posts-content">'
                           . '<h3 class="h-recent-posts">' . get_the_title() . '</h3>'
                           . '<span class="x-recent-posts-date">' . get_the_date() . '</span>'
                         . '</div>'
                       . '</div>'
                     . '</article>'
                   . '</a>';
    
        endwhile; endif; wp_reset_postdata();
    
      $output .= '</div>';
    
      return $output;
    }
    add_action('init', 'x_recent_posts_v2');
    
    function x_recent_posts_v2() {
      remove_shortcode( 'recent_posts' );
      add_shortcode( 'recent_posts', 'x_shortcode_recent_posts_v2' );
    }
    

    Hope it helps.

    #249251
    Zsolt D
    Participant

    Hi,

    it works perfect, thank you for your quick response. Excellent support.
    You may apply this function in the X core…?

    Have a nice day!

    Zsolt

    #249282
    Paul R
    Moderator

    Hi Zsolt,

    Yes, I’ll forward this to our web development team for review.

    Thanks.

    #311447
    Zsolt D
    Participant

    Hi!

    I changed the recent post function with your help, see above.

    I am afraid, that after upgrading 4.x the suggested code isn’t valid, can you revise it?

    (After upgrading X to 4.x pages with shortcode [recent_posts] fail to load
    I should manually search pages with the shortcode [recent_posts] and change to [x_recent_posts], but loose the function excluding the active page.)

    Thanks

    Zsolt

    #311549
    Thai
    Moderator

    Hi There,

    Please update the previous code a bit:

    function x_shortcode_recent_posts_v2( $atts ) {
      extract( shortcode_atts( array(
        'id'          => '',
        'class'       => '',
        'style'       => '',
        'type'        => 'post',
        'count'       => '',
        'category'    => '',
        'offset'      => '',
        'orientation' => '',
        'no_image'    => '',
        'fade'        => ''
      ), $atts, 'x_recent_posts' ) );
    
      $allowed_post_types = apply_filters( 'cs_recent_posts_post_types', array( 'post' => 'post' ) );
      $type = ( isset( $allowed_post_types[$type] ) ) ? $allowed_post_types[$type] : 'post';
    
      $id            = ( $id          != ''          ) ? 'id="' . esc_attr( $id ) . '"' : '';
      $class         = ( $class       != ''          ) ? 'x-recent-posts cf ' . esc_attr( $class ) : 'x-recent-posts cf';
      $style         = ( $style       != ''          ) ? 'style="' . $style . '"' : '';
      $count         = ( $count       != ''          ) ? $count : 3;
      $category      = ( $category    != ''          ) ? $category : '';
      $category_type = ( $type        == 'post'      ) ? 'category_name' : 'portfolio-category';
      $offset        = ( $offset      != ''          ) ? $offset : 0;
      $orientation   = ( $orientation != ''          ) ? ' ' . $orientation : ' horizontal';
      $no_image      = ( $no_image    == 'true'      ) ? $no_image : '';
      $fade          = ( $fade        == 'true'      ) ? $fade : 'false';
    
      $js_params = array(
        'fade' => ( $fade == 'true' )
      );
    
      $data = cs_generate_data_attributes( 'recent_posts', $js_params );
    
      $output = "<div {$id} class=\"{$class}{$orientation}\" {$style} {$data} data-fade=\"{$fade}\" >";
    
        $args = array(
          'orderby'          => 'date',
          'post_type'        => "{$type}",
          'posts_per_page'   => "{$count}",
          'offset'           => "{$offset}",
          "{$category_type}" => "{$category}"
        );
        if(is_single()){
          $args['post__not_in'] = array(get_the_ID());
        }
        $q = new WP_Query( $args );
    
        if ( $q->have_posts() ) : while ( $q->have_posts() ) : $q->the_post();
    
          if ( $no_image == 'true' ) {
            $image_output       = '';
            $image_output_class = 'no-image';
          } else {
            $image              = wp_get_attachment_image_src( get_post_thumbnail_id(), 'entry-cropped' );
            $bg_image           = ( $image[0] != '' ) ? ' style="background-image: url(' . $image[0] . ');"' : '';
            $image_output       = '<div class="x-recent-posts-img"' . $bg_image . '></div>';
            $image_output_class = 'with-image';
          }
    
          $output .= '<a class="x-recent-post' . $count . ' ' . $image_output_class . '" href="' . get_permalink( get_the_ID() ) . '" title="' . esc_attr( sprintf( __( 'Permalink to: "%s"', csl18n() ), the_title_attribute( 'echo=0' ) ) ) . '">'
                     . '<article id="post-' . get_the_ID() . '" class="' . implode( ' ', get_post_class() ) . '">'
                       . '<div class="entry-wrap">'
                         . $image_output
                         . '<div class="x-recent-posts-content">'
                           . '<h3 class="h-recent-posts">' . get_the_title() . '</h3>'
                           . '<span class="x-recent-posts-date">' . get_the_date() . '</span>'
                         . '</div>'
                       . '</div>'
                     . '</article>'
                   . '</a>';
    
        endwhile; endif; wp_reset_postdata();
    
      $output .= '</div>';
    
      return $output;
    }
    add_action('init', 'x_recent_posts_v2');
    
    function x_recent_posts_v2() {
      remove_shortcode( 'x_recent_posts' );
      add_shortcode( 'x_recent_posts', 'x_shortcode_recent_posts_v2' );
    }

    Please replace all the [recent_posts] with [x_recent_posts].

    Let us know how it goes!

    #312427
    Zsolt D
    Participant

    Hi!

    Thank you. I updated the code, and it works fine with post, but I have one further question. I use “post-tags-and-categories-for-pages” plugin to list not only the posts but also the pages with the same categorie in different lists. The active page is not filtered with this code from the recent posts list. Can you change the code to filter not only the posts but the pages too? Thank you very much for your help.
    See here:

    Meddőségi vizsgálatok

    With kind regards:
    Zsolt

    #312601
    Zeshan
    Member

    Hi Zsolt,

    Thanks for writing in!

    Recent posts shortcode is specifically for “Posts” and “Portfolio” items, that might be the reason it’s not filtering the pages. As it sounds like you might be having an issue with a third party plugin or script. Regretfully, we cannot provide support for third party plugins or scripts as our support policy in the sidebar states due to the fact that there is simply no way to account for all of the potential variables at play when using another developer’s plugin or script. Because of this, any questions you have regarding setup, integration, or troubleshooting any piece of functionality that is not native to X will need to be directed to the original developer.

    Thank you for your understanding.

    #401571
    vendinimarketing
    Participant

    Hi there!

    I’ve used the above snippet (reply #311549) in my child theme and the shortcode is working beautifully. However, I was wondering if there was an alteration I could make so that the X theme “Recent Posts” widget also excludes the current post.

    An example of what I mean can be seen here:

    http://vendini.co/blog/2015/08/please-welcome-our-new-vp-of-customer-success-tricia-baker/

    The widget generated “Recent Posts” displays Tricia Baker’s post in the list.
    The shortcode I added as a text widget called “Recent Posts 2” does not.

    Thanks!

    #407318
    Friech
    Moderator

    Hi There,

    Thanks for writing in! Regretfully this isn’t a feature offered by X. Recent post can only filter category and offset but not the active post. It could be possible with custom development, but this would be outside the scope of support we can offer. You may wish to consult a developer to assist you with this. X is quite extensible with child themes, so there are plenty of possibilities.

    Thanks for understanding. Take care!

  • <script> jQuery(function($){ $("#no-reply-249106 .bbp-template-notice, .bbp-no-topic .bbp-template-notice").removeClass('bbp-template-notice'); }); </script>