• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 534
  • Last Modified:

How do I modify a shortcode in wordpress

I have a theme that has a "featured news" section that displays posts near the top of my home page.  However, it currently shows an excerpt of the posts text, and I want to change that to the posts title.  The shortcode doesnt seem to accept title as a parameter, so I want to add it, and i'm not sure where the function resides.  The shortcode currently looks like this:

[carousel title="The Chief Speaks" num="8" type="blog" thumb="true" thumb_width="150" thumb_height="90" category="basketball" excerpt_count="20" date="no" author="no" min_items="1" spacer="0" custom_class="featured-news"]

Open in new window

0
bmsande
Asked:
bmsande
  • 3
  • 3
  • 2
2 Solutions
 
Chris StanyonCommented:
The code for the shortcode should be in the functions.php file of your WordPress theme. You will see something like:

add_shortcode('carousel', 'functionToRun'); 

Open in new window

functionToRun is the function that gets called when you insert your shortcode, and that's where you'll need to be looking.
0
 
bmsandeAuthor Commented:
I found the function...now how would I go about adding "post title" as an attribute....

<?php

/**

 * Carousel

 *

 */

if (!function_exists('shortcode_carousel')) {



	function shortcode_carousel($atts, $content = null) {

			extract(shortcode_atts(array(

				'title' => '',

				'num' => '8',

				'type' => '',

				'thumb' => 'true',

				'thumb_width' => '220',

				'thumb_height' => '180',

				'more_text_single' => 'read more',

				'category' => '',

				'custom_category' => '',

				'excerpt_count' => '12',

				'date' => '',

				'author' => '',			

				'min_items' => '3',

				'spacer'	=> '18',

				'custom_class' => ''

			), $atts));



			$template_url = get_stylesheet_directory_uri();

			

			// check what type of post user selected

			switch ($type) {

			   	case 'blog':

					$type_post = '';

					break;

			   	case 'portfolio':

					$type_post = 'portfolio';

					break;

				case 'testimonial':

					$type_post = 'testi';

					break;

			}		



			$output = '<div class="carousel-wrap '.$custom_class.'">';

			if ($title != '') {

				$output .= '<h2>'.$title.'</h2>';

			}

			$output .= '<div id="carousel-'. $type .'" class="es-carousel-wrapper">';

			$output .= '<div class="es-carousel">';

			$output .= '<ul class="es-carousel_list unstyled">';

			

			global $post;

			global $my_string_limit_words;

			

			$args = array(

				'post_type' => $type_post,

				'category_name' => $category,

				$type_post . '_category' => $custom_category,

				'numberposts' => $num,

				'orderby' => 'post_date',

				'order' => 'DESC'

			);



			$latest = get_posts($args);

			$i = 0;

			

			foreach($latest as $post) {

				setup_postdata($post);

				$excerpt = get_the_excerpt();

				$format = get_post_format();

				$attachment_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );

				$url = $attachment_url['0'];

				$image = aq_resize($url, $thumb_width, $thumb_height, true);

				$link_format_url =  get_post_meta(get_the_ID(), 'tz_link_url', true);		



				$output .= '<li class="es-carousel_li '.$format.'">';				

					

					if ($thumb == 'true') {

						if (has_post_thumbnail($post->ID) && $format == 'image') {

												

							$prettyType = 'prettyPhoto';				

							$output .= '<figure class="featured-thumbnail">';

							$output .= '<a href="'.get_permalink($post->ID).'" title="'.get_the_title($post->ID).'">';

							$output .= '<img  src="'.$image.'" alt="'.get_the_title($post->ID).'" />';

							$output .= '</a></figure>';



						} elseif ( $format != 'video' && $format != 'audio') {						



							$thumbid = 0;

							$thumbid = get_post_thumbnail_id($post->ID);

							$images = get_children( array(

								'orderby' => 'menu_order',

								'order' => 'ASC',

								'post_type' => 'attachment',

								'post_parent' => $post->ID,

								'post_mime_type' => 'image',

								'post_status' => null,

								'numberposts' => -1

							) ); 



							if ( $images ) {



								$k = 0;

								//looping through the images

								foreach ( $images as $attachment_id => $attachment ) {					

									//if( $attachment->ID == $thumbid ) continue;



									$image_attributes = wp_get_attachment_image_src( $attachment_id, 'full' ); // returns an array

									$img = aq_resize($image_attributes[0], $thumb_width, $thumb_height, true);  //resize & crop img

									$alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);

									$image_title = $attachment->post_title;



									if ( $k == 0 ) {

										$output .= '<figure class="featured-thumbnail">';

										$output .= '<a href="'.get_permalink($post->ID).'" title="'.get_the_title($post->ID).'">';

										$output .= '<img src="'.$img.'" alt="'.get_the_title($post->ID).'" />';

									} else {

										$output .= '<figure class="featured-thumbnail" style="display:none;">';

										$output .= '<a href="'.get_permalink($post->ID).'" title="'.get_the_title($post->ID).'">';

										$output .= '<img src="'.$img.'" alt="'.get_the_title($post->ID).'" />';

									}

									$output .= '</a></figure>';

									$k++;

								}					

							} elseif (has_post_thumbnail($post->ID)) {

								$output .= '<figure class="featured-thumbnail">';

								$output .= '<a href="'.get_permalink($post->ID).'" title="'.get_the_title($post->ID).'">';

								$output .= '<img src="'.$image.'" alt="'.get_the_title($post->ID).'" />';

								$output .= '</a></figure>';

							} /*else {

								// empty_featured_thumb.gif - for post without featured thumbnail

								$output .= '<figure class="featured-thumbnail">';

								$output .= '<a href="'.get_permalink($post->ID).'" title="'.get_the_title($post->ID).'">';

								$output .= '<img  src="'.$template_url.'/images/empty_thumb.gif" alt="'.get_the_title($post->ID).'" />';

								$output .= '</a></figure>';							

							}*/

						} else {

							if (has_post_thumbnail($post->ID)) {

								// for Video and Audio post format - no lightbox

								$output .= '<figure class="featured-thumbnail"><a href="'.get_permalink($post->ID).'" title="'.get_the_title($post->ID).'">';

								$output .= '<img  src="'.$image.'" alt="'.get_the_title($post->ID).'" />';

								$output .= '</a></figure>';

							} /*else {

								// empty_featured_thumb.gif - for post without featured thumbnail

								$output .= '<figure class="featured-thumbnail">';

								$output .= '<a href="'.get_permalink($post->ID).'" title="'.get_the_title($post->ID).'">';

								$output .= '<img  src="'.$template_url.'/images/empty_thumb.gif" alt="'.get_the_title($post->ID).'" />';

								$output .= '</a></figure>';

							}*/

						}

					}



					$output .= '<div class="desc">';

					if ($date == "yes") {

						$output .= '<time datetime="'.get_the_time('Y-m-d\TH:i:s', $post->ID).'">' .get_the_time('M', $post->ID). ' <span>'.get_the_time('d', $post->ID).'</span></time>';

					}				



					if ($author == "yes") {

						$output .= '<em class="author">, by <a href="'.get_author_posts_url(get_the_author_meta( 'ID' )).'">'.get_the_author_meta('display_name').'</a></em>';

					}

					

					//Link format

					if ($format == "link") {

						$output .= '<h5><a href="'.$link_format_url.'" title="'.get_the_title($post->ID).'">';

						$output .= get_the_title($post->ID);

						$output .= '</a></h5>';



					//Other formats

					} else {

						$output .= '<h5><a href="'.get_permalink($post->ID).'" title="'.get_the_title($post->ID).'">';

						$output .= get_the_title($post->ID);

						$output .= '</a></h5>';

					}		

					

					if($excerpt_count >= 1){

						$output .= '<p class="excerpt">';

						$output .= my_string_limit_words($excerpt,$excerpt_count);

						$output .= '</p>';

					}

					

					if($more_text_single!=""){

						$output .= '<a href="'.get_permalink($post->ID).'" class="btn btn-primary" title="'.get_the_title($post->ID).'">';

						$output .= $more_text_single;

						$output .= '</a>';

					}

					$output .= '</div>';

					

				$output .= '</li>';



			}

			$output .= '</ul>';

	       	$output .= '</div></div>';

		   

			$output .= '<script>

					jQuery("#carousel-'. $type .'").elastislide({

						imageW 		: '.$thumb_width.',

						minItems	: '.$min_items.',

						speed		: 600,

						easing		: "easeOutQuart",

						margin		: '.$spacer.',

						border		: 0,

						onClick		: function() {}

					});';

			$output .= '</script>';

			

	       	$output .= '</div>';

			return $output;

	}

	add_shortcode('carousel', 'shortcode_carousel');

	

}?>

Open in new window

0
 
Jason C. LevineNo oneCommented:
The PHP that controls the excerpt output starts at line 345 and ends around line 371.  Make your changes there or comment out the structures to remove the excerpt entirely.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
bmsandeAuthor Commented:
I can remove the excerpt easily by disabling it in my shortcode within the post.  I need help adding an additional parameter to my shortcode (post title).
0
 
Jason C. LevineNo oneCommented:
All of the code you need is right around those lines:
//Other formats

	} else {
		$output .= '<h5><a href="'.get_permalink($post->ID).'" title="'.get_the_title($post->ID).'">';
		$output .= get_the_title($post->ID); // this line adds the post title to $output
		$output .= '</a></h5>';
	}		

	if($excerpt_count >= 1){
		$output .= '<p class="excerpt">'; // this line starts the excerpt
		$output .= my_string_limit_words($excerpt,$excerpt_count);
		$output .= '</p>';

	}

	if($more_text_single!=""){
		$output .= '<a href="'.get_permalink($post->ID).'" class="btn btn-primary" title="'.get_the_title($post->ID).'">';
		$output .= $more_text_single;
		$output .= '</a>';
	}

Open in new window


Look for my comments above.  You should be able to handle it from there...
0
 
Chris StanyonCommented:
The post title won't be part of the shortcode - the shortcode is designed to get the latest posts from the database, so will pull that info automatically.

As Jason says, the code that displays the excerpt is line 347-351:

$output .= '<p class="excerpt">';
$output .= my_string_limit_words($excerpt,$excerpt_count);
$output .= '</p>';

Open in new window

If you want to output the Post title instead, change it to something like:

$output .= '<p class="excerpt">';
$output .= get_the_title($post->ID);
$output .= '</p>';

Open in new window

It;'s up to you how you format the output, but make a backup of your functions.php file first :)
0
 
bmsandeAuthor Commented:
Ah.  I was hoping to add an additional attribute instead of modifying an existing one, but that is probably the quickest solution.  Accepted!
0
 
Chris StanyonCommented:
Your original question said that you want to change the excerpt for the title, so that's what we've given you. If you want to add the title and keep the excerpt as well, then just add to the code, rather than replacing it:

$output .= '<h2 class="title">';
$output .= get_the_title($post->ID);
$output .= '</h2>';
$output .= '<p class="excerpt">';
$output .= my_string_limit_words($excerpt,$excerpt_count);
$output .= '</p>';

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now