web-dev-qa-db-fra.com

Comment exclure un shortcode d'ID de page spécifiques s'il est défini sur global

En ce moment, j'utilise le code ci-dessous pour ajouter un shortcode pour un tableau sur tous les articles et toutes les pages, mais j'aimerais exclure quelques pages et tout article comportant une balise spécifique. (ex. identifiants de page 10,20 et identifiant de tag 30)

Je suis assez nouveau pour tout cela et j'apprends encore.

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

Merci pour l'aide à l'avance.

2
Archi25

S'il vous plaît essayez ceci et laissez-moi savoir le résultat

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
Abdul Awal