WordPress

niestandardowe typy postów – Wyświetlanie obrazu Meta Box

  • 22 marca, 2013
  • 4 min read
niestandardowe typy postów – Wyświetlanie obrazu Meta Box


Mam pewne problemy z wyświetlaniem informacji z mojego niestandardowego pola meta w moim niestandardowym poście typu single. Używam niestandardowych Meta Boxów wielokrotnego użytku WordPress autorstwa Tammy Hart.

Jestem w stanie wyświetlić pola tekstowe za pomocą tego:

<?php echo get_post_meta($post->ID, $prefix.'hjemmeside', true); ?>

Ale nie mogę wyświetlić obrazu, zamiast tego DEBUG mówi mi, że jest to „Niezdefiniowana zmienna: post_meta_data in”. Obecnie używam tego skryptu:

<?php
    $custom_image = $post_meta_data['image'][0];
    echo wp_get_attachment_image($custom_image, 'thumbnail');
?>

Czy to źle? Identyfikator pola meta obrazu to „image”.

To jest mój plik functions.php (tylko część meta-box):

include (TEMPLATEPATH . '/metaboxes/meta_box.php');

$prefix = 'sample_';

$fields = array(
array( // Hjemmeside
    'label' => 'Hjemmeside', // <label>
    'desc'  => 'Skriv inn hjemmesiden til butikken her.', // description
    'id'    => $prefix.'hjemmeside', // field id and name
    'type'  => 'text' // type of field
),
array( // Facebook
    'label' => 'Facebook', // <label>
    'desc'  => 'Skriv inn facebookadressen til butikken her.', // description
    'id'    => $prefix.'facebook', // field id and name
    'type'  => 'text' // type of field
),
array( // Telefon
    'label' => 'Telefon', // <label>
    'desc'  => 'Skriv inn telefonnummeret til butikken her.', // description
    'id'    => $prefix.'telefon', // field id and name
    'type'  => 'text' // type of field
),
array( // Mailadresse
    'label' => 'Mailadresse', // <label>
    'desc'  => 'Skriv inn mailadressen til butikken her.', // description
    'id'    => $prefix.'mailadresse', // field id and name
    'type'  => 'text' // type of field
),
array( // Senterbeliggenhet
    'label' => 'Senterbeliggenhet', // <label>
    'desc'  => 'Skriv inn hvor butikken er plassert i senteret.', // description
    'id'    => $prefix.'senterbeliggenhet', // field id and name
    'type'  => 'textarea' // type of field
),
array( // Logo
    'label' => 'Logo', // <label>
    'desc'  => 'Last opp logoen til butikken her.', // description
    'id'    => $prefix.'image', // field id and name
    'type'  => 'image' // type of field
),
);

/**
 * Instantiate the class with all variables to create a meta box
 * var $id string meta box id
 * var $title string title
 * var $fields array fields
 * var $page string|array post type to add meta box to
 * var $js bool including javascript or not
 */
$sample_box = new custom_add_meta_box( 'sample_box', 'Butikkinformasjon', $fields, 'butikker', true );

Również tutaj jest link do pliku meta_box.php, który zawiera wszystkie kody odnoszące się do metaboxów 🙂

Warto przeczytać!  Miesiąc w WordPress – kwiecień 2023 – Wiadomości WordPress

Oto wyjście z var_dump(get_post_custom($post->ID));

array(11) { 
  ["_edit_last"]=> array(1) { 
    [0]=> string(1) "1" 
  } 
  ["_edit_lock"]=> array(1) { 
    [0]=> string(12) "1363962761:1" 
  } 
  ["_thumbnail_id"]=> array(1) { 
    [0]=> string(2) "58" 
  } 
  ["sample_text"]=> array(1) { 
    [0]=> string(11) "99 88 99 88" 
  } 
  ["sample_image"]=> array(1) { 
    [0]=> string(1) "0" 
  } 
  ["sample_hjemmeside"]=> array(1) { 
    [0]=> string(21) "www.ethic-clinique.no" 
  } 
  ["sample_facebook"]=> array(1) { 
    [0]=> string(31) "www.facebook.com/ethic-clinique" 
  } 
  ["sample_telefon"]=> array(1) { 
    [0]=> string(11) "99 88 99 88" 
  } 
  ["sample_mailadresse"]=> array(1) { 
    [0]=> string(22) "post@ethic-clinique.no" 
  } 
  ["sample_senterbeliggenhet"]=> array(1) { 
    [0]=> string(42) "Tredje butikken til høyre i andre etasje." 
  } 
  ["sample_logo"]=> array(1) { 
    [0]=> string(42) "Tredje butikken til høyre i andre etasje." 
  } 
}

Nowy var_dump:

array(12) { 

        ["_edit_last"]=> array(1) { [0]=> string(1) "1" } 
        ["_edit_lock"]=> array(1) { [0]=> string(12) "1363964314:1" } 
        ["_thumbnail_id"]=> array(1) { [0]=> string(2) "58" } 
        ["sample_text"]=> array(1) { [0]=> string(11) "99 88 99 88" } 
        ["sample_image"]=> array(1) { [0]=> string(2) "58" } 
        ["sample_hjemmeside"]=> array(1) { [0]=> string(21) "www.ethic-clinique.no" } 
        ["sample_facebook"]=> array(1) { [0]=> string(31) "www.facebook.com/ethic-clinique" } 
        ["sample_telefon"]=> array(1) { [0]=> string(11) "99 88 99 88" } 
        ["sample_mailadresse"]=> array(1) { [0]=> string(22) "post@ethic-clinique.no" } 
        ["sample_senterbeliggenhet"]=> array(1) { [0]=> string(42) "Tredje butikken til høyre i andre etasje." } 
        ["sample_logo"]=> array(1) { [0]=> string(42) "Tredje butikken til høyre i andre etasje." } 
        ["sample_repeatable"]=> array(1) { [0]=> string(74) "a:1:{i:0;a:3:{s:5:"image";s:2:"89";s:5:"title";s:0:"";s:4:"desc";s:0:"";}}" } 

}

Doceń każdą pomoc w tej sprawie 🙂

Warto przeczytać!  Wyszukuj i filtruj przy użyciu niestandardowego typu postu, niestandardowej taksonomii i zaawansowanych pól niestandardowych


Źródło