Ricerca…


Sintassi

  • add_shortcode( $tag , $func );

Parametri

Parametro Dettagli
$ tag (stringa) (obbligatorio) Tag shortcode da cercare nel contenuto del post
$ func (callable) (richiesto) Hook da eseguire quando viene trovato shortcode

Osservazioni

  • Il callback shortcode avrà tre argomenti: gli attributi shortcode, il contenuto shortcode (se presente) e il nome dello shortcode.
  • Ci può essere un solo gancio per ogni shortcode. Il che significa che se un altro plugin ha uno shortcode simile, sostituirà il tuo o il tuo, sovrascriverlo a seconda dell'ordine in cui i plug-in sono inclusi e / o eseguiti.
  • I nomi degli attributi Shortcode vengono sempre convertiti in caratteri minuscoli prima di essere passati alla funzione del gestore. I valori sono intatti.
  • Si noti che la funzione chiamata dallo shortcode non dovrebbe mai produrre output di alcun tipo. Le funzioni Shortcode devono restituire il testo che deve essere utilizzato per sostituire lo shortcode. Produrre direttamente l'output porterà a risultati inaspettati. Questo è simile al modo in cui le funzioni del filtro dovrebbero comportarsi, in quanto non dovrebbero produrre effetti collaterali attesi dalla chiamata, dal momento che non è possibile controllare quando e da dove vengono chiamati.

Shortcode semplice per post recenti

add_shortcode è la parola chiave wp.

// recent-posts is going to be our shortcode.
add_shortcode('recent-posts', 'recent_posts_function');

// This function is taking action when recent post shortcode is called.
function recent_posts_function() {
   query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));
   if (have_posts()) :
      while (have_posts()) : the_post();
         $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';
      endwhile;
   endif;
   wp_reset_query();
   return $return_string;
}

Questo frammento può essere inserito nel tuo theme functions.php .

[recent-posts] Questo è uno shortcode per i post recenti. Possiamo applicare questo shortcode nel backend (come pagine, post, widget).

Possiamo anche usare lo stesso shortcode nel nostro codice. con l'aiuto di do_shortcode .
Per esempio. echo do_shortcode( '[recent-posts]' );

Shortcode avanzato per i post recenti

Questa funzione accetta i parametri per il numero di post recenti che desideri visualizzare.

Es: vuoi mostrare solo cinque post recenti. Ho appena passato gli argomenti con posts = "5" (puoi passare qualsiasi numero di post recenti che vuoi visualizzare).

La funzione recupera solo cinque post recenti dal database.

// recent-posts is going to be our shortcode.
add_shortcode('recent-posts', 'recent_posts_function');

// Functions takes parameter such as posts="5".
function recent_posts_function($atts){
   extract(shortcode_atts(array(
      'posts' => 1,
   ), $atts));

   $return_string = '<ul>';
   query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
   if (have_posts()) :
      while (have_posts()) : the_post();
         $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
      endwhile;
   endif;
   $return_string .= '</ul>';

   wp_reset_query();
   return $return_string;
}

Per esempio. echo do_shortcode( '[recent-posts posts="5"]' );



Modified text is an extract of the original Stack Overflow Documentation
Autorizzato sotto CC BY-SA 3.0
Non affiliato con Stack Overflow