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

    Pauline H
    Participant

    I’m using ethos:

    I know how to add the recent posts shortcode to the widget area, my question is how do I make it a specific post id, so I can control what is showing. I know I can by category but I would like to use specific posts.

    Thanks

    #105410

    Nabeel A
    Moderator

    Thanks for writing in! Regretfully, at this time I am not entirely certain what it is you would like to accomplish based on the information given in your post. If you wouldn’t mind providing us with a little more clarification on what it is you’re wanting to do (a link to a similar example site would be very helpful, or perhaps some screenshots), we’ll be happy to provide you with a response once we have a better understanding of the situation.

    #105413

    Pauline H
    Participant

    I’m just wanting to know what I would add to the recent post shortcode to have the result based on a specific post instead of a category?

    #105505

    Christian
    Moderator

    Hey Pauline,

    You’ll need to create a modified copy of the Recent Posts shortcode function. The file you need is shortcodes.php located in wp-content\plugins\x-shortcodes\functions. For an example, please add the code below in your functions.php. The code does what you need. Use the shortcode [x_post post_id="1"] (post_id is the ID of your post). It is a modified copy of the Recent Posts shortcode function. Please compare the difference between the original (Recent Posts section in shortcodes.php and the modified copy.

    function x_shortcode_post( $atts ) {
      extract( shortcode_atts( array(
        'id'          => '',
        'class'       => '',
        'style'       => '',
        'type'        => '',
        'category'    => '',
        'no_image'    => '',
        'fade'        => '',
        'post_id'     => ''
      ), $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';
      $category      = ( $category    != ''          ) ? $category : '';
      $category_type = ( $type        == 'post'      ) ? 'category_name' : 'portfolio-category';
      $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(
          'p'          => $post_id
        ) );
    
        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-' . get_theme_mod( 'x_stack' ) . '-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( 'x_post', 'x_shortcode_post' );

    Hope that helps. 🙂

    #105515

    Pauline H
    Participant

    Do I do this within the child theme? Will I have two shortcodes that work? the old one and the new one? I found the file, I’m confused as where to add it in that file. It little more clarification would help. Thanks so much!

    #105561

    Christian
    Moderator

    Hey Pauline,

    It is best to put this in the functions.php of the child theme so it won’t get overridden by updates. Place the given code at the very end of the code in functions.php. The child theme’s functions.php file should be blank the first time you open it.

    Thanks.