• Resolved OffWorld

    (@offworld)


    Ok, I’ve looked and looked and I’m finally just going to ask. Is there any way, in a loop, to show ONLY the shortcode content (preferably the content inside a specific shortcode)?

    It’s dead easy to strip shortcodes out or selectively show and hide them, but what about the other way around – where the main content is hidden and the only thing displayed is whatever is inside a shortcode?

    I’m guessing there might be some filter magic that can do this, but I can’t figure it out.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter OffWorld

    (@offworld)

    That was the first thing I tried, figuring it’s purpose is to JUST “do the shortcode” but I’ve tried code based on the examples on that page and they just seem to filter out all the post content, including the stuff inside the shortcode.

    The only real-world examples I’ve found so far are about using shortcodes in widgets and sidebars. This is being used in a custom post_type where I have the shortcode working, I just want to get rid of anything in the_content() that isn’t the shortcode.

    You are working with the template file(s), yes?

    Thread Starter OffWorld

    (@offworld)

    No, it’s not a page template. It’s a single-custom.php custom post_type.

    do_shortcode() – unless I’m misunderstanding – seems to be when you want to create an instance of a shortcode somewhere it normally wouldn’t appear. But the shortcodes I’m dealing with already exist in the post and already normally appear with the unfiltered the_content().

    I’m kinda new to PHP and WP plugins so I haven’t fully wrapped my head around add_filter and apply_filters. I feel like those are probably what I want, I just don’t know how to get the output I want from them.

    Using the add_filter example on the do_shortcode codex page just spits back a number “1” and a suggestion I found somewhere online of echoing apply_filters(‘the_content’,'[myshortcode]’) gives me the shortcode wrapper HTML, but none of the $content in it, none of the $atts, and only one instance (even though the custom post has multiple instances).

    I wouldn’t think filtering the post to just display the content and atts of a shortcode would be this difficult!

    Thread Starter OffWorld

    (@offworld)

    Ok, finally figured out something that does exactly what I want it to do. Not sure this is the “best” way, but I’ll use it since it works. Inside my loop:

    $pattern = get_shortcode_regex();
    		preg_match_all( '/'. $pattern .'/s', $post->post_content, $matches );
    
    		if( is_array( $matches ) && array_key_exists( 2, $matches ) && in_array( 'myshortcode', $matches[2] ) )
    		{
    			foreach ($matches[0] as $value) {
    				$value = wpautop( $value, true );
    				echo do_shortcode($value);
    			}
    		} else {
    			// Do Nothing
    		}

    So I regex the post content looking for my shortcode, if it doesn’t find any instances it does nothing, if it does for each instance I grab the value of it, run it through the wpautop filter (to clean it up), then reconstruct the shortcode on the fly with do_shortcode().

    Found a discussion on a similar case here. Some of the examples might be helpful.

    Thread Starter OffWorld

    (@offworld)

    yep, the regex part is pretty much an identical case. Except the content in my shortcode doesn’t require I enqueue any scripts, so I was able to put my code right inside the loop within my single-custom.php file. Though I believe I read somewhere that WP 3.3 now allows you to enqueue scripts mid-page as well.

    I’m just glad I got it to finally work so I can move on to the next part I need to fix!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Show ONLY shortcode content’ is closed to new replies.