Ich habe 2 verschiedene Arbeitsweisen bei functions.php für Backend. Jede Methode unten hat 2 Haken; 1, um das neue benutzerdefinierte Feld anzuzeigen, und ein weiterer Haken, um die Werte zu speichern/aktualisieren:
Methode 1:
function media_hacks_attachment_field_to_edit( $form_fields, $post ){
// https://codex.wordpress.org/Function_Reference/wp_get_attachment_metadata
$media_author = get_post_meta( $post->ID, 'media_author', true );
$form_fields['media_author'] = array(
'value' => $media_author ? $media_author : '',
'label' => __( 'Author' )
);
return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'media_hacks_attachment_field_to_edit', null, 2 );
//Saving value on Update (method 1)
function media_hacks_edit_attachment( $attachment_id ){
if ( isset( $_REQUEST['attachments'][$attachment_id]['media_author'] ) ) {
$media_author = $_REQUEST['attachments'][$attachment_id]['media_author'];
update_post_meta( $attachment_id, 'media_author', $media_author );
}
}
add_action( 'edit_attachment', 'media_hacks_edit_attachment' );
Methode 2:
function my_image_attachment_fields_to_edit($form_fields, $post) {
// $form_fields is a special array of fields to include in the attachment form
// $post is the attachment record in the database
// $post->post_type == 'attachment'
// (attachments are treated as posts in WordPress)
// add our custom field to the $form_fields array
// input type="text" name/id="attachments[$attachment->ID][custom1]"
$form_fields["custom1"] = array(
"label" => __("Custom Text Field"),
"input" => "text", // this is default if "input" is omitted
"value" => get_post_meta($post->ID, "_custom1", true)
);
// if you will be adding error messages for your field,
// then in order to not overwrite them, as they are pre-attached
// to this array, you would need to set the field up like this:
$form_fields["custom1"]["label"] = __("Custom Text Field");
$form_fields["custom1"]["input"] = "text";
$form_fields["custom1"]["value"] = get_post_meta($post->ID, "_custom1", true);
return $form_fields;
}
// attach our function to the correct hook
add_filter("attachment_fields_to_edit", "my_image_attachment_fields_to_edit", null, 2);
//Saving value on Update (method 2)
function my_image_attachment_fields_to_save($post, $attachment) {
// $attachment part of the form $_POST ($_POST[attachments][postID])
// $post attachments wp post array - will be saved after returned
// $post['post_type'] == 'attachment'
if( isset($attachment['custom1']) ){
// update_post_meta(postID, meta_key, meta_value);
update_post_meta($post['ID'], '_custom1', $attachment['custom1']);
}
return $post;
}
add_filter("attachment_fields_to_save", "my_image_attachment_fields_to_save", null, 2);
Hier ist das gute Ergebnis in der Backend-Medienbibliothek (Benutzerdefiniertes Textfeld & Autor):
Das war es für die Backend-Dashboard.
Meine Frage bezieht sich auf das Frontend:
Jetzt Wie kann ich Werte dieser 2 benutzerdefinierten Felder am FRONTEND abrufen und anzeigen??
Hier ist mein fehlgeschlagener Versuch einer PHP-Vorlagenseite:
<tr id='MySpecialRow'>
<td colspan='2' style="background:#000;color:#fff;">
<?php
$args = array('cat' => 8);
$query = new WP_Query($args);
if ($query->have_posts()) {
// some code here if you want.
while ($query->have_posts()) {
$query->the_post();
$untitled_meta = rwmb_meta('image_advanced_8hswqfsoqai', '', get_the_ID());
foreach ($untitled_meta as $image) {
$media_author = get_post_meta( get_the_ID(), 'media_author', true );
echo get_the_ID();//correctly prints post id
echo $media_author;//prints nothing :(
}
}
}
?>
</td>
</tr>
Kleine Anmerkungen:
-
get_the_ID()
druckt die Post-ID, aber$media_author
hat keinen wert 🙁 -
Ich mache eine Abfrageschleife für WordPress-Posts, da die Galerie mit den benutzerdefinierten Feldern in einem Post vorhanden ist. Mit anderen Worten, ich habe die Beitrags-ID nicht, da ich mich in einer Seitenvorlage befinde.
foreach ($untitled_meta as $image)
– was ist$untitled_meta
soll hier sein? Und sollten Sie nicht vermutlich die ID von verwenden$image
hier dann um die Metadaten zu holen? Es ist doch auf dem Medienposten gespeichert, oder?– CBroe
7. Juni um 8:53
@CBroe ja, tut mir leid, ich habe vergessen einzufügen
$untitled_meta
Linie. Okay also mit dem 1get_the_ID()
Ich habe die richtigen Post-IDs in einer Schleife, also wie bekomme ich die Medienbild-IDs?– Beachtung
7. Juni um 10:20 Uhr
Das sagt uns immer noch nicht, was genau Sie in diesem Metafeld gespeichert haben. Was ist
$image
jetzt? Bild-ID als ganze Zahl? Ein komplexes Objekt? …?– CBroe
7. Juni um 10:24 Uhr
@CBroe, wenn ich das tue
var_dump($untitled_meta);
Ich erhalte die Bilder der Medienbibliothek, die sich in einer erweiterten Galerie befinden (ich verwende MetaBox), die in einem Beitrag vorhanden ist. So$image
ist 1 Bild aus dieser Galerie. Danke für deine Frage, Liebes.– Beachtung
7. Juni um 10:34 Uhr
“Ich bekomme die Bilder der Medienbibliothek” – Bedeutung was, exakt? Erhalten Sie PHP-Objekte/Arrays, erhalten Sie HTML-Code, der diese Bilder anzeigen würde …?
– CBroe
7. Juni um 10:36 Uhr