Recent Post Shortcode

Hello, I am trying to customize the recent post widget for stickied posts.

I found a few forum threads, similar to what I am looking for, but I hope I could get help in piecing them all together.

Stickied Post Shortcode: https://theme.co/apex/forums/topic/sticky-posts-on-the-home-page/

Adding Excerpt to Recent Post Shortcode: https://theme.co/apex/forums/topic/add-excerpt-to-recent-posts-shortcode/

And in the ‘Stickied Post Shortcode’ thread, the support staff provided a way to ignore stickied posts on the blog. Is there a way to remove them from the loop completely?

Also, I found that the shortcode would add the most recent ‘non-stickied’ post if there are less stickied posts than the count. Is there a way to simply have it so that the shortcode displays only stickied posts. An extreme example would be having no posts showing if there are no stickied posts.

Lastly, is there a way to style it like regular posts?

Sorry this is a long request. Thank you.

Hello There,

Thanks for writing in and the very detailed post information. Could you please post in your reply the actual code you have in your child theme’s functions.php so that we can modify it and add the exclusion code of the sticky posts as suggested from this link: http://www.wpbeginner.com/wp-themes/how-to-exclude-sticky-posts-from-the-loop-in-wordpress/

Thank you in advance.

Yes, of course!
I haven’t actually customized the functions.

So I would like to add the post excerpts to this function and prevent non-sticky posts from displaying to match the count. An extreme example would be having no posts showing if there are no stickied posts.

// Custom Recent Post
// =============================================================================
function x_shortcode_recent_posts_v2code( $atts ) {
extract( shortcode_atts( array(
‘id’ => ‘’,
‘class’ => ‘’,
‘style’ => ‘’,
‘type’ => ‘post’,
‘count’ => ‘’,
‘category’ => ‘’,
‘offset’ => ‘’,
‘orientation’ => ‘’,
// ‘show_excerpt’ => ‘true’,
‘no_sticky’ => ‘’,
‘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’;
// $show_excerpt = ( $show_excerpt == ‘true’ );
$no_sticky = ( $no_sticky == ‘true’ );
$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=“mao ni {$class}{$orientation}” {$style} {$data} data-fade=”{$fade}" >";

$q = new WP_Query( array(
  'orderby'             => 'date',
  'post_type'           => "{$type}",
  'posts_per_page'      => "{$count}",
  'offset'              => "{$offset}",
  "{$category_type}"    => "{$category}",
  'ignore_sticky_posts' => $no_sticky
) );

$i = 0;

if ( $q->have_posts() ) : while ( $q->have_posts() && $count > $i  ) : $q->the_post();
  $i++;
  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';
  }

  // $excerpt = ( $show_excerpt ) ? '<div class="x-recent-posts-excerpt"><p>' . preg_replace('/<a.*?more-link.*?<\/a>/', '', cs_get_raw_excerpt() ) . '</p></div>' : '';

  $output .= '<a class="x-recent-post' . $count . ' ' . $image_output_class . '" href="' . get_permalink( get_the_ID() ) . '" title="' . esc_attr( sprintf( __( 'Permalink to: "%s"', 'cornerstone' ), 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>'
                   // . $excerpt
                 . '</div>'
               . '</div>'
             . '</article>'
           . '</a>';

endwhile; endif; wp_reset_postdata();

$output .= ‘’;

return $output;
}

add_action(‘wp_head’, ‘change_recent_posts_to_v2’);

function change_recent_posts_to_v2() {
remove_shortcode( ‘x_recent_posts’ );
add_shortcode( ‘x_recent_posts’, ‘x_shortcode_recent_posts_v2code’ );
}

And remove sticky posts instead of ignoring them like this function.

add_action(‘pre_get_posts’, ‘my_ignore_sticky’);
function my_ignore_sticky($query) {
if (is_home() && $query->is_main_query()) {
$query->set(‘ignore_sticky_posts’, true);
}
}

Thank you!

Hi,

Would you mind providing us with login credentials so we can take a closer look? Please provide following information:

Set it as Secure Note

  • Link to your site
  • WordPress Admin username / password
  • FTP credentials

All the best!

Okay. I will.

Hi There,

To show the excerpt, look for the following lines of codes from what you have,

 // 'show_excerpt' => 'true',

  // $show_excerpt = ( $show_excerpt == 'true' );

   // $excerpt = ( $show_excerpt ) ? '<div class="x-recent-posts-excerpt"><p>' . preg_replace('/<a.*?more-link.*?<\/a>/', '', cs_get_excerpt_for_social() ) . '</p></div>' : '';
  
  // . $excerpt

Remove the double slash from the start of each line. Double slash means comment. The excerpt will show after that when you use the recent post shortcode.

To remove stickied post completely, please update the last function to this:

  add_action('pre_get_posts', 'my_ignore_sticky');
   function my_ignore_sticky($query) {
        $query->set('ignore_sticky_posts', true);
   }

We have remove the blog filter from the code.
Unfortunately, adding a shortcode that will display only sticky post is outside the scope of our support as this is not related to an issue with the existing theme functions and instead has to do with customization. We do have an in-house custom development team that offer paid services, who may be able to assist. They can be contacted at pinnacle@theme.co if this is of interest to you. If you have any further questions about the theme, we are more than happy to provide you with assistance on these inquiries.

Thank you for your understanding.

March 2021 Update: The above code example has been adjusted to account for a change to a function name.

Hello,

I changed the code, but it doesn’t seem to be working?

I don’t see any excerpts and I still see stickied posts on the homepage.

Also, the stickied post widget doesn’t show stickied posts once I add the new function (add_action(‘pre_get_posts’, ‘my_)ignore_sticky’);…) to the functions.php

Thank you.

Hi there,

The excerpt is hidden by this custom CSS

.x-recent-posts p:first-child, .x-recent-posts > p {
    display: none;
}

Please remove it :slight_smile:

Thanks!

That’s the code an X Theme staff member gave me to fix the formatting issue from using X Theme’s recent post shortcode as a text widget.

Hi,

We can override that code by adding the code below in the custom css.

.x-recent-posts .entry-wrap .x-recent-posts-excerpt p {
   display:block !important;
}

The entire code will be

.x-recent-posts p:first-child, .x-recent-posts > p {
    display: none;
}

.x-recent-posts .entry-wrap .x-recent-posts-excerpt p {
   display:block !important;
}

Hope that helps.

Okay, that seems to work.
Thank you.

You are most welcome. :slight_smile:

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.