i've been at this for a couple of evening now, i've tried many different solutions and would rather keep it PHP rather than a jQuery solution or AJAX, here's my code, for some reason the Select Dropdown just won't keep it's value, any advice would be amazing. Thanks in advance.
<?php
$taxonomy = 'accommodation_area';
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => true
);
$tax_terms = get_terms( $taxonomy, $args );
?>
<select name="<? echo $taxonomy ?>" id="<? echo $taxonomy ?>" class="postform">
<option>Choose Area</option>
<?php if($tax_terms): ?>
<?php foreach ($tax_terms as $tax_term): ?>
<option value="<?php echo $tax_term->slug; ?>" <?php if ( isset( $_POST[ $taxonomy ] ) && ( $_POST[ $taxonomy ] == $tax_term->slug ) ) echo "selected";?>><?php echo $tax_term->name; ?></option>
<?php endforeach; ?>
<?php endif; ?>
</select>
Related
In a custom Woocommerce products page I filter posts_per_page with a dropdown menu. It works well and the table is updated with the new value.
But then if I click on the 'Next' button, next_posts_link doesn't post the new posts_per_page value, and the second page starts from record #11, as default.
Any solution?
<?php
$prodNum = $_POST['num_prods'];
?>
<!-- SELECT POST PER PAGES -->
<form id="numProdsForm" method="POST">
<select name="num_prods" id="num_prods" onchange="this.form.submit()">
<option value="10" <?php if ($prodNum==10){ ?> selected <?php } ?>>10</option>
<option value="20" <?php if ($prodNum==20){ ?> selected <?php } ?>>20</option>
<option value="30" <?php if ($prodNum==30){ ?> selected <?php } ?>>30</option>
<option value="50" <?php if ($prodNum==40){ ?> selected <?php } ?>>50</option>
<option value="100" <?php if ($prodNum==50){ ?> selected <?php } ?>>100</option>
</select>
</form>
<!-- CUSTOM TABLE -->
<table>
<?php if ( woocommerce_product_loop() ) { ?>
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => $prodNum,
'orderby'=>'title',
'order' => 'ASC'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
?>
<tr>
<td class="flex-row" role="cell"><?php echo $product->get_attribute( 'color' ); ?></td>
</tr>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<?php
} else {
do_action( 'woocommerce_no_products_found' );
}
?>
</table>
<div class="products-pagination">
<?php previous_posts_link( '« PREV', $loop->max_num_pages) ?>
<?php next_posts_link( 'NEXT »', $loop->max_num_pages) ?>
</div>
I've tried to put the pagination before wp_reset_query();, or to use get $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; but nothing changes.
You could try using a "GET" rather than "POST" for the form, and pass the offset and number of posts to show as URL params. Then you can add the offset attribute to the query. You can also optimize some of your code by using selected selected function definition rather than your if statements for the options.
<?php
$product_number = ( isset( $_GET['num_prods'] ) ) ? absint( $_GET['num_prods'] ) : 0;
$existing_offset = ( isset( $_GET['offset'] ) ) ? absint( $_GET['offset'] ) : 0;
$offset = $product_number + $existing_offset;
?>
<!-- SELECT POST PER PAGES -->
<form id="numProdsForm" method="GET">
<input type="hidden" name="offset" value="<?php echo esc_attr( $offset ); ?>">
<select name="num_prods" id="num_prods" onchange="this.form.submit()">
<option value=""> - - Choose Number of Products to Show -- </option>
<option value="10" <?php selected( 10, $product_number ); ?>>10</option>
<option value="20" <?php selected( 20, $product_number ); ?>>20</option>
<option value="30" <?php selected( 30, $product_number ); ?>>30</option>
<option value="50" <?php selected( 50, $product_number ); ?>>50</option>
<option value="100" <?php selected( 100, $product_number ); ?>>100</option>
</select>
</form>
<!-- CUSTOM TABLE -->
<table>
<?php if ( woocommerce_product_loop() ) { ?>
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => $product_number,
'offset' => $offset, // the offset.
'orderby' => 'title',
'order' => 'ASC',
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) :
$loop->the_post();
global $product;
?>
<tr>
<td class="flex-row" role="cell"><?php echo $product->get_attribute( 'color' ); ?></td>
</tr>
<?php endwhile; ?>
<?php wp_reset_postdata(); // use wp_reset_postdata(). ?>
<?php
} else {
do_action( 'woocommerce_no_products_found' );
}
?>
</table>
<div class="products-pagination">
<?php previous_posts_link( '« PREV', $loop->max_num_pages ); ?>
<?php next_posts_link( 'NEXT »', $loop->max_num_pages ); ?>
</div>
I am looking for a way to retain the selection from a a list of options which are created from within a WordPress post loop:
<?php
$args = array( 'post_type' => 'office_locations', 'posts_per_page' => -1, 'order_by' => 'title', 'order' => 'ASC' );
$loop = new WP_Query( $args ); ?>
<select style="width: 100%;" name="selectedValue" onchange="this.form.submit()">
<option disabled>Select an office location...</option> // This is disabled
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<option><?php echo get_the_title();?></option>
<?php endwhile; ?>
</select>
So if a User makes a selection I have set it to post that upon selection, is there a way for the select to remain on that option after this happens?
Sure, check the value of POST matches the value of the select, then set as selected:
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<option<?= (isset($_POST['selectedValue']) && $_POST['selectedValue'] == get_the_title() ? ' selected' : null) ?>><?php echo get_the_title();?></option>
<?php endwhile; ?>
I've got nested dropdown & I get format problems with more than 2 times nested items. Im pretty sure my "Mona Lisa" quality image made in Paint will demonstrate it perfectly.
Im looking for any suggestions what to change about my code to make it work properly.
IMAGE ABOUT MY PROBLEM:
CODE:
<div class="form-group">
<label for="p-location"><?php _e( 'Location', 'tt' ); ?></label>
<select name="p-location" id="p-location" class="form-control required" title="<?php _e( 'Please select the location.', 'tt' ); ?>" data-placeholder="<?php _e( 'Choose a location', 'tt' ); ?>">
<option value=""></option>
<?php
$locations = get_terms('p-location', array( 'orderby' => 'slug', 'parent' => 0, 'hide_empty' => false) );
if ( isset( $_GET['p_location'] ) ) {
$get_location = $_GET['p_location'];
}
else {
$get_location = '';
}
$p_locations = get_the_terms( $p_id , 'p-location' );
if ( !empty( $p_locations ) ) {
foreach ( $p_locations as $p_location ) {
$get_location = $p_location->term_id;
break;
}
}
?>
<?php foreach ( $locations as $key => $location ) : ?>
<option value="<?php echo $location->term_id; ?>" <?php selected( $location->term_id, $get_location ); ?>>
<?php
echo $location->name;
$location2 = get_terms( 'p-location', array( 'orderby' => 'slug', 'parent' => $location->term_id, 'hide_empty' => false ) );
if( $location2 ) :
?>
<optgroup>
<?php foreach( $location2 as $key => $location2 ) : ?>
<option value="<?php echo $location2->term_id; ?>" class="level2" <?php selected( $location2->term_id, $get_location ); ?>>
<?php
echo $location2->name;
$location3 = get_terms( 'p-location', array( 'orderby' => 'slug', 'parent' => $location2->term_id, 'hide_empty' => false ) );
if( $location3 ) : ?>
<optgroup>
<?php foreach( $location3 as $key => $location3 ) : ?>
<option value="<?php echo $location3->term_id; ?>" class="level3" <?php selected( $location3->term_id, $get_location ); ?>>
<?php
echo $location3->name;
$location4 = get_terms( 'p-location', array( 'orderby' => 'slug', 'parent' => $location3->term_id, 'hide_empty' => false ) );
if( $location4 ) :
?>
<optgroup>
<?php foreach( $location4 as $key => $location4 ) : ?>
<option value="<?php echo $location4->term_id; ?>" class="level4" <?php selected( $location4->term_id, $get_location ); ?>>
<?php echo $location4->name; ?>
</option>
<?php endforeach; ?>
</optgroup>
<?php endif; ?>
</option>
<?php endforeach; ?>
</optgroup>
<?php endif; ?>
</option>
<?php endforeach; ?>
</optgroup>
<?php endif; ?>
</option>
<?php endforeach; ?>
</select>
</div>
Can you show how it renders now in your browser? As mentioned here current HTML spec does not include multi-nested optgroups, so I'm not sure how you achieve 3rd level indentation there.
So there are two solutions of your problem:
easier is using x times = your current nested level. See here:
<body>
<select>
<option>Option 1</option>
<option>Option 2</option>
<!-- as many 's as the nest level you need -->
<option> Option 3</option>
<option> Option 4</option>
<!-- as many 's as the nest level you need -->
<option> Option 3</option>
<option> Option 4</option>
<!-- as many 's as the nest level you need -->
<option> Option 5</option>
<option> Option 6</option>
</select>
</body>
using kind of UI library such as this one + indenting your groups with CSS paddings
I have 2 custom post types named clients and casestudies. I'm trying to build a meta box on the clients post type that will have a drop down list featuring the titles of all posts from the casestudies post type. This will end up with a page displaying the featured image from the clients post type, then hyperlink off to the relevant casestudies post if a selection is made from the drop down list.
I have followed this tutorial to get a meta box put together: http://code.tutsplus.com/tutorials/how-to-create-custom-wordpress-writemeta-boxes--wp-20336
This is the meta box code I have in my functions.php file:
add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function cd_meta_box_add()
{
add_meta_box( 'my-meta-box-id', 'My First Meta Box', 'cd_meta_box_cb', 'clients', 'side', 'default' );
}
function cd_meta_box_cb( $post )
{
$values = get_post_custom( $post->ID );
$selected = isset( $values['my_meta_box_select'] ) ? esc_attr( $values['my_meta_box_select'][0] ) : ”;
?>
<p>
<label for="my_meta_box_select">Select which case study this logo will link to when it is clicked:<br /><br /></label>
<select name="my_meta_box_select" id="my_meta_box_select" style="width:100%;">
<option value="No case study">No case study</option>
<?php
$casestudies = array( 'post_type' => 'casestudies', 'orderby' => 'title', 'order' => 'asc', );
$casestudiesloop = new WP_Query( $casestudies );
while ( $casestudiesloop->have_posts() ) : $casestudiesloop->the_post();
?> <option value="<?php the_title(); ?>" <?php selected( $selected, $casestudies['the_title'] ); ?> ><?php the_title(); ?></option>
<?php
endwhile;
?>
</select>
</p>
<?php
}
add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $post_id )
{
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
if( !current_user_can( 'edit_post', $post_id ) ) return;
if( isset( $_POST['my_meta_box_select'] ) )
update_post_meta( $post_id, 'my_meta_box_select', esc_attr( $_POST['my_meta_box_select'] ) );
}
The meta box displays correctly on the correct post type, but when I update the post it won't save the data.
Thanks.
You don't have nonce hidden field. Save function would return nothing.
<input type="hidden" name="meta_box_nonce" id="meta_box_nonce" value="<?php echo wp_create_nonce( 'my_meta_box_nonce' ); ?>" />
UPDATE:
So your cd_meta_box_cb function would be
<?php
function cd_meta_box_cb( $post )
{
$values = get_post_custom( $post->ID );
$selected = isset( $values['my_meta_box_select'] ) ? esc_attr( $values['my_meta_box_select'][0] ) : ”;
?>
<p>
<label for="my_meta_box_select">Select which case study this logo will link to when it is clicked:<br /><br /></label>
<select name="my_meta_box_select" id="my_meta_box_select" style="width:100%;">
<option value="No case study">No case study</option>
<?php
$casestudies = array( 'post_type' => 'casestudies', 'orderby' => 'title', 'order' => 'asc', );
$casestudiesloop = new WP_Query( $casestudies );
while ( $casestudiesloop->have_posts() ) : $casestudiesloop->the_post();
?> <option value="<?php the_title(); ?>" <?php selected( $selected, $casestudies['the_title'] ); ?> ><?php the_title(); ?></option>
<?php
endwhile;
?>
</select>
</p>
<input type="hidden" name="meta_box_nonce" id="meta_box_nonce" value="<?php echo wp_create_nonce( 'my_meta_box_nonce' ); ?>" />
<?php
}
?>
i make meta box and in this i make select option with multiple
$opt_meta_author = get_post_meta($post->ID, 'opt_meta_author', true);
<select name="opt_meta_author" id="opt_meta_author" multiple="multiple">
<?php
$auth_args = array(
'post_type' => 'author',
'orderby' => 'title',
'order' => 'ASC'
);
$authors = new WP_Query($auth_args);
while($authors->have_posts()) :
$authors->the_post();
?>
<option value="<?php echo the_title() ?>">
<?php echo the_title() ?>
</option>
<?php
endwhile;
?>
</select>
when i select multiple options it save only one option, i want to save selected options
is there any suggestions
Saving meta values
$opt_meta_author = $_POST['opt_meta_author'];
update_post_meta( $post->ID, 'opt_meta_author', $opt_meta_author);
$opt_meta_author = unserialize(get_post_meta($post->ID, 'opt_meta_author', true));
<select name="opt_meta_author" id="opt_meta_author" multiple="multiple">
<?php
$auth_args = array(
'post_type' => 'author',
'orderby' => 'title',
'order' => 'ASC'
);
$authors = new WP_Query($auth_args);
while($authors->have_posts()) :
$authors->the_post();
?>
<option value="<?php echo the_title() ?>">
<?php echo the_title() ?>
</option>
<?php
endwhile;
?>
</select>
To get multiple selected value while storing do this:
$opt_meta_author = serialize($_POST['opt_meta_author']);
What is the var dump of $_POST['opt_meta_author'])? if it is an array, convert it in string using implode and save the string in database