Hilfe wobei?
Was ist deine Frage?
Sorry.
Meine Frage ist: Wie erstelle ich am besten diese Query, die dann anschließend den Shortcode befüllt.
Danke!
Das sprengt hier eigentlich ein wenig den Rahmen und deine Frage wäre vermutlich bei Stackexchange besser aufgehoben.
Ich habe rasch ein paar Zeilen zusammengestellt, wobei für die Ausgabe z.B. ein zusätzlicher Parameter für die Anzahl der Spalten sicherlich sinnvoll wäre. Schau mal, ob du damit zurecht kommst:
<?php
/**
* Plugin Name: Gallery Shortcode
*/
// Add Shortcode for CPT thumbnail galleries
// Add Shortcode
function custom_posttype_shortcode( $atts ) {
$out = '<div class="cptgallery">';
// Attributes
$atts = shortcode_atts(
array(
'cpt' => '',
'ids' => '',
),
$atts
);
$idarray = explode( ',', strval( $atts['ids'] ) );
// check, if CPT-attribute was set
if ( '' == $atts['cpt'] ) {
return 'Bitte Custom Post Type angeben <br>
(z.B. <code>[cptgallery cpt="portfolio"]</code>)';
} else {
// WP_Query arguments
if ( ! empty( $atts['ids'] ) ) {
$args = array(
'post_type' => array( $atts['cpt'] ),
'posts_per_page' => -1,
'post__in' => $idarray,
);
} else {
$args = array(
'post_type' => array( $atts['cpt'] ),
'posts_per_page' => -1,
);
}
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$out .= get_the_post_thumbnail( $query->ID, 'medium' );
}
} else {
'Keine Beiträge gefunden';
}
// Restore original Post Data
wp_reset_postdata();
$out .= '</div>';
return $out;
}
}
add_shortcode( 'cptgallery', 'custom_posttype_shortcode' );
Vielen Dank für den Code.
Der hat mir wunderbar weitergeholfen.
Ich habe das nun für mich passend umgeschrieben.
Danke!
/**
* Plugin Name: Gallery Shortcode
*/
// Add Shortcode for CPT thumbnail galleries
// Add Shortcode
function custom_posttype_shortcode( $atts ) {
// Attributes
$atts = shortcode_atts(
array(
'posttype' => '',
'ids' => '',
),
$atts
);
$idarray = explode( ',', strval( $atts['ids'] ) );
$out = '[gallery type="justified" link="file" ids="';
// check, if CPT-attribute was set
if ( '' == $atts['posttype'] ) {
return 'Bitte Custom Post Type angeben <br>
(z.B. <code>[postgallery posttype="portfolio"]</code>)';
} else {
// WP_Query arguments
if ( ! empty( $atts['ids'] ) ) {
$args = array(
'post_type' => array( $atts['posttype'] ),
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'title',
'post__in' => $idarray,
);
} else {
$args = array(
'post_type' => array( $atts['posttype'] ),
'posts_per_page' => -1,
);
}
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$out .= get_post_thumbnail_id( $query->ID ).',';
}
} else {
'Keine Beiträge gefunden';
}
// Restore original Post Data
wp_reset_postdata();
$out .= '"]';
echo do_shortcode( $out );
}
}
add_shortcode( 'postgallery', 'custom_posttype_shortcode' );
-
Diese Antwort wurde vor 8 Jahren, 2 Monaten von
fabianpnke geändert.
Das dürfte nicht funktionieren, weil in der Loop die HTML-Tags der Thumbnails ausgegeben werden, nicht aber die ID’s der Beitragsbilder. Außerdem sollte eine Shortcode-Funktion einen Wert an den Filter zurückgeben (return
) und nicht selber etwas ausgeben (echo
).