Home › Forum › Problemi Vari con WP › Insertimento posts/allegati da via script
-
AutorePost
-
-
7 Febbraio 2012 alle 9:09 #20618m4rko80Partecipante
Ciao a tutti, come da titolo devo inserire dei post presi da xml feeds e se necessario con tanto di allegato/i.
Utilizzo php in quanto sviluppo di lavoro ma non ho mia utilizzato per ora piu’ di tanto il codex di wp.
Al momento ho già creato quasi tutti, prendo le news e le metto in wp_posts con tutti i dati necessari.
L’unica cosa che sbaglio(probabilmente mi manca un passaggio) e’ il collegamento fra il post e l’allegato creato.
Dopo aver creato un post mi ritrovo con l’ID del post appena inserito e quindi faccio questo:
$ins = wp_insert_post( $post );//ID NUOVO POST
//path contenitore immagini
$upload_dir = wp_upload_dir();
//path completo immagine
$filename = $upload_dir.'/auto.jpg';//statica per test
$wp_filetype = wp_check_filetype(basename($filename), null );
//i dati relativi all'allegato
$attachment = array(
'post_mime_type' => $wp_filetype,
'post_title' => preg_replace('/.[^.]+$/', '', basename($filename)),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $filename, $ins ); //id allegato
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
La foto viene creata nei vari formati ma quando visualizzo il post non c’e’ un collegamento a questo.
Per chi ne dovesse capire di codice e vorrebbe darmi una mano ringrazio:)
-
7 Febbraio 2012 alle 9:25 #92958wollyAmministratore del forum
è presente nella galleria del post?
Lo puoi vedere in 2 modi, uno dal menu media e vedi se l’allegato è legato al post oppure entrando in modifica del post fai clik su aggiungi media e vedi se hai file allegati.
-
7 Febbraio 2012 alle 9:37 #92960m4rko80Partecipante
CIao Wolly, ok dal menu Media vedo l’allegato associato al post.
Controllo il template, magari per qualche ragione non mostra l’allegato.
Ti faccio sapere grazie.
-
7 Febbraio 2012 alle 10:06 #92961wollyAmministratore del forum
probabilmente manca l’html che visualizza l’immagine all’interno del post o se sono gallery lo shortcode [gallery] all’interno del content.
-
7 Febbraio 2012 alle 12:55 #92976m4rko80Partecipante
Perfetto ora funziona tutto.
Grazie
-
7 Febbraio 2012 alle 13:33 #92983wollyAmministratore del forum
se scrivi qual era il problema e come lo hai risolto aiuti altri che potrebbero avere lo stesso problema.
-
8 Febbraio 2012 alle 9:56 #93026m4rko80Partecipante
In pratica la creazione/associazione era corretta.
Ho dovuto aggiungere alla parte di codice:
/* visualizzazione immagine post
$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => get_the_ID() );
$attachments = get_posts($args);
echo wp_get_attachment_image( $attachments[0]->ID, 'thumbnail', true );
*/
-
-
AutorePost
- Devi essere connesso per rispondere a questo topic.