Include excerpt with shortcode - wordpress

Working with a plugin that provides a shortcode with only a couple of parameters. https://wordpress.org/plugins/radio-station I'm using the [list-shows] shortcode. I would like to also pull in an excerpt of the content, I imagine that would be applicable to a lot of situations and improve my understanding of shortcodes.
I was able to add in the thumbnail to the output, but not the content, which seems to be referred to in the atts of the other shortcodes as 'show_desc'. You can see the output on this page: http://www.kzmuradio.org/kzmu-programs/
Here is the code for the shortcode. Apologies for my ignorance. Learning curve.
/* Shortcode for displaying a list of all shows * Since 2.0.0*/
function station_shortcode_list_shows($atts) {
extract( shortcode_atts( array(
'genre' => '',
'show_desc' => 1
), $atts ) );
//grab the published shows
$args = array(
'numberposts' => -1,
'offset' => 0,
'orderby' => 'title',
'order' => 'ASC',
'post_type' => 'show',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'show_active',
'value' => 'on',
)
)
);
if($genre != '') {
$args['genres'] = $genre;
}
$shows = get_posts($args);
//if there are no shows saved, return nothing
if(!$shows) {
return false;
}
$output = '';
$output .= '<div id="station-show-list">';
$output .= '<ul>';
foreach($shows as $show) {
$output .= '<li>' . get_the_post_thumbnail($show->ID, 'thumbnail') . ''.get_the_title($show->ID).'</li>';
$output .= '</li>';
}
$output .= '</ul>';
$output .= '</div>';
return $output;

You can access all the data through $show in your foreach-loop. To see what they contain, you can use print_r():
print_r( $show );
For excerpt and content you can use this:
echo $show->post_excerpt;
echo wpautop( $show->post_content );
See also: https://codex.wordpress.org/Function_Reference/$post

Related

wp_query in shortcode not working as expected

I created a simple shortcode
add_shortcode('lichthidau', 'hp_lich_thi_dau');
function hp_lich_thi_dau( $atts ) {
$output = '';
extract( shortcode_atts( array( 'posttype' => 'lich' ), $atts) );
$args = array(
'post_type' => $posttype,
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
$itemprop = '';
if ( 'microdata' === generate_get_schema_type() ) {
$itemprop = ' itemprop="text"';
}
echo '<div class="bang-lich">';
//while ( $the_query->have_posts() ) {
$output = $the_query->found_posts;
//}
echo '</div>';
}
wp_reset_postdata();
return $output;
}
Then put it in Gutenberg shortcode block [lichthidau] in a page (ID = 106, for example).
Without while loop, it's showing 2, which is the count of returning posts, and it's correct. However, if I enable while loop, it's taking the current page ID (106), and creating unlimited loops, while the expected result should be only two number 2.
Can anyone advice why and how to fix, please?
Thanks.
The first problem is that you're using echo in the shortcode output. The shortcode can only return content, and echo will produce unexpected results.
The second problem is trying to output the $output = $the_query->found_posts; within your loop. If you return something else, it will work.
This returns your loop with the post titles.
add_shortcode( 'lichthidau', 'hp_lich_thi_dau' );
function hp_lich_thi_dau( $atts ) {
$output = '';
extract( shortcode_atts( array( 'posttype' => 'lich' ), $atts ) );
$args = array(
'post_type' => $posttype,
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
$itemprop = '';
if ( 'microdata' === generate_get_schema_type() ) {
$itemprop = ' itemprop="text"';
}
$output = '<div class="bang-lich">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$output .= get_the_title( get_the_ID() ) . '</br>';
}
$output .= '</div>';
}
wp_reset_postdata();
return $output;
}

wordpress get all post from custom post type that date field is bigger than today

I don't understand why I get all posts and not only the one I want.
I need all posts of the current user that log in and that have the 'date_end' field (acf) is bigger than today.
with this code I get all the post of the user it's like it doesn't read the second array of the conditions :(
this is my code:
$user_id = get_current_user_id();
$today = date("m.d.Y");
$meta_query_args = array(
array(
'key' => 'user_id',
'value' => $user_id,
),
array(
'key' => 'date_end',
'value' => $today,
'compare' => '>'
),
);
$args = [
'posts_per_page' => -1,
'post_type' => 'trip',
'meta_query' => $meta_query_args
];
$TripQuery = get_posts( $args );
if( ! empty( $TripQuery ) ){
foreach( $TripQuery as $TripPost ){
echo '<div name="CurrentTripList">';
echo '<a href="' . $TripPost->ID . '" >' . the_field('date_end',$TripPost->ID) . '</a>';
echo '</div>';
}
}
can some one please help me. I don't understand what I'm doing wrong :(

How do you include custom field in Shortcode output?

with help from another developer, I have created the following for a custom shortcode:
add_shortcode( 'children' , 'display_custom_post_type_v1' );
function display_custom_post_type_v1($atts) {
$atts = shortcode_atts( array(
'category' => ''
), $atts );
$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',
'operator' => 'AND',
'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;
}
So far this works really well for my application, but there is one additional item I would like to include. These custom post types include meta boxes (custom fields) and I would like to include this in the output. Typically I use something like:
<?php echo get_post_meta( $post->ID, '_cd_birthdate', true ); ?>
But I can't figure out how to include it in the output of the shortcode. I've tried this line, but only the div area shows up, but no content:
<div>' . get_post_meta( $post->ID, '_cd_birthdate', true ) . '</div>
Any suggestions would be greatly appreciated.
I think '$post->ID,' only works when in the WP loop. Try 'get_the_ID()' instead.
Like...
<div>' . get_post_meta( get_the_ID(), '_cd_birthdate', true ) . '</div>
See this

Displaying Image Caption at Functions.php in Wordpress

in our wordpress site we modified image gallery style with using theme functions. We changed div classes, ids to use jquery image slider.
To make this we used this codes on functions.php
add_filter('post_gallery', 'ct_post_gallery', 10, 2);
function ct_post_gallery($output, $attr) {
global $post;
if (isset($attr['orderby'])) {
$attr['orderby'] = sanitize_sql_orderby($attr['orderby']);
if (!$attr['orderby'])
unset($attr['orderby']);
}
extract(shortcode_atts(array(
'order' => 'ASC',
'orderby' => 'menu_order ID',
'id' => $post->ID,
'itemtag' => 'dl',
'icontag' => 'dt',
'captiontag' => 'dd',
'columns' => 3,
'size' => 'thumbnail',
'include' => '',
'exclude' => ''
), $attr));
$id = intval($id);
if ('RAND' == $order) $orderby = 'none';
if (!empty($include)) {
$include = preg_replace('/[^0-9,]+/', '', $include);
$_attachments = get_posts(array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby));
$attachments = array();
foreach ($_attachments as $key => $val) {
$attachments[$val->ID] = $_attachments[$key];
}
}
if (empty($attachments)) return '';
// Here's your actual output, you may customize it to your need
$output = "<div id=\"icmimarlikreferans\">\n";
$output .= "<ul id=\"icmimarlikreferansimg\">\n";
// Now you loop through each attachment
foreach ($attachments as $id => $attachment) {
// Fetch the thumbnail (or full image, it's up to you)
// $img = wp_get_attachment_image_src($id, 'medium');
// $img = wp_get_attachment_image_src($id, 'my-custom-image-size');
$img = wp_get_attachment_image_src($id, 'full');
$output .= "<li class=\"item\">\n";
$output .= "<img src=\"{$img[0]}\" width=\"{$img[1]}\" height=\"{$img[2]}\" alt=\"\" />\n";
$output .= "</li>\n";
}
$output .= "</ul>\n";
$output .= "</div>\n";
return $output;
}
When we use it without making edits WordPress displaying image captions in single post pages at bottom of images in galleries . But when we use this code at functions.php captions are not displaying. We know the caption codes are missing, in this code block what we are using...
We tried to add caption functions in this codes but we have failed.
We need to display image captions at bottom of images in galleries.
We will love you if you help.

How to use the visual composer attach_images as in a shortcode

I am trying to create a custom image slider using visual composers attach_images but cant quite work out how to get the URLs from the array of image IDs.
Any help would be appreciated.
var_dump($bg_images) returns
string(9) "19,6,1692"
vc_map( array(
"name" => __( "Fading Background Block", "farrat_vc" ),
"base" => "block_background",
"class" => "",
"category" => __( "Farrat Shortcodes", "farrat_vc"),
"as_parent" => array('except' => 'farrat_panel'), // Use only|except attributes to limit child shortcodes (separate multiple values with comma)
"content_element" => true,
"show_settings_on_create" => true,
"is_container" => true,
'admin_enqueue_css' => array(get_template_directory_uri().'/wp-content/themes/unite/inc/css/gallery.css'),
"params" => array(
array(
"type" => "attach_images",
"heading" => __( "Backgroud Images", "farrat_vc" ),
"param_name" => "bg_images",
),
),
"js_view" => 'VcColumnView'
) );
// Hi try this one this is perfectly working
//Param Registration
function designas_partners() {
// Title
vc_map(
array(
'name' => __( 'Clients' ),
'base' => 'designas_partners_content',
'category' => __( 'Easy Component' ),
'params' => array(
array(
"type" => "attach_images",
"heading" => esc_html__( "Add Clients Images", "appcastle-core" ),
"description" => esc_html__( "Add Clients Images", "appcastle-core" ),
"param_name" => "screenshots",
"value" => "",
),
)
)
);
}
add_action( 'vc_before_init', 'designas_partners' );
// Short code
function designas_partners_content_function( $atts, $content ) {
$gallery = shortcode_atts(
array(
'screenshots' => 'screenshots',
), $atts );
$image_ids = explode(',',$gallery['screenshots']);
$return = '
<div class="clients">';
foreach( $image_ids as $image_id ){
$images = wp_get_attachment_image_src( $image_id, 'company_logo' );
$return .='<div class="images"><img src="'.$images[0].'" alt="'.$atts['title'].'"></div>';
$images++;
}
$return .='</div>';
return $return;
}
add_shortcode( 'designas_partners_content', 'designas_partners_content_function' )
I got a neet workaround for this one on the loop, theres no need for the counter, loop over wp_get_attachment_image( $image_id, 'full' ); will get you every information u use on the wordpress panel.
I'll thank to #sushovan bhowmik was looking for this, I think this will help to avoid lots of variables calling the images :)
<?php
function vc_gallery_carrousel($atts, $content) {
// Attributes
$gallery = shortcode_atts(
array(
'carrousel_images' => 'carrousel_images',
),
$atts );
// Attributes in var
$image_ids=explode(',',$gallery['carrousel_images']);
// Output Code
$output .= '<section class="loopclientsimg">';
$output .= '<article>';
$output .= '<div>';
$output .= '<ul>';
foreach( $image_ids as $image_id ){
$output .='<li>';
$output .= wp_get_attachment_image( $image_id, 'full' );
$output .='</li>';
}
$output .= '</ul>';
$output .= '</div>';
$output .= '</article>';
$output .= '</section>';
return $output;
}
add_shortcode( 'vc_img_carrousel', 'vc_gallery_carrousel' );

Resources