insérer plusieurs shortcodes dans un shortcode wordpress

Publié par Jean-Michel le

Je suis nouveau en php et j’aimerais savoir comment insérer plusieurs shortcodes avec des shortcodes

le résultat souhaité ressemblerait à ceci

[logo_section_2 section-title="1" section-title-text="WE SHARE THE SAME CULTURE" logos-second-row="true"]
  [logo_images logo-id="1" logo-image-source="http://localhost:8888/signal-theme-export/wordpress/wp-content/uploads/2016/07/logo-swiss.png"]
  [logo_images logo-id="2" logo-image-source="http://localhost:8888/signal-theme-export/wordpress/wp-content/uploads/2016/07/logo-hudson.png"]
  [logo_images logo-id="3" logo-image-source="http://localhost:8888/signal-theme-export/wordpress/wp-content/uploads/2016/07/logo-twitter.png"]
  [logo_images logo-id="4" logo-image-source="http://localhost:8888/signal-theme-export/wordpress/wp-content/uploads/2016/07/logo-index.png"]
  [logo_images logo-id="5" logo-image-source="http://localhost:8888/signal-theme-export/wordpress/wp-content/uploads/2016/07/logo-reed.png"]
  [logo_images logo-id="6" logo-image-source="http://localhost:8888/signal-theme-export/wordpress/wp-content/uploads/2016/07/logo-viagogo.png"]

  [logo_images_2 logo-id="4" logo-image-source="http://localhost:8888/signal-theme-export/wordpress/wp-content/uploads/2016/07/logo-index.png"]
  [logo_images_2 logo-id="5" logo-image-source="http://localhost:8888/signal-theme-export/wordpress/wp-content/uploads/2016/07/logo-reed.png"]
  [logo_images_2 logo-id="6" logo-image-source="http://localhost:8888/signal-theme-export/wordpress/wp-content/uploads/2016/07/logo-viagogo.png"]

[/logo_section_2]

C’est pour que je puisse ajouter plusieurs lignes d’images

mon php est comme ci-dessous

<?php

add_shortcode(‘logo_section_2’, ‘logo_section_2’); function logo_section_2( $atts, $content = «  » ) { shortcode_atts( array( ‘section-title’ => false, ‘section-title-text’ =>  », ‘logos-second-row’ => false, / / ….. ),$atts);

    $output = '';
    $output .= '<div class="logo-section clearfix">';
      $output .= '<div class="logo-section--inner clearfix">';
          if ($atts['section-title'] == true) {
          $output .= '<h2 class="heading--small heading--bold heading--uppercase heading--sans-serif heading--center heading">'. $atts['section-title-text'] .'</h2>';
          } else {
                  $output .= '';
          }

          $output .=  '<div class="logo-section--row clearfix">' . do_shortcode($content) . '</div>';

          if ($atts['logos-second-row'] == 'true') {
            $output .=  '<div class="logo-section--row clearfix">' . do_shortcode($content) . '</div>';
          } else {
                  $output .= '';
          }
      $output .= '</div>';
    $output .= '</div>';
  return $output;

}

add_shortcode('logo_images', 'logo_images');
function logo_images($atts){
  shortcode_atts(
    array(
      'logo-id' => '',
      'logo-image-source' => '',
      'logo-alt' => '',
    ),$atts);
    $output = '';
    if($atts['logo-id'] || $atts['logo-image-source']){
               $output .= '<img class="logo-'.$atts['logo-id'].'" src="' . $atts['logo-image-source'] . '" alt="" />';
    } else {
            $output .= '';
    }
    return $output;
 }
add_shortcode('logo_images_2', 'logo_images_2');
function logo_images_2($atts){
  shortcode_atts(
    array(
      'logo-id' => '',
      'logo-image-source' => '',
      'logo-alt' => '',
    ),$atts);
    $output = '';
    if($atts['logo-id'] || $atts['logo-image-source']){
               $output .= '<img class="logo-'.$atts['logo-id'].'" src="' . $atts['logo-image-source'] . '" alt="" />';
    } else {
            $output .= '';
    }
    return $output;
 }

toute aide serait géniale!

Solution n°1 trouvée

I think what you’re asking is about nested shortcodes… you want to trigger a UI element that may have one or more (variable number) internal items.

This concept is difficult to explain because it requires several code concepts put together. But I will try:

  1. Outer Shortcode:

    a. Set a global to test if already in that shortcode

    b. call do_shortcode($content) to execute inner shortcodes

    c. (Inner shortcode will set up an global array)

    d. Generate full UI using a loop on the global array

    e. return the output

  2. Inner Shortcode:

    a. set up a global array to be used by the outer shortcode

    b. generate the content and place the output in an array item

    c. return blank/no output

Ce n’est pas si simple que cela … vous devrez prendre en compte plusieurs appels de shortcode externes, des shortcodes mal imbriqués, et si vous voulez ou non que le non-shortcode dans la couche interne soit traité ou non.

Jetez un œil à ce code… peut-être pourrez-vous en extraire quelques idées.

Catégories : Wordpress

Jean-Michel

Jean-Michel est en charge de la partie blog du site. Il met en place la stratégie de contenu et répond aux questions fréquentes sur Wordpress.

0 commentaire

Laisser un commentaire

Avatar placeholder

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *