Get custom post types inside a wordpress widget


/ Published in: PHP
Save to your folder(s)

this creates a function inside a widget object to create a dropdown menu with all the custom post types created in wordpress functions.


Copy this code and paste it in your HTML
  1. function select_custom_posts($selected){
  2.  
  3. $out.='<select name="'.$this->get_field_name('ctype').'" >';
  4. $out.='<option value="">Seleccione un tipo</option>';
  5.  
  6. $args=array(
  7. '_builtin' => false
  8. );
  9.  
  10. $post_types=get_post_types($args,'names');
  11. foreach ($post_types as $post_type ) {
  12. $selec=($selected==$post_type)?'selected':'';
  13. $out.= '<option value="'.$post_type.'" '.$selec.'>'. $post_type. '</option>';
  14. }
  15.  
  16. $out.='</select>';
  17. echo $out;
  18.  
  19. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.