Solved

How do I modify a shortcode in wordpress

Posted on 2013-11-07
8
520 Views
Last Modified: 2013-11-07
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
Comment
Question by:bmsande
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39630736
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
 

Author Comment

by:bmsande
ID: 39630833
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
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 39630915
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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 

Author Comment

by:bmsande
ID: 39631023
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
 
LVL 70

Assisted Solution

by:Jason C. Levine
Jason C. Levine earned 250 total points
ID: 39631036
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
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 250 total points
ID: 39631038
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
 

Author Closing Comment

by:bmsande
ID: 39631121
Ah.  I was hoping to add an additional attribute instead of modifying an existing one, but that is probably the quickest solution.  Accepted!
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39631146
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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
This article discusses how to implement server side field validation and display customized error messages to the client.
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question