Как исключить шорткод из определенных идентификаторов страниц, если для него установлено значение глобальный


В данный момент я использую приведенный ниже код, чтобы добавить шорткод для таблицы во всех сообщениях и страницах, но я хотел бы исключить несколько страниц и любое сообщение с определенным тегом. (например, идентификаторы страниц 10,20 и идентификатор тега 30)

Я совершенно новичок во всем этом, и я все еще учусь.

function my_shortcode_to_a_post( $content ) {
  global $post;
  if( ! $post instanceof WP_Post ) return $content;
  switch( $post->post_type ) {
    case 'post':
      return $content . '[table id=1 /]';
    case 'page':
      return $content . '[table id=1 /]';
    default:
      return $content;
  }
}

add_filter( 'the_content', 'my_shortcode_to_a_post' );

Заранее спасибо за помощь.

Author: Archi25, 2017-03-26

1 answers

Пожалуйста, попробуйте это и дайте мне знать результат

function my_shortcode_to_a_post( $content ) {

  global $post;
  $disabled_post_ids = array('20', '31', '35');
  $disabled_tag_ids = array('5', '19', '25');

  $current_post_id = get_the_ID(); // try setting it to $post->ID; if it doesn't work for some reason
  if(in_array($current_post_id, $disabled_post_ids)) {
    return $content;
  }

  $current_post_tags = get_the_tags($current_post_id);

  if($current_post_tags){
    $tags_arr = array();
    foreach ($current_post_tags as $single_tag) {
      $tag_id = $single_tag->term_id;
      if(in_array($tag_id, $disabled_tag_ids)) {
        return $content;
      }
    }
  }

  switch( $post->post_type ) {
    case 'post':
      return $content . '[table id=1 /]';
    case 'page':
      return $content . '[table id=1 /]';
    default:
      return $content;
  }
}

add_filter( 'the_content', 'my_shortcode_to_a_post' );
 1
Author: Abdul Awal Uzzal, 2017-03-26 22:54:09