• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

Write A Shortcode For The Following

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Archived Forums › Design Tips and Tricks › Write A Shortcode For The Following

This topic is: resolved

Tagged: image, medium, shortcode, text, widget

  • This topic has 2 replies, 1 voice, and was last updated 10 years, 10 months ago by Porter.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • January 29, 2015 at 2:04 pm #138980
    Porter
    Participant

    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


    Buy me a beer? | Try DigitalOcean VPS Hosting

    January 29, 2015 at 2:54 pm #138985
    Porter
    Participant

    After 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.


    Buy me a beer? | Try DigitalOcean VPS Hosting

    January 29, 2015 at 7:07 pm #139001
    Porter
    Participant

    Finally 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' );

    Buy me a beer? | Try DigitalOcean VPS Hosting

  • Author
    Posts
Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘Design Tips and Tricks’ is closed to new topics and replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2025 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble