Benutzerdefinierte Post -Typ -WordPress, Ausgabe speichern BildgaleriePhp

PHP-Programmierer chatten hier
Anonymous
 Benutzerdefinierte Post -Typ -WordPress, Ausgabe speichern Bildgalerie

Post by Anonymous »

Ich arbeite an einem benutzerdefinierten Beitragstyp in WordPress für "Projekte", der drei benutzerdefinierte Meta -Boxen enthält: einen für einen Projekttitel, einen für eine Projektbeschreibung und eine für eine Bildergalerie. Mit der Galerie -Metabox können ich Bilder mit dem WordPress Media -Uploader hinzufügen. Die Bilder werden in der Meta -Box sofort nach dem Hinzufügen korrekt angezeigt, aber sobald ich das Projekt aktualisiere und die Seite aktualisiere, verschwinden die Galeriebilder. (_progetto_titolo). Es enthält JavaScript, das Bilder aus der Galerie hinzufügt und entfernt. Ich vermutete, dass dies einen zweiten Save -Zyklus auslöste, der andere Metawerte (wie die Galerie) ausgelöscht hat. Also habe ich ein paar Ansätze ausprobiert: < /p>
vorübergehend den Save_Post-Hook entfernen, bevor ich wp_update_post () aufgerufen habe, und dann neu aufzurufen. Trotz dieser Versuche endet das Galerie -Meta -Feld nach dem Aktualisieren und Nachladen des Beitrags immer noch leer.// Registra il Custom Post Type "Progetti"
function create_custom_post_type_progetti() {
$labels = array(
'name' => _x( 'Progetti', 'Post Type General Name', 'textdomain' ),
'singular_name' => _x( 'Progetto', 'Post Type Singular Name', 'textdomain' ),
'menu_name' => __( 'Progetti', 'textdomain' ),
'name_admin_bar' => __( 'Progetto', 'textdomain' ),
'archives' => __( 'Archivio Progetti', 'textdomain' ),
'attributes' => __( 'Attributi Progetto', 'textdomain' ),
'parent_item_colon' => __( 'Progetto Genitore:', 'textdomain' ),
'all_items' => __( 'Tutti i Progetti', 'textdomain' ),
'add_new_item' => __( 'Aggiungi Nuovo Progetto', 'textdomain' ),
'add_new' => __( 'Aggiungi Nuovo', 'textdomain' ),
'new_item' => __( 'Nuovo Progetto', 'textdomain' ),
'edit_item' => __( 'Modifica Progetto', 'textdomain' ),
'update_item' => __( 'Aggiorna Progetto', 'textdomain' ),
'view_item' => __( 'Visualizza Progetto', 'textdomain' ),
'view_items' => __( 'Visualizza Progetti', 'textdomain' ),
'search_items' => __( 'Cerca Progetto', 'textdomain' ),
'not_found' => __( 'Non trovato', 'textdomain' ),
'not_found_in_trash' => __( 'Non trovato nel cestino', 'textdomain' ),
'featured_image' => __( 'Immagine di copertina', 'textdomain' ),
'set_featured_image' => __( 'Imposta immagine di copertina', 'textdomain' ),
'remove_featured_image' => __( 'Rimuovi immagine di copertina', 'textdomain' ),
'use_featured_image' => __( 'Usa come immagine di copertina', 'textdomain' ),
'insert_into_item' => __( 'Inserisci nel progetto', 'textdomain' ),
'uploaded_to_this_item' => __( 'Caricato in questo progetto', 'textdomain' ),
'items_list' => __( 'Lista progetti', 'textdomain' ),
'items_list_navigation' => __( 'Navigazione lista progetti', 'textdomain' ),
'filter_items_list' => __( 'Filtra lista progetti', 'textdomain' ),
);
$args = array(
'label' => __( 'Progetto', 'textdomain' ),
'description' => __( 'Custom post type per progetti', 'textdomain' ),
'labels' => $labels,
// Supporta l'immagine in evidenza; per titolo e contenuto usiamo metabox personalizzati
'supports' => array( 'thumbnail' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-portfolio',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'show_in_rest' => true,
);
register_post_type( 'progetti', $args );
}
add_action( 'init', 'create_custom_post_type_progetti', 0 );

// Enqueue delle funzioni necessarie per il media uploader in admin
function progetti_enqueue_admin_scripts( $hook ) {
global $post;
if ( ( $hook == 'post-new.php' || $hook == 'post.php' ) && isset( $post ) && $post->post_type === 'progetti' ) {
wp_enqueue_media();
}
}
add_action( 'admin_enqueue_scripts', 'progetti_enqueue_admin_scripts' );

// Aggiunge le Meta Box per i campi personalizzati
function progetti_add_meta_boxes() {
add_meta_box( 'progetti_titolo', 'Titolo Progetto', 'progetti_titolo_meta_box_callback', 'progetti', 'normal', 'high' );
add_meta_box( 'progetti_descrizione', 'Descrizione Progetto', 'progetti_descrizione_meta_box_callback', 'progetti', 'normal', 'high' );
add_meta_box( 'progetti_gallery', 'Gallery Immagini', 'progetti_gallery_meta_box_callback', 'progetti', 'normal', 'high' );
}
add_action( 'add_meta_boxes', 'progetti_add_meta_boxes' );

// Meta box per il Titolo: include il nonce
function progetti_titolo_meta_box_callback( $post ) {
wp_nonce_field( 'progetti_save_meta_box_data', 'progetti_meta_box_nonce' );
$value = get_post_meta( $post->ID, '_progetto_titolo', true );
wp_editor( $value, 'progetto_titolo', array(
'textarea_name' => 'progetto_titolo',
'media_buttons' => false,
'textarea_rows' => 5,
) );
}

// Meta box per la Descrizione: non include il nonce
function progetti_descrizione_meta_box_callback( $post ) {
$value = get_post_meta( $post->ID, '_progetto_descrizione', true );
wp_editor( $value, 'progetto_descrizione', array(
'textarea_name' => 'progetto_descrizione',
'media_buttons' => false,
'textarea_rows' => 10,
) );
}

// Meta box per la Gallery: non include il nonce
function progetti_gallery_meta_box_callback( $post ) {
$gallery = get_post_meta( $post->ID, '_progetto_gallery', true );
?>

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post