2016-04-02 7 views
0

ich ein Plugin schreibe, in dem ich unter benutzerdefinierten Post-Typ haben:Aufruf benutzerdefinierten Post-Typ in Plugin selbst

function create_post_type_contact() { 
register_post_type('contact', 
    array(
     'labels' => array(
      'name' => __('Contacts'), 
        'singular_name' => __('contact'), 
      'add_new' => __('Add New contact'), 
      'add_new_item' => __('Add New contact'), 
      'edit_item' => 'Edit Contact', 
      'new_item' => 'New Contact', 
      'view_item' => 'View Contact', 
      'search_items' => 'Search Contact', 
      'not_found' => 'No contacts found', 
      'not_found_in_trash' => 'No contacts found in Trash' 
      ), 
    'public' => true, 
    'menu_position' => 24, 
    'menu_icon' => 'dashicons-email', 
    'rewrite' => array(
     'slug' => __('contact') 
    ), 
    'supports' => array('title'), 
)); 
} 

Auch haben unter benutzerdefinierten Meta-Felder hinzugefügt:

// Field Array 
$custom_meta_fields_contact = array( 
array(
    'label'=> __('Contact Name'), 
    'desc' => 'Enter Contact Name here', 
    'id' => 'contact_name', 
    'type' => 'text' 
), 
array(
    'label'=> __('Contact Address'), 
    'desc' => 'Enter Contact address here', 
    'id' => 'contact_address', 
    'type' => 'textarea' 
), 
array(
    'label'=> __('Contact No'), 
    'desc' => 'Enter Contact number here', 
    'id' => 'contact_no', 
    'type' => 'text' 
), 
array(
    'label'=> __('Contact Email'), 
    'desc' => 'enter contact email id here', 
    'id' => 'contact_email', 
    'type' => 'text' 
), 
); 

Ich habe Funktionen hinzugefügt um benutzerdefinierte Meta-Felder anzuzeigen und benutzerdefinierte Meta zu speichern, die in Ordnung ist und funktioniert.

Innerhalb dieses Plugins muss ich contact_email Meta-Feld aufrufen. Für die ich folgenden Code hinzugefügt:

// get the Contact email address 
$args_contact = array('post_type' => 'contact'); 

$contact_posts = new WP_Query($args_contact); 
if($contact_posts->have_posts()) : 
    while($contact_posts->have_posts()) : $contact_posts->the_post(); 
     $to = get_post_meta($post->ID, 'contact_email'); 
    endwhile; endif; 
wp_reset_query(); 

Aber immer, wenn ich versuche, den Wert von $ zu innerhalb des Plugins zu bekommen, es ist nichts zurück. Wo wie auf der Kontaktseite kann contact_name, contact_number, contact_address und contact_email richtig angezeigt werden.

Jede Hilfe oder Anregung wird geschätzt.

Antwort

0

verwenden

get_post_meta(get_the_ID(), 'contact_email', true); 

OR erklären global $ post-Variable nach, wenn die Bedingung

if($contact_posts->have_posts()) : 
global $post; 
+0

dankt 4 der gr8 Hilfe. es funktionierte. Ich hatte die globale Erklärung verpasst. – Shubh

Verwandte Themen