Community Forums › Forums › Archived Forums › Design Tips and Tricks › Write A Shortcode For The Following
- This topic has 2 replies, 1 voice, and was last updated 9 years, 10 months ago by Porter.
-
AuthorPosts
-
January 29, 2015 at 2:04 pm #138980PorterParticipant
On my front page, I have 4 widgetized areas, where I currently plug in a bit of manual html in a text widget to get my desired effect. The html I insert is as follows:
<a href="http://anightinburlington.com/articles"> <img src="http://anightinburlington.com/wordpress/wp-content/uploads/2014/12/IMG_1595-379x253.jpg" /> <h3>The Whiskey Room</h3> </a>
Basically I wrap the entire thing in a link to my content, display an image, and have an overlayed H3 element on top of the photo, done by my CSS classes. The annoying part here, is that I have to specifically supply the "medium" sized photo, which is a pain to acquire the url of manually (I had to add it to a post, then right click and get the url).
I'd like to shorten everything above into a single shortcode that I can simply use in my text widget. I've never made a shortcode before, but I doubt it's too complicated. Can anyone guide me on how to go about this?
Page - anightinburlington.com
January 29, 2015 at 2:54 pm #138985PorterParticipantAfter a bit of reading, I've got the gist of what I needed. This is what I have so far:
// Home page featured content shortcode function home_page_featured_shortcode($atts) { extract(shortcode_atts(array( 'link' => 'default-string', 'image' => 'image-url', 'text' => 'featured-title', ), $atts)); return '<a href="' . $link . '"> <img src="' . $image . '" /> <h3>' . $text . '</h3> </a>'; } add_shortcode( 'home_featured', 'home_page_featured_shortcode' );
My only issue is:
-What is the syntax for the default string values? 'string' or simply string?
All in all, does this seem correct for what I'm trying to do?
EDIT - fixed the parsing issue.
January 29, 2015 at 7:07 pm #139001PorterParticipantFinally got it! This creates the effect seen on my front page, and even grabs the "medium" sized image to optimize load times.
// Home page featured content shortcode function home_page_featured_shortcode($atts) { extract(shortcode_atts(array( 'link' => 'http://anightinburlington.com', 'image' => 'http://anightinburlington.com/wordpress/wp-content/uploads/2014/12/IMG_1587.jpg', 'text' => 'Featured Title', ), $atts)); // Convert the image url given to the ID, then use that to obtain the medium size of the image. $imageID = url_to_postid($image); $mediumImage = wp_get_attachment_image_src($imageID, 'medium'); return '<a href="'.$link.'"><img src="'.$mediumImage[0] . '" /><h3>'.$text.'</h3></a>'; } add_shortcode( 'home_featured', 'home_page_featured_shortcode' );
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.