• Resolved rwilki

    (@rwilki)


    Great plugin. Easy to use and it’s so flexible for many purposes.

    I’m trying to hook into a simple drop down list of my most recent posts within a specific category.

    Basically, I’d like the user to select a post from a drop down box and this to be integrated into the contact form. This code grabs my recent posts from this category and puts into a drop down item but I can’t figure out if there’s a way to integrate it into contact form 7.

    <select name="events">
                         <option value="" selected="selected">Select an Event...</option>
                            <?php while (have_posts()) : the_post(); ?>
                            	<?php if (urldecode($alert[0]) == the_title()){ ?>
    								<option value="<?php the_title();?>"><?php the_title();?></option>
    							<?php } else { ?>
    								<option value="<?php the_title();?>"><?php the_title();?></option>
    							<?php } ?>
    						<?php endwhile; ?>

    Is this possible or should I try another approach?

    Thanks,
    Bob

    http://wordpress.org/extend/plugins/contact-form-7/

Viewing 8 replies - 1 through 8 (of 8 total)
  • Would like to know this as well.. Is there any code to use to get a custom function like the above?

    Hi guys,

    This is the code that makes it all work. Maybe not the best script? but it does really work. Simple add this function in 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);
    	$title = get_the_title();
    	$output .= "<option> $title </option>";
    
    	endforeach;
    $output .= "</select>";
    return $output;	
    
    }

    With the shortcode in contactform 7 [postdropdown]. This will show all the posts titles in category 5.

    Cheers!

    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.

    This is great! It doesn’t work when set with required (*). How can this be enabled?

    hello ,
    I want just to have the content of the post when I put the short code of contact form.

    Can you help me to do that .?
    Thanks a lot …

    Guies please help me out

    how can i achieve this from php dynamic values like

    <input type=”text” value=”<?php the_title(); ?>” />

    please help me out

    Thanks for the code

    Just want to share that I have modified the code so that it could be used within the blog pages without affecting the main loop

    I also added in parameters like name and max width

    wpcf7_add_shortcode('postdropdown', 'createbox', true);
    function createbox($tag){
    
    	$name = $tag['name'];
    	$options = (array) $tag['options'];
    	$values = (array) $tag['values'];
    
    	foreach ($options as $option) {
    		if (preg_match('%^(\d*)[/x](\d*)$%i', $option, $matches)) {
    			$size_att = (int) $matches[1];
    			$maxlen_att = (int) $matches[2];
    		}
    	}
    
    	$args = array('category_name' => 'property-list' );
    	$output = "<select name='".$name."' id='".$name."' onchange='document.getElementById(\"" . $name ."\").value=this.value;' style='min-width:".$maxlen_att."px; max-width:".$maxlen_att."px; border:none; margin-top: 5px; margin-bottom:5px;'><option></option>";
    	$query2 = new WP_Query( $args );
    
    	// The 2nd Loop
    	while( $query2->have_posts()):
    		$query2->next_post();
    		$output .= "<option value='$title'>".get_the_title( $query2->post->ID )."</option>";
    	endwhile;
    	$output .= "</select>";
    
    	return $output;
    }

    So the shortcode will become [postdropdown post_selection /100] and mailtemplate can be [post_selection]

    What if I want to shoow all the posts published by the user in a dropbox? I need this to let the contributors choose from this list the name of teh post they want the admin to be removed…

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘[Plugin: Contact Form 7] Add drop down list of recent posts’ is closed to new replies.