0

I use a dropdownbox to show all custom types ('abschreibungstabelle'). It works fine. But now, I want to show the value of the custom field 'nutzungsdauer' in my input-field. Any ideas?

  <input type="text" class="text-input" placeholder="Nutzungsdauer" value="" name="nutzungsdauer" id="nd">

function.php

 function afa_type_dropdown( $post_type )
{
$posts = get_posts(
    array(
        'post_type'  => $post_type,
        'numberposts' => -1
    )
);
if( ! $posts ) return;

$out = '<select id="afa_select"><option>Anlagegut auswählen</option>';
foreach( $posts as $p )
{
    $out .= '<option value="' . get_permalink( $p ) . '">' . esc_html( $p->post_title ) . '</option>';  
 }
$out .= '</select>';
return $out;
}
  • Please add more details regarding what is nutzungsdauer and how is it related to the above drop down – Maruti Mohanty Dec 19 '13 at 12:27
  • nutzungsdauer is the name of a custom field. Every post of the post type 'abschreibungstabelle' has one custom field called nutzungsdauer. The custom field contains a value like 10 or 23 or 44 or ... – user1735856 Dec 19 '13 at 12:31
0

As the $posts are the posts of post_type abschreibungstabelle, you can get the custom field values by using get_post_custom_values

if( ! $posts ) return;

$out = '<select id="afa_select"><option>Anlagegut auswählen</option>';
foreach( $posts as $p )
{
  $custom_field_value = get_post_custom_values( 'nutzungsdauer', $p->ID );
  // Now you can use the value the way you want
    $out .= '<option value="' . get_permalink( $p->ID ) . '">' . esc_html( $p->post_title ) . '</option>';  
 }
$out .= '</select>';
return $out;
}
| improve this answer | |
0

I´ve tried to this. But I only get an array of the custom fields. What is the problem?

function afa_type_dropdown( $post_type )
{
$posts = get_posts(
array(
    'post_type'  => 'abschreibungstabelle',
    'numberposts' => -1,
)
);
if( ! $posts ) return;

 $out = '<select id="afa_select"><option>Anlagegut auswählen</option>';
 foreach( $posts as $p )
{

$nd = get_post_custom_values('nutzungsdauer', $p->ID);
// Now you can use the value the way you want
$out .= '<option value="' . get_permalink( $p->ID ) . '">' . esc_html( $p->post_title )    ." ".  $nd . '</option>';  
}
$out .= '</select>';
return $out;
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.