add shortcode heading showing multiple time - php

Hi my code is below for adding shortcode in posts. when i am adding shortcode two times it shows me heading two times that i added in code "Recent Posts" is there is way to show this heading only top means one time?
/*shortcode start*/
add_shortcode( 'recent-posts', 'PL_recent_posts' );
function PL_recent_posts( $atts ) {
extract( shortcode_atts( array(
'numbers' => '5',
'order' => 'ASC',
), $atts ) );
$rposts = new WP_Query( array( 'posts_per_page' => $numbers, 'orderby' => 'date' , 'colorss' => $color ) );
if ( $rposts->have_posts() ) {
$html = '<h3>Recent Posts</h3><ul class="recent-posts">';
while( $rposts->have_posts() ) {
$rposts->the_post();
$html .= sprintf(
'<li>%s</li>',
get_permalink($rposts->post->ID),
get_the_title(),
get_the_title()
);
}
$html .= '</ul>';
}
wp_reset_query();
return $html;
}

Define a global variable to detect whether title is already added.
function PL_recent_posts( $atts ) {
global $title_added;
...
if ( $rposts->have_posts() ) {
if ( $title_added ) {
$html = '<ul class="recent-posts">';
} else {
$html = '<h3>Recent Posts</h3><ul class="recent-posts">';
$title_added = true;
}
Hope that helps..!

Related

Display some WooCommerce product reviews with limited content length randomly

I am making shortcode for reviews Based on WooCommerce: Display some reviews randomly on home page to put on other pages. How can i limit reviews by character numbers, like 100 or 200 characters. SO they will not be too long and fit any page.
I am using following code for reviews now
function get_random_five_stars_products_reviews( $atts ) {
// Extract shortcode attributes
extract( shortcode_atts( array(
'limit' => 5, // number of reviews to be displayed by default
), $atts, 'woo_reviews' ) );
$comments = get_comments( array(
'status' => 'approve',
'post_status' => 'publish',
'post_type' => 'product',
'meta_query' => array( array(
'key' => 'rating',
'value' => '5',
) ),
) );
shuffle($comments);
$comments = array_slice( $comments, 0, $limit );
$html = '<ul class="products-reviews">';
foreach( $comments as $comment ) {
$rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) );
$html .= '<li class="review">';
$html .= '<div>'.get_the_title( $comment->comment_post_ID ).'</div>';
if ( $rating > 4 ) $html .= wc_get_rating_html( $rating );
$html .= '<div>' .$comment->comment_content.'</div>';
$html .= "<div>Posted By :".$comment->comment_author." On ".$comment->comment_date. "</div>";
$html .= '</li>';
}
return $html . '</ul>';
}
add_shortcode('woo_reviews', 'get_random_five_stars_products_reviews');
To limit displayed 5 starts reviews with a content that doesn't exceed a specific length (here 100 characters) use the following:
function get_random_five_stars_products_reviews( $atts ) {
// Extract shortcode attributes
extract( shortcode_atts( array(
'limit' => 5, // number of reviews to be displayed by default
'length' => 100, // Limit comments text content lenght
'show_stars' => true, // Or "false" to hide
), $atts, 'woo_reviews' ) );
$comments = get_comments( array(
'status' => 'approve',
'post_status' => 'publish',
'post_type' => 'product',
'meta_query' => array( array(
'key' => 'rating',
'value' => '5',
) ),
) );
shuffle($comments);
$count = 0; // Initializing
$html = '<ul class="products-reviews">';
foreach( $comments as $comment ) {
$content = $comment->comment_content;
if( strlen($content) <= $length && $count < $limit ) {
$rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) );
$html .= '<li class="review">';
$html .= '<div>'.get_the_title( $comment->comment_post_ID ).'</div>';
if ( $show_stars ) {
$html .= wc_get_rating_html( $rating );
}
$html .= '<div>' .$content. '</div>';
$html .= "<div>Posted By :".$comment->comment_author." On ".$comment->comment_date. "</div>";
$html .= '</li>';
$count++; // Increase count
}
if ( $count == $limit ) {
break; // Stop the loop
}
}
return $html . '</ul>';
}
add_shortcode('woo_reviews', 'get_random_five_stars_products_reviews');
Code goes in functions.php file of the active child theme (or active theme). It should works.

WooCommerce: Display some 5 stars rating products reviews randomly

Based on WooCommerce: Display some reviews randomly on home page answer code, I am using the following code that shows 5 random reviews:
function get_woo_reviews()
{
$comments = get_comments(
array(
'status' => 'approve',
'post_status' => 'publish',
'post_type' => 'product',
)
);
shuffle($comments);
$comments = array_slice( $comments, 0, 5 );
$html = '<ul>';
foreach( $comments as $comment ) :
$rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) );
$html .= '<li class="review">';
$html .= '<div>'.get_the_title( $comment->comment_post_ID ).'</div>';
if ( $rating > 4 ) $html .= wc_get_rating_html( $rating );
$html .= '<div>' .$comment->comment_content.'</div>';
$html .= "<div>Posted By :".$comment->comment_author." On ".$comment->comment_date. "</div>";
$html .= '</li>';
endforeach;
$html .= '</ul>';
ob_start();
echo $html;
$html = ob_get_contents();
ob_end_clean();
return $html;
}
add_shortcode('woo_reviews', 'get_woo_reviews');
But It also shows some comments without any rating.
How to change this code to show only 5 star rating reviews?
A "meta query" is required to the WP_Comment_Query based on "rating" meta key set to 5 stars meta value, to get random reviews that have 5 stars rating, as follows:
function get_random_five_stars_products_reviews( $atts ) {
// Extract shortcode attributes
extract( shortcode_atts( array(
'limit' => 5, // number of reviews to be displayed by default
), $atts, 'woo_reviews' ) );
$comments = get_comments( array(
'status' => 'approve',
'post_status' => 'publish',
'post_type' => 'product',
'meta_query' => array( array(
'key' => 'rating',
'value' => '5',
) ),
) );
shuffle($comments);
$comments = array_slice( $comments, 0, $limit );
$html = '<ul class="products-reviews">';
foreach( $comments as $comment ) {
$rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) );
$html .= '<li class="review">';
$html .= '<div>'.get_the_title( $comment->comment_post_ID ).'</div>';
if ( $rating > 4 ) $html .= wc_get_rating_html( $rating );
$html .= '<div>' .$comment->comment_content.'</div>';
$html .= "<div>Posted By :".$comment->comment_author." On ".$comment->comment_date. "</div>";
$html .= '</li>';
}
return $html . '</ul>';
}
add_shortcode('woo_reviews', 'get_random_five_stars_products_reviews');
Code goes in functions.php file of the active child theme (or active theme). Tested and works
USAGE: [woo_reviews] or [woo_reviews limit="3"] for 3 random reviews for example.

WordPress custom post type array not working as expcted

I'm using this code...
function include_post_types() {
$post_types = get_post_types( array (
'show_ui' => true
),
'objects' );
$return = '';
foreach ( $post_types as $post_type ) {
$my_sitemap_options = get_option( 'my_sitemap_settings' );
$post_type_name = $post_type->name;
if ($my_sitemap_options['post_types'] && in_array($post_type_name, $my_sitemap_options['post_types'])) {
$the_excluded = $post_type_name;
$return .= "'" . $the_excluded . "', ";
}
}
return $return;
}
... to return a list of custom post types that I have selected from an options page. This works fine, and if I do this...
echo included_post_types();
...I see this...
'clothes', 'shoes', 'jackets',
...which is what I excpected.
But the problem is when I try to use included_post_types() in a loop to only show posts with those post types, like this:
$sitemap_post_args = array(
'post_type' => array(included_post_types()),
'posts_per_page' => -1,
'orderby' =>'post_type',
'order' =>'asc'
);
$loop = new WP_Query($sitemap_post_args);
global $post_type;
global $post;
echo '<ul>';
$last_post_type = '';
while($loop->have_posts()): $loop->the_post();
$current_post_type = $post->post_type;
$current_post_type_object = get_post_type_object( $current_post_type );
if($current_post_type != $last_post_type) echo "<br /><li><strong>" . $current_post_type_object->labels->name . "</strong></li>";?>
<li><?php echo get_the_title(); ?></li>
<?php echo "\n";
$last_post_type = $current_post_type;
endwhile;
wp_reset_query();
echo '</ul>';
It simply doesn't display anything on my page, but it doesn't throw an error either.
I'm almost certain the problem is this line:
'post_type' => array(included_post_types()),
I even tried it like this...
'post_type' => included_post_types(),
...but it did not work.
If I try this...
'post_type' => array('clothes', 'shoes', 'jackets', ),
...it works, but I need to be able to use the function name.
Any suggestions would be helpful.
please replace below code with yours. It should be help.
function include_post_types() {
$post_types = get_post_types( array (
'show_ui' => true
),
'objects' );
$return = array();
foreach ( $post_types as $post_type ) {
$my_sitemap_options = get_option( 'my_sitemap_settings' );
$post_type_name = $post_type->name;
if ($my_sitemap_options['post_types'] && in_array($post_type_name, $my_sitemap_options['post_types'])) {
$the_excluded = $post_type_name;
$return[] = $the_excluded;
}
}
return $return;
}
and also replace below code that show posts
$post_type_list = include_post_types();
$sitemap_post_args = array(
'post_type' => $post_type_list,
'posts_per_page' => -1,
'orderby' =>'post_type',
'order' =>'asc'
);

How to add category option to shortcode

I'm new to creating wordpress shortcodes and have one just about working the way I want. Missing something specific. Currently I am able to put the following on any page - [children] and it pulls a query of all posts from the custom post type "children" I would like to add the option to add the category id within the shortcode - something like [children category="8"] Here is the code I have so far:
add_shortcode( 'children', 'display_custom_post_type' );
function display_custom_post_type(){
$args = array(
'post_type' => 'children',
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
);
$string = '';
$query = new WP_Query( $args );
if( $query->have_posts() ){
while( $query->have_posts() ){
$query->the_post();
$string .= '<div id="childWrapper"><div id="childImage">' . get_the_post_thumbnail() . '</div><div style="clear: both;"></div><div id="childName">' . get_the_title() . '</div><div style="clear: both;"></div></div>';
}
}
wp_reset_postdata();
return $string;
}
Secondary - is it possible for it to show posts in multiple categories, but only where the posts are in each of the categories. For example showing a list of children, who fall under a category for critical care and surgery needed.
Any help would be greatly appreciated.
You should refer Shortcode function from WordPress Codex. Assuming your taxonomy name is category, I made some changes in your current code it should work as per your requirements.
/**
* [children category="5,7,8"]
*/
add_shortcode( 'children' , 'display_custom_post_type' );
function display_custom_post_type($atts) {
$atts = shortcode_atts( array(
'category' => ''
), $atts );
//If category is multiple: 8,9,3
$categories = explode(',' , $atts['category']);
$args = array(
'post_type' => 'children',
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page'=> -1,
'tax_query' => array( array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => $categories
) )
);
$string = '';
$query = new WP_Query( $args );
if( ! $query->have_posts() ) {
return false;
}
while( $query->have_posts() ){
$query->the_post();
$string .= '<div id="childWrapper"><div id="childImage">' . get_the_post_thumbnail() . '</div><div style="clear: both;"></div><div id="childName">' . get_the_title() . '</div><div style="clear: both;"></div></div>';
}
wp_reset_postdata();
return $string;
}

shortcode for related post in wp

I’m using bellow function for related post shortcode and I have a question about it. It’s supposed to display posts with related tag (I don't why it shows just random posts) but I want to use something like [rps tag=google] and the function just return a post has a tag as “google”, I mean nothing related to the current post tags. How can I do that?
This is the code:
add_shortcode('rps', 'fphp_get_related_posts');
function fphp_get_related_posts() {
$reset_post = $post;
global $post;
$post_tags = wp_get_post_tags($post->ID);
if ($post_tags) {
$post_tag_ids = array();
foreach($post_tags as $post_tag) $post_tag_ids[] = $post_tag->term_id;
$args=array(
'tag__in' => $post_tag_ids,
'post__not_in' => array($post->ID),
'posts_per_page' => 1,
'orderby' => 'rand',
);
$related_query = new wp_query( $args );
if (intval($related_query->post_count) === 0) return '';
$html = '<div class="rps"><ul><h3>Also read:</h3>';
$related_query->the_post();
$html .= '<li style="width:250px">';
$html .= '<div class="relatedthumb"><a rel="external" href="'. get_the_permalink(). '">';
$html .= get_the_title() . '</a>';
$html .= '</div></li>';
}
$post = $reset_post;
wp_reset_query();
$html .= '</ul></div>';
return $html;
}
If you want to show given tag post, then no need to use wp_get_post_tags(), because its return multi tags. try this code,
function fphp_get_related_posts( $atts ) {
$atts = shortcode_atts( [
'tag' => '',
], $atts );
$args = [ 'posts_per_page' => 1, 'tag' => $atts['tag'], 'orderby' => 'rand' ];
$query = new WP_Query( $args );
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
ob_start(); ?>
<!-- your html here -->
<?php endwhile; endif; wp_reset_postdata();
return ob_get_clean();
}
add_shortcode( 'rps','fphp_get_related_posts' );

Categories