Ich benutze die Funktion do_shortcode
, um einen Shortcode in meine Vorlage einzufügen. Aber ich möchte überprüfen, ob dieser Shortcode existiert, bevor ich sie anzeige.
Ich meine so
If (shortcode_gallery_exists) {
echo do_shortcode('[gallery]');
}
Kann mir jemand helfen? Vielen Dank
# 23572 hat shortcode_exists () in 3.6 eingeführt.
Ich denke, Sie könnten den folgenden Code verwenden, um dies zu tun:
$content = get_the_content();
//write the begining of the shortcode
$shortcode = '[gallery';
$check = strpos($content,$shortcode);
if($check=== false) {
//Code to execute if there isn't the shortcode
} else {
//Code to execute if the shortcode is present
}
(Einschränkung: nicht getestet)
Sie können Ihre eigene Funktion erstellen,
// check the current post for the existence of a short code
function has_shortcode( $shortcode = NULL ) {
$post_to_check = get_post( get_the_ID() );
// false because we have to search through the post content first
$found = false;
// if no short code was provided, return false
if ( ! $shortcode ) {
return $found;
}
// check the post content for the short code
if ( stripos( $post_to_check->post_content, '[' . $shortcode) !== FALSE ) {
// we have found the short code
$found = TRUE;
}
// return our final results
return $found;
}
Die in Ihrer Vorlage schreiben eine bedingte wie,
if(has_shortcode('[gallery]')) {
// perform actions here
}
Idee von diesem NetTuts Link
Fand dies online irgendwo und wird ein- oder zweimal benutzt
//first we check for shortcode in the content
$tempContent = get_the_content();
$tempCheck = '[gallery';
$tempVerify = strpos($tempContent,$tempCheck);
if($tempVerify === false) {
//Your Shortcode not found do nothing ? you choose
} else {
echo do_shortcode('[gallery]');
}
.
(ich weiß, die [Galerie fehlt die] .. lass es so)
Dies sollte innerhalb der Schleife verwendet werden ..
Ich hoffe, das hilft, Sagive
Mit WordPress können Sie prüfen, ob ein Shortcode existiert oder nicht.
Um dies zu überprüfen, können Sie shortcode_exists()
function verwenden. Es wird true zurückgegeben, wenn der Shortcode vorhanden ist.
<?php if ( shortcode_exists( $tag ) ) { } ?>
Wobei $tag
der Name des zu überprüfenden Shortcodes ist.
<?php
if ( shortcode_exists( 'latest_post' ) ) {
// The short code exists.
}
?>