• Hi guys,
    I am trying to create a custom dropdown (select) box in my Contactform with values (post titles from category id 5) from the database, called by a custom shortcode.

    To do so i put the following function/code in my functions.php

    wpcf7_add_shortcode('postdropdown', 'createbox');
    function createbox(){ 
    
    global $post;
    $args = array( 'category' => 5 );
    $myposts = get_posts( $args );
    $output = '<select name="lstdate" id="fleet" onchange="document.getElementById(\'fleet\').value=this.value;"><option></option>';
    foreach ( $myposts as $post ) : setup_postdata($post); 
    
    	$output .= "<option> the_title(); </option>";
    
    	endforeach;
    $output .= "</select>";
    return $output;	
    
    }

    When i add the shortcode [postdropdown] in my contactform a dropdown menu does show up, but the value is “the_title();” instead of the concerning post title from category_id 5.

    What am i doing wrong? Can anyone help me?
    thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter wzshop

    (@wzshop)

    Got it fixed already. Sometimes I’m totally blind:)

    The code to make this work is:

    wpcf7_add_shortcode('postdropdown', 'createbox');
    function createbox(){ 
    
    global $post;
    $args = array( 'category' => 5 );
    $myposts = get_posts( $args );
    $output = '<select name="lstdate" id="fleet" onchange="document.getElementById(\'fleet\').value=this.value;"><option></option>';
    foreach ( $myposts as $post ) : setup_postdata($post);
    	$title = get_the_title();
    	$output .= "<option> $title </option>";
    
    	endforeach;
    $output .= "</select>";
    return $output;	
    
    }

    With the shortcode in contactform 7 [postdropdown]

    Cheers!

    Thread Starter wzshop

    (@wzshop)

    To fully get it functioning, use this updated code:

    Updated code:

    wpcf7_add_shortcode('postdropdown', 'createbox', true);
    function createbox(){
    global $post;
    $args = array('numberposts' => 0, 'category' => 5 );
    $myposts = get_posts( $args );
    $output = "<select name='cursus' id='cursus' onchange='document.getElementById(\"cursus\").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;
    }

    With the shortcode [postdropdown cursus] in the contactform to show posts from category 5. Put shortcode [cursus] in the mailtemplate.

    cheers.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘[Plugin: Contact Form 7]Create a selectbox from database?’ is closed to new replies.