Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save govindak/af54bf3aa7d00f3d037b3dc1b2d94bc0 to your computer and use it in GitHub Desktop.
Save govindak/af54bf3aa7d00f3d037b3dc1b2d94bc0 to your computer and use it in GitHub Desktop.
Adding a custom Wordpress shortcode for building a dynamic dropdown of post type titles with Contact Form 7
wpcf7_add_shortcode('postdropdown', 'createbox', true);
function createbox(){
global $post;
extract( shortcode_atts( array( 'post_type' => 'some_post_type',), $atts ) );
$args = array('post_type' => $post_type );
$myposts = get_posts( $args );
$output = "<select name='postType' id='postType' onchange='document.getElementById(\"postType\").value=this.value;'><option></option>";
foreach ( $myposts as $post ) : setup_postdata($post);
$title = get_the_title();
$output .= "<option value='$title'> $title </option>";
endforeach;
$output .= "</select>";
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment