Navigation
This is archived content. Visit our new forum.
  • Author
    Posts
  • #76834

    MulderDSM
    Participant

    I’m using this code to display a couple or rows of portfolio items I have.

    [recent_posts type=”portfolio” category=”products” count=”4″ orientation=”horizontal”]

    [recent_posts type=”portfolio” category=”products” count=”4″ offset=”4″ orientation=”horizontal”]

    All works well, except I’d like to see these in alphabetical order by title. What do I need to edit?

    #77224

    Christian
    Moderator

    Hey there,

    You can edit shortcodes.php located in wp-content\plugins\x-shortcodes\functions. Search for the function x_shortcode_recent_posts. Under that function, look for the line

    'orderby' => 'date',

    Change date to title. If you want to order it ascending, below it, add the line

    'order' => 'ASC',

    The problem with this is that you need to do this every plugin update. With that said, you’ll want to have a modified copy of the shortcode in your functions.php. In order for us to do that, we need to rename the function and the shortcode tag of the modified copy because it would create a conflict if you create the same function. Add the code below in your functions.php..

    function x_shortcode_recent_posts_custom( $atts ) {
      extract( shortcode_atts( array(
        'id'          => '',
        'class'       => '',
        'style'       => '',
        'type'        => '',
        'count'       => '',
        'category'    => '',
        'offset'      => '',
        'orientation' => '',
        'no_image'    => '',
        'fade'        => ''
      ), $atts ) );
    
      $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';
    
      $output = "<div {$id} class=\"{$class}{$orientation}\" {$style} data-fade=\"{$fade}\">";
    
        $q = new WP_Query( array(
          'orderby'          => 'title',
          'order'            => 'ASC',
          'post_type'        => "{$type}",
          'posts_per_page'   => "{$count}",
          'offset'           => "{$offset}",
          "{$category_type}" => "{$category}"
        ) );
    
        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-' . x_get_option( 'x_stack', 'integrity' ) . '-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_shortcode( 'recent_posts_custom', 'x_shortcode_recent_posts_custom' );

    We call this new shortcode [recent_posts_obt] (order by title). You can use it like the Recent Post shortcode. You can also override the Recent Posts shortcode (see http://theme.co/x/member/forums/topic/add-excerpt-to-recent-posts-shortcode/#post-66964).

    Hope that helps. 🙂

    #77830

    MulderDSM
    Participant

    That did the trick! Thank’s so much! Long term, you may want to consider that has option when you update the plug in. I can see others needing to sort recent posts by more than just the date.

    #78070

    Cousett
    Member

    Glad this helped!

    #107195

    Michael V
    Participant

    Hi there,

    Is it possible to sort the recent posts by a number that we enter as a tag?

    Something like:

    ‘orderby’ => ‘tags’,
    ‘order’ => ‘ASC’,

    Thanks for the help,

    Michael

    #107280

    Mrinal
    Member

    Hi Michael,

    Luckily, you can add tag parameter to recent posts shortcode as like the following:
    [recent_posts count="4" tag="tag-slug"]

    So, if you’ve done the above code customization described by us then just add the tag parameter in recent posts shortcode.

    #107311

    Michael V
    Participant

    Hi Support,

    Great! I’ll definitely use that!

    But it’s not what I try to achieve right now: I don’t want to show all portfolio-item with a specific tag but I would like to show all the portfolio-item of a category ordered by tag value (alphabetical).

    You did it by title here:

    $q = new WP_Query( array(
    ‘orderby’ => ‘title’,
    ‘order’ => ‘ASC’,
    ‘post_type’ => “{$type}”,
    ‘posts_per_page’ => “{$count}”,
    ‘offset’ => “{$offset}”,
    “{$category_type}” => “{$category}”
    ) );

    I’m sure it can be achieve easily but I don’t know which slug to use (I know “title” and “date”, but is there one for the tag?)!

    Thanks for help!
    X support is great 😉

    Michael

    #107338

    Michael V
    Participant

    Or maybe it’s easier to sort by post meta ? :

    ‘orderby’ => ‘post_meta’,

    #107389

    Zeshan
    Member

    Hi Micheal,

    Thank you for writing in!

    Regretfully it isn’t possible with WP_Query function, however it could be possible with MySQL query but that would fall beyond the scope of support we can offer. You may wish to consult a developer, or a service like WerkPress or Elto to assist you with this. X is quite extensible with child themes, so there are plenty of possibilities.

    Thanks for understanding. Take care!

    #347115

    eugenetim
    Participant

    Hi. I want to make such trick with latest version of X theme (Cornerstone). Can you update the example of custom shortcode function if I need just reverse order of posts (by date) when I’ll use [recent_posts_obt] shortcode. Thank you!
    P.S. Important! I am using Icon stack.

    #347120

    Thai
    Moderator

    Hi @eugenetim,

    Try adding following code under functions.php file locates in child theme folder:

    <?php
    
    // Recent Posts
    // =============================================================================
    
    function x_shortcode_recent_posts_new( $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}\" >";
    
        $q = new WP_Query( array(
          'orderby'          => 'date',
          'order'            => 'ASC',
          'post_type'        => "{$type}",
          'posts_per_page'   => "{$count}",
          'offset'           => "{$offset}",
          "{$category_type}" => "{$category}"
        ) );
    
        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_shortcode( 'recent_posts_obt', 'x_shortcode_recent_posts_new' );

    Let us know how it goes!

    #347565

    eugenetim
    Participant

    Works great! Thank you!

    #347566

    Christopher
    Moderator

    You’re welcome.