Cyber Week Sale! Get $20 off GP Premium, $40 off our new Lifetime license, and 45% off license renewals/extensions! Learn more

[Resolved] Remove short codes from the posts in a specific category

Home Forums Support Remove short codes from the posts in a specific category

  • This topic has 12 replies, 3 voices, and was last updated 3 years ago by Leo.
Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #415425
    Surya

    Hi,

    I have a website with short code for google maps in all the posts.

    However, I need to stop the short code from showing in posts listed under specific category.

    Category ID : 1
    Category name : USA Attorneys
    Category slug : usa-attorneys

    Can you please suggest a conditional command to strip the shortcodes from the posts listed under USA Attorneys from displaying while retaining the short codes in posts.

    I tried the code

    function remove_shortcodes( $content ) {
    if ( is_category(1) ) {
    $content = strip_shortcodes( $content );
    }
    return $content;
    }
    add_filter( ‘the_content’, ‘remove_shortcodes’ );

    but, no use.

    May please advise.

    #415648
    Leo
    Staff
    Customer Support

    Hi there,

    Hmm you can use ! to exclude a certain category when you are adding code initially:
    if ( ! is_singular ( 'usa-attorneys' ) && ! in_category ( 'usa-attorneys' ) )

    #415678
    Surya

    Hi,

    I inserted the following code in functions.php, but does not change anything.

    function remove_shortcodes( $content ) {
    if ( ! is_singular ( ‘usa-attorneys’ ) && ! in_category ( ‘usa-attorneys’ ) ) {
    $content = strip_shortcodes( $content );
    }
    return $content;
    }
    add_filter( ‘the_content’, ‘remove_shortcodes’ );

    #415769
    Leo
    Staff
    Customer Support

    You shouldn’t have to remove it – just don’t add it in to that category to start with.

    How and where are you adding the shortcode currently?

    #416381
    Surya

    Hi,

    I have already created a number of posts with shortcodes for Google maps under this category.

    Now, I am trying to strip the shortcodes from the posts belonging to this specific category.

    I was able to successfully strip shortcodes from archive pages like index, categories & tags with the code I have mentioned in the first thread but not in the posts belonging to a specific category.

    #416434
    Tom
    Lead Developer
    Lead Developer

    Instead of is_category(1), try in_category(1).

    #416590
    Surya

    Hi,

    It’s not working.

    #416612
    Tom
    Lead Developer
    Lead Developer

    Can you show me your exact code?

    #416644
    Surya

    Hi,

    The sample short code is

    [flexiblemap address=”711 Gaffney Rd Suite 202, Fairbanks, AK-99701″ title=”Zimmerman & Wallace Attorneys At Law” description=”Estate Planning Attorney, Personal Injury Lawyer, Real Estate Attorney” width=”100%” directions=”true”]

    #416767
    Leo
    Staff
    Customer Support

    Can you show us the full code? not just the short code part.

    Thanks!

    #419683
    Leo
    Staff
    Customer Support

    I don’t see the part where you used in_category(1) as Tom suggested?

    Also please reply here. Thanks!

    #421787
    Surya

    Hi,

    I added the below code in the beginning of single.php before header and got the desired result of stripping short codes from posts in category id 1.

    function remove_shortcodes( $content ) {
    if ( in_category(1) ) {
    $content = strip_shortcodes( $content );
    }
    return $content;
    }
    add_filter( ‘the_content’, ‘remove_shortcodes’ );
    
    The entire single.php is as below.
    
    <?php
    /**
     * The Template for displaying all single posts.
     *
     * @package GeneratePress
     */
    
    function remove_shortcodes( $content ) {
      if ( in_category(1) ) {
        $content = strip_shortcodes( $content );
      }
      return $content;
    }
    add_filter( 'the_content', 'remove_shortcodes' );
    
    get_header(); ?>
    
    	<div>>
    		<main id="main" <?php generate_main_class(); ?>>
    		<?php do_action('generate_before_main_content'); ?>
    		
    
    		<?php while ( have_posts() ) : the_post(); ?>
    
    			<?php get_template_part( 'content', 'single' ); ?>
    
    			<?php
    				// If comments are open or we have at least one comment, load up the comment template
    				if ( comments_open() || '0' != get_comments_number() ) : ?>
    					<div class="comments-area">
    						<?php comments_template(); ?>
    					</div>
    			<?php endif; ?>
    
    		<?php endwhile; // end of the loop. ?>
    		<?php do_action('generate_after_main_content'); ?>
    		</main><!-- #main -->
    	</div><!-- #primary -->
    
    <?php 
    do_action('generate_sidebars');
    get_footer();

    Is this okay?

    Thanks & Regards,

    Surya

    #421909
    Leo
    Staff
    Customer Support

    Yup if it works then it should be good!

Viewing 13 posts - 1 through 13 (of 13 total)
  • You must be logged in to reply to this topic.