I have this code from the interwebs about how to duplicate a field in a form.
Now the problem is that this form is a text field, but I would like to have a drop down list with all pages from my website.
This would be easy if it only was in PHP since I don't know how to write jQuery
So is there anybody out there who is kind enough and willing to combine these two codes for me:
JQuery:
<script type="text/javascript">
var fieldname = <?php echo json_encode( $this->get_field_name('stream_sources') ) ?>;
var fieldnum = <?php echo json_encode( $stream_counter-1 ) ?>;
jQuery(function($) {
var count = fieldnum;
$('.<?php echo $this->get_field_id( 'add_field' );?>').click(function() {
$("#<?php echo $this->get_field_id( 'field_clone' );?>").append("<p><input type='text' name='"+fieldname+"["+(count+1)+"][title] value='' class='widefat sourc"+(count+1)+"'><span class='remove-field button button-primary button-large'>Verwijderen</span></p>");
count++;
});
$(".remove-field").live('click', function() {
$(this).parent().remove();
});
});
</script>
With this PHP:
<select name="meta-url-1" id="meta-url-1"><?php
global $post;
$args = array( 'numberposts' => -1);
$posts = get_pages($args);
echo '<option value="#"></option>';
foreach( $posts as $post ) : setup_postdata($post); ?>
<option value="<?php echo $post->post_name; ?>"><?php the_title(); ?></option>
<?php endforeach; ?>
</select>
Notice that the PHP is how I would write it. But I need to have the drop down in a repeatable field.
Hope this is possible && anyone would help.
M.
Your code is
<option value="<?php echo $post->post_name; ?>"><?php the_title(); ?></option>
In this case the value you are assigning in option will not repeat. You will need to assign a unique value to each option.
I suggest you to use a number variable to assign value.
Try below code:
<select name="meta-url-1" id="meta-url-1"><?php
global $post;
$args = array( 'numberposts' => -1);
$posts = get_pages($args);
$i = 0;
echo '<option value="#"></option>';
foreach( $posts as $post ) : setup_postdata($post); ?>
<option value="<?php echo $i++; ?>"><?php the_title(); ?></option>
<?php endforeach; ?>
</select>
Related
Please Help me! I want to show the Name of agent, but unfortunately, I get value instead of Agent name. I am new to PHP. This the code. What I do, i use this code.
<?php
if( !empty( $wpc_agents ) ) {
$agent = $wpcargo->get_shipment_agent( $post->ID );
?>
<select name="agent_fields">
<option value=""><?php _e('-- Select One --', 'wpcargo' ); ?></option>
<?php
foreach ($wpc_agents as $val) {
?>
<option value="<?php _e(sanitize_text_field($val->ID)); ?>" <?php selected( $agent, $val->ID ); ?> ><?php _e(sanitize_text_field($val->display_name)); ?></option>
<?php
}
?></select><?php
}
?>
And display value with this code
$agent = get_post_meta($shipment_id,
'agent_fields', true);
<div class="col-4">
<p class="label">
<?php _e('Agent Name', 'wpcargo'); ?>
</p>
<p class="label-info"><?php echo $agent; ?></p>
</div>
But this $agent giving me the ID of the agent instead of his name :(
Thanks in advance!
I have a custom taxonomy and I've added a category dropdown to the archive page to allow users to quickly switch between categories. I have the following code, which works in redirecting users to the new category that they choose. However, the dropdown always defaults to the first option in the <select>. How can I pass the selected <option> through and set it as the default value when a user switches categories?
<?php
$args = array(
'taxonomy' => 'custom_taxonomy',
'orderby' => 'name',
'show_count' => 1,
'hierarchical' => 1,
'title_li' => 'title'
);
<form id="categoriesform" action="<?php bloginfo('url'); ?>" method="post">
<div>
<?php $cats = get_categories($args); ?>
<select id="categories" name="custom_taxonomy">
<option value="allCategories">All Categories</option>
<?php foreach ($cats as $cat) : ?>
<option value="<?php echo get_term_link($cat, $cat->taxonomy) ?>">
<?php echo $cat->name ?></option>
<?php endforeach; ?>
</select>
</div>
</form>
<script>
jQuery(document).ready(function() {
jQuery('#categories').change(function() {
if (jQuery(this).val() == 'allCategories') {
window.location = 'http://example.com/all-categories';
} else {
window.location = jQuery(this).val();
}
});
});
</script>
First you need to get current category id via get_queried_object()->term_id; then you check and add selected in option.
<?php
$current=get_queried_object()->term_id;
$args = array(
'taxonomy' => 'custom_taxonomy',
'orderby' => 'name',
'show_count' => 1,
'hierarchical' => 1,
'title_li' => 'title'
);
<form id="categoriesform" action="<?php bloginfo('url'); ?>" method="post">
<div>
<?php $cats = get_categories($args); ?>
<select id="categories" name="custom_taxonomy">
<option value="allCategories">All Categories</option>
<?php foreach ($cats as $cat) : ?>
<option <?php echo ($current == $cat->term_id ? 'selected' : ''); ?> value="<?php echo get_term_link($cat, $cat->taxonomy) ?>">
<?php echo $cat->name ?></option>
<?php endforeach; ?>
</select>
</div>
</form>
<script>
jQuery(document).ready(function() {
jQuery('#categories').change(function() {
if (jQuery(this).val() == 'allCategories') {
window.location = 'http://example.com/all-categories';
} else {
window.location = jQuery(this).val();
}
});
});
</script>
i want to make dropdown for child categories, i have this code but it's not working ...
<select name="event-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'>
<option value=""><?php echo attribute_escape(__('Select Event')); ?></option>
<?php
$categories= get_categories('child_of=3');
foreach ($categories as $cat) {
$option = '<option value="/category/archive/'.$cat->category_nicename.'">';
$option .= $cat->cat_name;
$option .= ' ('.$cat->category_count.')';
$option .= '</option>';
echo $option;
}
?>
</select>
Argument have to array use this
$categories = get_categories( array( 'child_of' => 10 );
instead of
$categories= get_categories('child_of=3');
thanks but it's not working, i found another solution
<li id="categories"><h2><?php _e( 'category-name' ); ?></h2>
<?php wp_dropdown_categories('include=3,10' ); ?>
<script type="text/javascript">
<!--
var dropdown = document.getElementById("cat");
function onCatChange() {
if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
location.href = "<?php echo esc_url( home_url( '/' ) ); ?>?cat="+dropdown.options[dropdown.selectedIndex].value;
}
}
dropdown.onchange = onCatChange;
-->
</script>
</li>
Problems:
The save works, but is only storing one of the options in the database after save.
Options not showing as selected after save.
The select fields:
<select id="exclude_page_from_cookies" name="exclude_page_from_cookies[]" multiple="multiple">
<?php
$pages = get_pages();
foreach ( $pages as $page ) {
$title = $page->post_title;
$id = $page->id;
?>
<option id="<?php echo $id; ?>" value="<?php echo $title ?>" <?php selected( $title ); ?> >
<?php echo $title;?>
</option>
<?php
}
?>
</select>
The save function:
if ( isset( $_POST['exclude_page_from_cookies'] ) ) {
foreach( $_POST['exclude_page_from_cookies'] as $exclude_page ) {
echo $exclude_page;
update_option( 'exclude_page_from_cookies', $exclude_page ) ;
}
}
I'm assuming selected() is a wordpress function?
I guess your line should look like this:
<option <?php selected( $title ); ?> value="<?php echo $title ?>">
(with selected outside of value="")
EDIT
as per #comfreak:
foreach($_POST['exclude_page_from_cookies'] as $exclude_page ){
update_option('exclude_page_from_cookies',$exclude_page);
}
Hi I'm trying to create a function to show a list of posts in my admin menu of wordpress but because I'm calling the function a few times on the same page I needed to add some extra statements but it breaks the output and I don't know why
Here is my current code that outputs the basics:
function test() {
// The Query
query_posts( array ('posts_per_page' => -1 ) );
// The Loop
while ( have_posts() ) : the_post();
?>
<option value="<?php the_permalink() ?>"><?php the_title(); ?></option>
<?php endwhile;
// Reset Query
wp_reset_query();
}
Output code:
<select>
<?php test(); ?>
</select>
Returned Output:
<select>
<option value="http://website/posttitle1">POST TITLE 1</option>
<option value="http://website/posttitle2">POST TITLE 2</option>
<option value="http://website/posttitle3">POST TITLE 3</option>
</select>
But I need to add a select option on like this:
function test($select) {
// The Query
query_posts( array ('posts_per_page' => -1 ) );
// The Loop
while ( have_posts() ) : the_post();
if ($select == the_permalink()) { $selected = " selected"; }
?>
<option value="<?php the_permalink() ?>"><?php the_title(); ?></option><?php echo "\n"; ?>
<?php endwhile;
// Reset Query
wp_reset_query();
}
Output code:
<select>
<?php test("..GET Permalink from Database.."); ?>
</select>
But then this is my output:
<select>
http://website/posttitle1<option value="http://website/posttitle1">POST TITLE 1</option>
http://website/posttitle2<option value="http://website/posttitle2">POST TITLE 2</option>
http://website/posttitle3<option value="http://website/posttitle2">POST TITLE 3</option>
</select>
I dont understand?
the_permalink() print the value and get_permalink() returns the value.
Try this.
Change the following line
if($select == the_permalink()) { $selected = " selected"; }
to
if ($select == get_permalink()) { $selected = " selected"; }
And this line to
<option value="<?php the_permalink() ?>"><?php the_title(); ?></option><?php echo "\n"; ?>
this
<option value="<?php echo get_permalink() ?>"><?php echo get_the_title(); ?></option><?php echo "\n"; ?>