1

When there is a specific shortcode in a post (example [sc]anything[/sc]), I want to hide the featured and take it's place. How can I do that?

I add this on my theme function

// TopWide Shortcode
function wpsite_topwide( $atts, $content = null ){
    return '<div class="topwide">' . do_shortcode($content) . '</div>';
}
add_shortcode( 'topwide', 'wpsite_topwide' );

/** 
 * Find if content has topwide shortcode in it. 
 */ 
function wpsite_has_topwide_shortcode() { 
    global $post; 

    if ( ! is_single() ) 
        return; 

    if ( false === strpos( $post->post_content, '[topwide' ) ) 
        return false; 

    return true; 
}

and this on single post

<?php
if (wpsite_has_topwide_shortcode() && has_post_thumbnail() ) { 
    echo '<div class="topwide">' . get_the_post_thumbnail( $post->ID, 'full' ) . '</div>'; 
} 
?>

But doesn't takes the place of the featured image in the post...

2
  • I think the shortcode detection part has a solution here, try to dig in the archives
    – brasofilo
    Commented Mar 15, 2013 at 18:26
  • @brasofilo I tried, but I think something I have done wrong, check plz if you can help me
    – Alex
    Commented Mar 15, 2013 at 20:45

1 Answer 1

2

Apparently you are simply not translating your logic into code:

  • if it has shortcode, do this,
  • if not has shortcode but has thumbnail, do that,
  • otherwise, do something else.
if ( wpsite_has_topwide_shortcode() ) { 
    echo 'shortcode';
} elseif( has_post_thumbnail() ) {
    echo get_the_post_thumbnail( $post->ID, 'full' );
} else {
    echo 'Hello World';
}
10
  • this: <?php if ( wpsite_has_topwide_shortcode() ) { get_shortcode(); } elseif( has_post_thumbnail() ) { get_the_post_thumbnail( $post->ID, 'full' ) } ?> ?
    – Alex
    Commented Mar 15, 2013 at 21:39
  • Did you try it? :) - But, yes, this is it.
    – brasofilo
    Commented Mar 15, 2013 at 21:45
  • yes, i get this: Parse error: syntax error, unexpected '}' in /home/xx/public_html/wp-content/themes/xxxx/content-single.php on line 71
    – Alex
    Commented Mar 15, 2013 at 21:46
  • You have a missing semi-colon ; in get_the_post_thumb. A good IDE would have shown that, eg, NetBeans.
    – brasofilo
    Commented Mar 15, 2013 at 21:55
  • now i get unexpected ';' :/
    – Alex
    Commented Mar 15, 2013 at 22:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.