Tagged: x
-
AuthorPosts
-
May 12, 2016 at 10:49 am #987968
Chris FParticipantHey there,
I’ve been following some past posts to figure out how to add tag filtering to the Recent Posts shortcode, but I’m not having any luck. I already have a child theme set up and have previously edited it to add the ability to show post excerpts. Would someone be able to assist?
- This is my current functions.php code: http://www.codesend.com/view/46e22dbd7cf76158c55ac74bd32453d1/
- This is the shortcode I’m using for displaying posts with excerpts:
[x_recent_posts type=”posts” count=”1″ orientation=”horizontal” no_image=”false” enable_excerpt=”true”]
Many thanks!
May 12, 2016 at 3:24 pm #988437
JadeModeratorHi Chris,
Please try this code:
function x_shortcode_recent_posts_v2code( $atts ) { extract( shortcode_atts( array( 'id' => '', 'class' => '', 'style' => '', 'type' => '', 'count' => '', 'category' => '', 'enable_excerpt' => '', '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'; $enable_excerpt = ( $enable_excerpt == 'true' ) ? true : false; $output = "<div {$id} class=\"{$class}{$orientation}\" {$style} data-fade=\"{$fade}\">"; $q = new WP_Query( array( 'orderby' => 'date', '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-' . 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>' . ( $enable_excerpt ? '<span class="x-recent-posts-excerpt">' . strip_tags( get_the_excerpt() ) . '</span>' : '' ) . '</div>' . '</div>' . '</article>' . '</a>'; endwhile; endif; wp_reset_postdata(); $output .= '</div>'; return $output; } add_action('wp_head', 'change_recent_posts_to_v2'); function change_recent_posts_to_v2() { remove_shortcode( 'recent_posts' ); add_shortcode( 'recent_posts', 'x_shortcode_recent_posts_v2code' ); }Let us know how it goes.
May 13, 2016 at 8:46 am #989702
Chris FParticipantHi Jade,
Thanks for the speedy reply! I think you may have misread my question. I’m trying to add tag filtering to the recent posts shortcode, as well as display excerpts (like my previous code had). I don’t see any tag-related code in the code you provided. Also, when I tested out the code, it no longer shows excerpts and only displays one post.
Cheers,
ChrisMay 13, 2016 at 4:18 pm #990333
RadModeratorHi Chris,
Please try this one,
function x_shortcode_recent_posts_v2code( $atts ) { extract( shortcode_atts( array( 'id' => '', 'class' => '', 'style' => '', 'type' => '', 'count' => '', 'category' => '', 'offset' => '', 'orientation' => '', 'no_image' => '', 'fade' => '', 'tag' => '' ), $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' => 'date', 'post_type' => "{$type}", 'posts_per_page' => "{$count}", 'offset' => "{$offset}", "{$category_type}" => "{$category}", 'tag' => $tag ) ); 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>' . '<span class="x-recent-posts-excerpt">' . strip_tags( get_the_excerpt() ) . '</span>' . '</div>' . '</div>' . '</article>' . '</a>'; endwhile; endif; wp_reset_postdata(); $output .= '</div>'; return $output; } add_action('wp_head', 'change_recent_posts_to_v2'); function change_recent_posts_to_v2() { remove_shortcode( 'recent_posts' ); remove_shortcode( 'x_recent_posts' ); remove_shortcode( 'cs_recent_posts' ); add_shortcode( 'recent_posts', 'x_shortcode_recent_posts_v2code' ); add_shortcode( 'x_recent_posts', 'x_shortcode_recent_posts_v2code' ); add_shortcode( 'cs_recent_posts', 'x_shortcode_recent_posts_v2code' ); }Hope that helps.
May 16, 2016 at 8:14 am #993542
Chris FParticipantHey there,
Thanks for the help. I think this is working properly, but there are some issues:
- Custom Post Types have stopped working
- Post images are not cropping properly/evenly and extra space is added below them (Screenshot: https://dl.dropboxusercontent.com/spa/g67vzfspcwsv70w/x9nunhed.png)
- Excerpts are displayed even if I don’t use the excerpt command or if I set it to false.
Cheers!
May 16, 2016 at 8:41 am #993584
Paul RModeratorHi Chris,
Would you mind providing us with login credentials so we can take a closer look? To do this, you can make a post with the following info:
– Link to your site
– WordPress Admin username / password
– FTP credentialsDon’t forget to select Set as private reply. This ensures your information is only visible to our staff.
May 16, 2016 at 11:25 am #993863
Chris FParticipantThis reply has been marked as private.May 17, 2016 at 1:37 am #995000
Paul RModeratorHi Chris,
I was able to fix it by changing the code in functions.php file with this.
function x_shortcode_recent_posts_v2code( $atts ) { extract( shortcode_atts( array( 'id' => '', 'class' => '', 'style' => '', 'type' => 'post', 'count' => '', 'category' => '', 'offset' => '', 'orientation' => '', 'enable_excerpt' => 'false', 'no_sticky' => '', 'no_image' => '', 'fade' => '', 'tag' => '' ), $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'; $enable_excerpt = ( $enable_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=\"{$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, 'tag' => $tag ) ); 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'; } $excerpt = ( $enable_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 .= '</div>'; return $output; } add_action('wp_head', 'change_recent_posts_to_v2'); function change_recent_posts_to_v2() { remove_shortcode( 'recent_posts' ); remove_shortcode( 'x_recent_posts' ); remove_shortcode( 'cs_recent_posts' ); add_shortcode( 'recent_posts', 'x_shortcode_recent_posts_v2code' ); add_shortcode( 'x_recent_posts', 'x_shortcode_recent_posts_v2code' ); add_shortcode( 'cs_recent_posts', 'x_shortcode_recent_posts_v2code' ); }Kindly check on your end.
Thanks
May 17, 2016 at 7:40 am #995323
Chris FParticipantHi Paul,
Thanks! We have two out of three working now. Unfortunately, custom post types still aren’t displaying. This can be tested using the CPT pl_publications (http://plough.cssdesign.ca/pl_publications/). We had it working on the homepage at one point…
Thanks again,
ChrisMay 17, 2016 at 9:55 am #995536
Paul RModeratorHi Chris,
You need to add your custom post types under allowed post types.
eg. Add pl_publications.
$allowed_post_types = apply_filters( 'cs_recent_posts_post_types', array( 'post' => 'post', 'pl_publications' => 'pl_publications' ) );http://plough.cssdesign.ca/test/
Hope that helps
May 18, 2016 at 12:26 pm #997765
Chris FParticipantThanks, Paul! I’m a coder not a programmer so I never would have figured out how to do that. 🙂
One last question… sorry about this… but my client is asking if there is any way to filter by tag across all posts and CPTs?
Last question, I promise. 🙂
May 18, 2016 at 3:08 pm #998027
RahulModeratorHi There,
Thanks for writing in! But further customizations from here would be getting into custom development, which is outside the scope of support we can offer. If you need more in depth changes, you may wish to consult with a developer. X is quite extensible with child themes, so there are plenty of possibilities.
Thanks for understanding.
May 18, 2016 at 3:11 pm #998034
Chris FParticipantYup, that’s exactly what I told my client but they asked me to ask anyway. You’ve already gone above and beyond, thanks again!
May 18, 2016 at 6:42 pm #998336
JadeModeratorYou’re welcome Chris.
-
AuthorPosts
- <script> jQuery(function($){ $("#no-reply-987968 .bbp-template-notice, .bbp-no-topic .bbp-template-notice").removeClass('bbp-template-notice'); }); </script>
