I have a theme options page and a loop which grabs my categories. This code works fine and I am able to save my options. The code like this:
function drop_elements_function(){
$my_cats = get_categories();
$i = 1;
foreach( $my_cats as $my_cat ) :
$my_categories[$my_cat->cat_ID] = array(
'value' => $my_cat->cat_ID,
'label' => $my_cat->cat_name
);
$i++;
endforeach;
$options = get_option('sandbox_theme_social_options');
echo'<select id="featured_cat" name="sandbox_theme_social_options[Drop_Elements]">';
foreach ( $my_categories as $category ){
$label = $category['label'];
$selected = '';
if ( $options['Drop_Elements'] == $category['value'] )
$selected = 'selected="selected"';
echo '<option style="padding-right: 10px;" value="' . esc_attr( $category['value'] ) . '" ' . $selected . '>' . $label . '</option>';
}
echo '</select>';
//print_r($options['Drop_Elements']);
}/*Function end*/
The output looks like this:
http://www.vasinternetposao.com/img.png
Problem: Now when my theme is installed for the first time i am getting the output like the screen shot above but obviously my option is not yet saved to the database (user must click the save button in order to select that category). So i was thinking to do something like this:
1.) User install the theme for the first time and then output looks like this:
http://www.vasinternetposao.com/img2.png
2.) If user unintentionally select "Choose your category" and clicks the "save button"
the output will be again:
http://www.vasinternetposao.com/img2.png
3.) If the user select the real category (not "Choose Your Category") then "Choose Your Category" Disappears:
http://www.vasinternetposao.com/img.png
This is my attempt but it is not working:
function drop_elements_function(){
$my_cats = get_categories();
$i = 1;
foreach( $my_cats as $my_cat ) :
$my_categories[$my_cat->cat_ID] = array(
'value' => $my_cat->cat_ID,
'label' => $my_cat->cat_name
);
$i++;
endforeach;
$options = get_option('sandbox_theme_social_options');
echo'<select id="featured_cat" name="sandbox_theme_social_options[Drop_Elements]">';
foreach ( $my_categories as $category ){
$label = $category['label'];
$selected = '';
if ( $options['Drop_Elements'] == $category['value'] ){
$selected = 'selected="selected"';
echo '<option style="padding-right: 10px;" value="' . esc_attr( $category['value'] ) . '" ' . $selected . '>' . $label . '</option>';
}
elseif(!isset($options['Drop_Elements'])){
$selected = 'selected="selected"';
echo '<option selected="selected" value="Choose Your Category">Choose Your Category</option>';
echo '<option style="padding-right: 10px;" value="' . esc_attr( $category['value'] ) . '" ' . '>' . $label . '</option>';
}
}/*Foreach close*/
echo '</select>';
//print_r($options['Drop_Elements']);
}/*Function end*/
Can it be done with PHP? Any help is appreciated! Thank you!
I think what you want to do is something like the following example:
echo '<select id="featured_cat" name="sandbox_theme_social_options[Drop_Elements]">';
echo '<option selected="selected" value="Choose Your Category">Choose Your Category</option>';
foreach ( $my_categories as $category ){
$label = $category['label'];
$selected = ( $options['Drop_Elements'] == $category['value'] ) ? " selected='selected' " : "";
echo '<option style="padding-right: 10px;" value="' . esc_attr( $category['value'] ) . '" ' . $selected . '>' . $label . '</option>';
}
echo "</select>";
Related
My code:
<?php
function filter_profiles_by_country()
{
$url = get_site_url();
if ( $terms = get_terms( array('taxonomy' => 'country','orderby' => 'name') ) )
{
// if categories exist, display the dropdown
echo '<select name="categoryfilter" onchange="if (this.value) window.location.href=this.value">';
echo ' <option value="'.$url.'/profiles">All Profiles...</option>';
foreach ( $terms as $term )
{
// ID of the category as an option value
echo ' <option value="'.$url ."/country/". $term->name . '">' . $term->name . '</option>';
}
echo '</select>';
}
}
?>
When I click on All Profiles, it should take me to /profiles/ page. But it is not working.
<?php
function filter_profiles_by_country(){
$url = get_site_url();
global $wp;
$current_url = home_url(add_query_arg(array(), $wp->request));
if( $terms = get_terms( array(
'taxonomy' => 'country',
'orderby' => 'name'
) ) ) :
// if categories exist, display the dropdown
echo '<select name="categoryfilter" onchange="if (this.value) window.location.href=this.value"><option value="'.$url.'/profiles/">All Profiles...</option>';
foreach ( $terms as $term ) :
$loadedItem = $url."/country/".$term->name;
$selectedItem = ($current_url == $loadedItem)? "selected": "";
echo '<option '.$selectedItem.' value="'.$url ."/country/". $term->name . '">' . $term->name . '</option>'; // ID of the category as an option value
endforeach;
echo '</select>';
endif;
}
?>
Try this.
I am displaying a list of filters using Isotope. The functionality works when the first part of the code works however, when the screen is resized the posts don't filter when it executes the last part... Any help is much appreciated!
<?php
$filterLinks = array();
$newarray = array();
$starter = array('name'=>'View All','slug'=>'*');
$filterLinks[] = $starter;
$taxterms = get_terms( $customTax );
if ( ! empty( $taxterms ) && ! is_wp_error( $taxterms ) ){
foreach ( $taxterms as $taxterm ) {
$datafilter = '.' . $taxterm->slug;
$newarray = array(
'name' => $taxterm->name,
'slug' => $datafilter,
);
$filterLinks[] = $newarray;
}
}
echo '<ul id="filters" class="desk-filters button-group">' ."\n";
foreach ($filterLinks as $links) {
echo '<li><button class="button" data-filter="' . $links['slug'] . '">' . $links['name'] . '</button></li>'."\n";
}
echo '</ul>';
// Drop down menu on mobile
echo '<div id="filters" class="resp-filters button-group">'."\n";
echo '<div class="resp-filter-btn">Select Filter</div>'."\n";
echo '<div class="resp-filter-content">'."\n";
foreach ($filterLinks as $links) {
echo '<a class="button" data-filter="' . $links['slug'] . '">' . $links['name'] . '</a>'."\n";
}
echo '</div>'."\n";
echo '</div>';
?>
Functionality Works:
echo '<ul id="filters" class="desk-filters button-group">' ."\n";
foreach ($filterLinks as $links) {
echo '<li><button class="button" data-filter="' . $links['slug'] . '">' . $links['name'] . '</button></li>'."\n";
}
echo '</ul>';
Functionality Doesn't Work:
// Drop down menu on mobile
echo '<div id="filters" class="resp-filters button-group">'."\n";
echo '<div class="resp-filter-btn">Select Filter</div>'."\n";
echo '<div class="resp-filter-content">'."\n";
foreach ($filterLinks as $links) {
echo '<a class="button" data-filter="' . $links['slug'] . '">' . $links['name'] . '</a>'."\n";
}
echo '</div>'."\n";
echo '</div>';
Is this because i'm executing it twice, just hiding one on desktop and showing the other on mobile?
Looking for some direction. Thanks in advance!
You're using id="filters" twice, which is improper use of the id attribute, since it should be unique. Not sure if that is of any concern?
this is my taxonamy code in word press
<?php
$terms = get_terms(['taxonomy' => 'listing_type', 'hide_empty' => false,]);
if ($terms) {
foreach ($terms as $term) {
$selected = 'selected';
echo '<option value="' . $term->name . '" '.$selected.' >' .esc_attr($term->name) . '</option>';
}
} ?>
Instead of writing this manually you can just use wp_dropdown_categories()
Docs here — https://codex.wordpress.org/Function_Reference/wp_dropdown_categories
Thanks,
Tom
The below code works really well for pulling in a select option list for all the entries in a custom taxonomy called 'make'. It only pulls in parent level ones which is perfect.
<select id="make" name="make">
<option value="">--</option>
<?php $term_query = new WP_Term_Query( array( 'taxonomy' => 'make', 'parent' => 0 ) ); if ( ! empty( $term_query->terms ) ) {
foreach ( $term_query ->terms as $term ) {
echo '<option class="' . $term->name . '" value="' . $term->name . '">' . $term->name . '</option>';
}
} else {
};?>
</select>
However, i would like to create another identical version of this code but instead of pulling in a list of all the parent values, it should pull in all the child values. Not for a particular page or post, but should output a select list of ALL the child entries of a custom taxonomy, ignoring the parents. Any help is massively appreciated!
you could use get_term_children and get_term_by functions to build your second select:
<select id="make" name="make">
<option value="">--</option>
<?php
$term_query = new WP_Term_Query( array( 'taxonomy' => 'make', 'parent' => 0 ) );
if ( ! empty( $term_query->terms ) ) {
foreach ( $term_query ->terms as $term ) {
$term_children = get_term_children( $term->term_id, 'make' );
foreach($term_children as $children){
$child = get_term_by( 'id', $children, 'make' );
echo '<option class="' . $child->name . '" value="' . $child->name . '">' . $child->name . '</option>';
}
}
} else {
};
?>
</select>
I am trying to write a content rating script where the user can select what type of rating to give to their article (for instance, what age group the articles suits for).
I am using a Wordpress star rating script as a template.
This part of the script is where the user selects the rating:
function pn_apr_meta_box_form( $post )
{
wp_nonce_field( 'pn_apr_meta_box_nonce', 'pn_apr_meta_box_nonce_field' );
$current_post_rating = get_post_meta( $post->ID, PN_APR_RATING_META_KEY, true );
echo '<label for="pn_apr_rating">' . __( 'Choose a rating for this post:', 'author-post-ratings' ) . '</label> ';
echo '<select name="pn_apr_rating" id="pn_apr_rating">';
echo '<option value="unrated"' . selected( $current_post_rating, 0, false ) . '>' . __( 'Unrated', 'author-post-ratings' ) . '</option>';
for ( $i = 1; $i <= 5; $i++ ) {
echo '<option value="' . $i . '"' . selected( $current_post_rating, $i, false ) . '>' . sprintf( _n( '%1s Star', '%1s Stars', $i, 'author-post-ratings' ), $i ) . '</option>';
}
echo '</select>';
}
This part of the script outputs the ratings:
if ( $rating ) {
$output = null;
$output .= '<div class="author-post-rating">';
$output .= '<span class="author-post-rating-label">' . esc_attr( $pn_apr_settings['label_text'] ) . '</span> ';
$output .= '<span class="author-post-rating-stars" title="' . sprintf( __( '%1$d out of %2$d stars', 'author-post-ratings' ), $rating, 5 ) . '">';
// Output active stars
for ( $i = 1; $i <= $rating; $i++ ) {
$output .= '<img src="' . PN_APR_PLUGIN_DIR_URL . 'images/star-active.png" />';
}
// Output inactive stars
for ( $i = $rating + 1; $i <= 5; $i++ ) {
$output .= '<img src="' . PN_APR_PLUGIN_DIR_URL . 'images/star-inactive.png" />';
}
$output .= '</span>' . "\n";
$output .= '</div><!-- .author-post-rating -->';
if ( true == $return ) { return $output; }
// We don't need to use "else" here, since calling return will automatically stop further execution of this function.
echo $output;
}
Now, I want to change this script so that it becomes a content rating script (rather than star rating one). I want to offer these choices for the user:
G — Suitable for all audiences
PG — Possibly offensive, usually for audiences 13 and above
R — Intended for adult audiences above 17
X — Even more mature than above
Question: How can I change the script so that if the user selects for instance PG, then it will output the text Suitable for age 13 and up.
EDIT:
To Shawn, observe the following code:
function rating_select_cb( $post ) {
global $wpdb;
$value = get_post_meta($post->ID, 'rating', true);
echo '<div class="misc-pub-section misc-pub-section-last"><span id="timestamp"><label>Select a rating:<br></label>';
// build an array of each available rating
$ratings = array(
1 => 'G — Suitable for all audiences',
2 => 'PG — Possibly offensive, usually for audiences 13 and above',
3 => 'R — Intended for adult audiences above 17',
4 => 'X — Even more mature than above'
);
echo '<select name="rating">';
echo '<option value=""' . ((($value == '') || !isset($ratings[$value])) ? ' selected="selected"' : '') . '> None... </option>';
// output each rating as an option
foreach ($ratings as $id => $text) {
echo '<option value="' . $id . '"' . (($value == $id) ? ' selected="selected"' : '') . '">' . $text. '</option>';
}
echo '</select>';
echo '</span></div>';
}
Here's something that should get you to where you need to be:
Add to your functions.php:
add_action( 'add_meta_boxes', 'rating_select_box' );
function rating_select_box() {
add_meta_box(
'rating_select_box', // id, used as the html id att
__( 'Select rating:' ), // meta box title, like "Page Attributes"
'rating_select_cb', // callback function, spits out the content
'post', // post type or page. We'll add this to posts only
'side', // context (where on the screen
'low' // priority, where should this go in the context?
);
}
function rating_select_cb( $post )
{
global $wpdb;
$value = get_post_meta($post->ID, 'rating', true);
echo '<div class="misc-pub-section misc-pub-section-last">
<span id="timestamp">'
. '<label>Select a rating:<br></label>';
$selected = ($value == $result->post_name) ? ' selected="selected" ' : null;
echo '<select name="rating">';
echo '<option value="" default="default"> None... </option>';
echo '<option value="0" '.$selected.'> G — Suitable for all audiences </option>';
echo '<option value="1" '.$selected.'> PG — Possibly offensive, usually for audiences 13 and above </option>';
echo '<option value="2" '.$selected.'> R — Intended for adult audiences above 17 </option>';
echo '<option value="3" '.$selected.'> X — Even more mature than above </option>';
echo '</select>';
echo '</span></div>';
}
add_action( 'save_post', 'save_metadata');
function save_metadata($postid)
{
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return false;
if ( !current_user_can( 'edit_page', $postid ) ) return false;
if( empty($postid) ) return false;
if ( is_null($_REQUEST["rating"]) ) {
delete_post_meta($postid, 'rating');
} else {
update_post_meta($postid, 'rating', $_REQUEST['rating']);
}
}
The above code does this:
1. Adds a new metabox to your posts
2. Adds a select box with the rating values
3. Saves the metadata with the post
To access your metadata in your templates:
$meta = get_post_custom($post->ID);
echo $meta['rating'][0];
To have your template display a custom string use something like:
switch ( $meta['rating'][0] ) {
case 0:
echo "This is rated PG";
break;
case 1:
echo "This is rated G";
break;
case 2:
echo "This is rated R";
break;
case 3:
echo "Ug oh! This is rated X!";
break;
default:
echo "This is not yet rated.";
}
**Edit: This code provides full functionality.. you could abandon your current solution if it works for you