Denken Sie an einen Beitragsinhalt wie unten:
[shortcode a="a_param"]
... Some content and shortcodes here
[shortcode b="b_param"]
.. Again some content here
[shortcode c="c_param"]
Ich habe einen Shortcode, der 3 oder mehr Parameter benötigt. Ich möchte herausfinden, wie oft der Shortcode beim Inhalt und seinen Parametern in einem Array verwendet wird, z.
array (
[0] => array(a => a_param, b=> null, c=>null),
[1] => array(a => null, b=> b_param, c=>null),
[2] => array(a => null, b=> null, c=>c_param),
)
Ich muss dies im the_content-Filter, wp_head-Filter oder ähnlichem tun.
Wie kann ich das machen ?
Vielen Dank,
Tamil Selvan C
Bei WordPress get_shortcode_regex() Die Funktion gibt einen regulären Ausdruck zurück, der zum Suchen nach Shortcodes in Beiträgen verwendet wird.
$pattern = get_shortcode_regex();
Dann preg_match das Muster mit dem Beitragsinhalt
if ( preg_match_all( "https://stackoverflow.com/". $pattern .'/s', $post->post_content, $matches ) )
Wenn es „true“ zurückgibt, werden die extrahierten Shortcode-Details in der Variablen „$matches“ gespeichert.
Versuchen
global $post;
$result = array();
//get shortcode regex pattern wordpress function
$pattern = get_shortcode_regex();
if ( preg_match_all( "https://stackoverflow.com/". $pattern .'/s', $post->post_content, $matches ) )
{
$keys = array();
$result = array();
foreach( $matches[0] as $key => $value) {
// $matches[3] return the shortcode attribute as string
// replace space with '&' for parse_str() function
$get = str_replace(" ", "&" , $matches[3][$key] );
parse_str($get, $output);
//get all shortcode attribute keys
$keys = array_unique( array_merge( $keys, array_keys($output)) );
$result[] = $output;
}
//var_dump($result);
if( $keys && $result ) {
// Loop the result array and add the missing shortcode attribute key
foreach ($result as $key => $value) {
// Loop the shortcode attribute key
foreach ($keys as $attr_key) {
$result[$key][$attr_key] = isset( $result[$key][$attr_key] ) ? $result[$key][$attr_key] : NULL;
}
//sort the array key
ksort( $result[$key]);
}
}
//display the result
print_r($result);
}
Um Zeit zu sparen, ist dies die Funktion, die ich basierend auf der Antwort von @Tamil Selvan C erstellt habe:
function get_shortcode_attributes( $shortcode_tag ) {
global $post;
if( has_shortcode( $post->post_content, $shortcode_tag ) ) {
$output = array();
//get shortcode regex pattern wordpress function
$pattern = get_shortcode_regex( [ $shortcode_tag ] );
if ( preg_match_all( "https://stackoverflow.com/". $pattern .'/s', $post->post_content, $matches ) )
{
$keys = array();
$output = array();
foreach( $matches[0] as $key => $value) {
// $matches[3] return the shortcode attribute as string
// replace space with '&' for parse_str() function
$get = str_replace(" ", "&" , trim( $matches[3][$key] ) );
$get = str_replace('"', '' , $get );
parse_str( $get, $sub_output );
//get all shortcode attribute keys
$keys = array_unique( array_merge( $keys, array_keys( $sub_output )) );
$output[] = $sub_output;
}
if( $keys && $output ) {
// Loop the output array and add the missing shortcode attribute key
foreach ($output as $key => $value) {
// Loop the shortcode attribute key
foreach ($keys as $attr_key) {
$output[$key][$attr_key] = isset( $output[$key] ) && isset( $output[$key] ) ? $output[$key][$attr_key] : NULL;
}
//sort the array key
ksort( $output[$key]);
}
}
}
return $output;
}else{
return false;
}
}
137434 0 0 cookie-check Shortcode-Parameter im Inhalt extrahieren – WordPress yes
mögliches Duplikat der WordPress-Shortcode-Anzahl, wie oft sie in der Post verwendet wurde
– rnevius
11. September 2015 um 12:52 Uhr