Site logo

Archived topic

Remove short codes from the posts in a specific category

12 replies · Started by Surya on November 3, 2017

Viewing posts 1–13 of 13

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.

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

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’ );

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?

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.

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

Hi,

It's not working.

Can you show me your exact code?

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"]

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

Thanks!

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

Also please reply here. Thanks!

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

Yup if it works then it should be good!

This archived topic is closed to new replies.