WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to strip shortcodes if on index.php? (8 posts)

  1. Drew Baker
    Member
    Posted 4 years ago #

    Hey Guys,
    I'm building a CMS type site here and would like to make it so that if you view index.php (or home.php) then all the shortcodes are striped from the posts (the_content). When viewing another page like single.php then the shortcodes should be left along.

    I think I need to use this code, but I'm not sure how:
    <?php strip_shortcodes( $content ); ?>

    This is the Codex page for it.

    The reason for doing this is that my main page (home.php and index.php) get messed up when a Viper Video or Gallery shortcode get used, but obviously I want them to work when viewing them on single.php.

    Thanks for the help!

  2. MichaelH
    Member
    Posted 4 years ago #

    Going to guess you would need something similar to:

    function remove_shortcode_from_index($content) {
      if ( is_home() ) {
        $content = strip_shortcodes( $content );
      }
      return $content;
    }
    add_filter('the_content', 'remove_shortcode_from_index');
  3. Drew Baker
    Member
    Posted 4 years ago #

    This example is perfect! Thanks Michael! Is there a way to make it work for the_content tags that are in a certain DIV perhaps?

    It's that now that I have this working I released I have a part on my index.php page that shows a Video Highlight and that uses shortcodes to display a Viper Video. Here is the code I use to pull the latest post to show the video:

    <?php $my_query = new WP_Query('cat=1532&showposts=1');
    while ($my_query->have_posts()) : $my_query->the_post();
    $do_not_duplicate = $post->ID;?>
    
    <?php the_content(''); ?>
    
    <?php endwhile; ?>

    Thanks again! I'm going to add that code as an example on the Codex for strip_shortcodes.

  4. Drew Baker
    Member
    Posted 4 years ago #

    Michael, looking through the codex page for conditional tags, it's seems I need to combine the above with something using the is_category('Video Highlight') tag, but I'm not sure what the correct PHP code should be (I suck at PHP).

    Thanks again!

  5. MichaelH
    Member
    Posted 4 years ago #

    dbaker - okay I put that example in the Codex Function_Reference/strip_shortcodes#Examples. Thanks for the suggestion.

    Also note you may want to keep an eye on this trac ticket if you have problems:
    http://core.trac.wordpress.org/ticket/10082

    Finally, see Conditional Tags related to your last comment.

  6. Drew Baker
    Member
    Posted 4 years ago #

    Hey Micheal,
    Thanks so much for your detailed response.

    I think the below code is the correct code for going through my home.php file, and stripping all shortcodes except for the posts that are in cat=438 (my video highlight category). But it gives a syntax error saying the 'else' line is bad. Like I said, my PHP sucks so any suggestions are greatly appreciated.

    function remove_shortcode_from_index($content) {
      if ( in_category('438') ) {
        $content = $content;
      }
      return $content;
    }
    
    else ( is_home() ) {
        $content = strip_shortcodes( $content );
      }
      return $content;
    }
    add_filter('the_content', 'remove_shortcode_from_index');

    Thanks!

  7. MichaelH
    Member
    Posted 4 years ago #

    If 'home' and not in category 438

    function remove_shortcode_from_index($content) {
      if ( is_home() && !in_category('438') ) {
        $content = strip_shortcodes( $content );
      }
      return $content;
    }
    add_filter('the_content', 'remove_shortcode_from_index');
  8. Drew Baker
    Member
    Posted 4 years ago #

    Michael,
    That's amazing. I really have to learn PHP, you can do so much with it!

    Thanks a lot!
    DB

Topic Closed

This topic has been closed to new replies.

About this Topic