make child categories dropdown - php

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>

Related

Passing Selected Value in WordPress Category Dropdown

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>

Cascading dropdown select fields

I've updated my php code. I've managed to get all the parent taxonomies and their children in different select boxes. I'd like some help with the following: When I change the parent i'd like the second select to show his children only.
function categories_header_form()
{
?>
<div id="header-form">
<h3 class="form-title">
<?php echo 'Αναζήτηση προϊόντων ανά περιοχή' ?>
</h3>
<form id="search-form" action="#" method="post" >
<div class="form-container">
<?php nomoi(); ?>
<?php towns(); ?>
<?php products_selection(); ?>
<button type="submit" class="button" id="search-form-button">Εύρεση</button>
</div>
</form>
</div>
<?php
}
function products_selection()
{
$args = array(
'post_type' => 'seller',
'taxonomy' => 'category',
'hide_empty' => 0,
'exclude' => 1,1078,1079
);
$products = get_categories( $args );
if ( $products ) {
echo '<select id="products-select">';
echo '<option selected="" disabled="" value="0"><span>Προϊόντα</span></option>';
foreach ($products as $product) {
echo '<option class="product-name" id="'. $product->term_id .'">'. $product->name .'</option>';
}
echo '</select>';
}
}
function nomoi()
{
$args = array(
'post_type' => 'seller',
'taxonomy' => 'nomos',
'hide_empty'=> 0,
'parent' => 0
);
$categories = get_categories( $args );
if ( $categories ) {
// print_r($categories);
echo '<select id="nomoi-select">';
echo '<option selected="" disabled="" value="0"><span>Νομοί</span></option>';
foreach ($categories as $category) {
$id = $category->term_id;
$name = $category->name;
$taxonomy = $category->taxonomy;
echo '<option class="nomos" id="'. $id .'">'. $name .'</option>';
}
echo '</select>';
}
}
function towns()
{
$args = array(
'taxonomy' => 'nomos',
'hide_empty' => 0,
'hierarchical' => true,
'depth' => 1,
);
$cats = get_categories( $args );
echo '<select id="town-select">';
echo '<option selected="" disabled="" value="0"><span>Πόλεις</span></option>';
foreach ($cats as $cat) {
$cat_name = $cat->name;
$id = $cat->cat_ID;
echo '<option class="town" id="'. $id .'">'. $cat_name .'</option>';
}
echo '</select>';
}
Here is the most basic example i can give you..
You can improve this...
$("#nothingToSee").change(function() {
$("#sub1").css('display', 'none');
$("#sub2").css('display', 'none');
$("#sub3").css('display', 'none');
y = $(this).val();
$("#" + y).css('display', 'block');
});
#sub1 {
display: block;
}
#sub2,
#sub3 {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="nothingToSee" id="nothingToSee">
<option value="sub1">1</option>
<option value="sub2">2</option>
<option value="sub3">3</option>
</select>
<select name="nothingToSee" id="sub1">
<option value="1">1-1</option>
<option value="2">1-2</option>
<option value="3">1-3</option>
</select>
<select name="nothingToSee" id="sub2">
<option value="1">2-1</option>
<option value="2">2-2</option>
<option value="3">2-3</option>
</select>
<select name="nothingToSee" id="sub3">
<option value="1">3-1</option>
<option value="2">3-2</option>
<option value="3">3-3</option>
</select>

Create a drop down in JQuery with PHP values

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>

Options not showing "selected' after saving

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);
}

Wordpress Category Thumbnail List Plugin

I'm trying to tweak a Wordpress Plugin - Category Thumbnail List to display "Coming Soon" when the shortcode calls for posts in a category but there are none.
I've tried contacting the developer but had no luck.
I've modified the original code using the following but it doesn't display. Any ideas?
foreach($myposts as $post) :
setup_postdata($post);
if ( has_post_thumbnail() ) {
$link = get_permalink($post->ID);
$thmb = get_the_post_thumbnail($post->ID,'thumbnail');
$title = get_the_title();
$output .= '<div class="categoryThumbnailList_item">';
$output .= '' .$thmb . '<br/>';
$output .= '' .$title . '';
$output .= '</div>';
} else {
$output .= '<p>Coming Soon</p>';
}
endforeach;
Here's the full code:
<?php
/*
Plugin Name: Category Thumbnail List
Plugin URI: http://jonk.pirateboy.net/blog/category/bloggeriet/wordpress/plugins/
Description: Creates a list of thumbnail images, using the_post_thumbnail() in WordPress 2.9 and up.
Version: 1.11
Author: Jonk
Author URI: http://jonk.pirateboy.net
*/
$categoryThumbnailList_Order = stripslashes( get_option( 'category-thumbnail-list_order' ) );
if ($categoryThumbnailList_Order == '') {
$categoryThumbnailList_Order = 'date';
}
$categoryThumbnailList_OrderType = stripslashes( get_option( 'category-thumbnail-list_ordertype' ) );
if ($categoryThumbnailList_OrderType == '') {
$categoryThumbnailList_OrderType = 'DESC';
}
$categoryThumbnailList_Path = get_option('siteurl')."/wp-content/plugins/categoy-thumbnail-list/";
define("categoryThumbnailList_REGEXP", "/\[categorythumbnaillist ([[:print:]]+)\]/");
define("categoryThumbnailList_TARGET", "###CATTHMBLST###");
function categoryThumbnailList_callback($listCatId) {
global $post;
global $categoryThumbnailList_Order;
global $categoryThumbnailList_OrderType;
$tmp_post = $post;
$myposts = get_posts('numberposts=-1&&category='.$listCatId[1].'&&orderby='.$categoryThumbnailList_OrderType.'&&order='.$categoryThumbnailList_Order);
$output = '<div class="categoryThumbnailList">';
foreach($myposts as $post) :
setup_postdata($post);
if ( has_post_thumbnail() ) {
$link = get_permalink($post->ID);
$thmb = get_the_post_thumbnail($post->ID,'thumbnail');
$title = get_the_title();
$output .= '<div class="categoryThumbnailList_item">';
$output .= '' .$thmb . '<br/>';
$output .= '' .$title . '';
$output .= '</div>';
} else {
$output .= '<p>Coming Soon</p>';
}
endforeach;
$output .= '</div>';
$output .= '<div class="categoryThumbnailList_clearer"></div>';
$post = $tmp_post;
wp_reset_postdata();
return ($output);
$output = '';
}
function categoryThumbnailList($content) {
return (preg_replace_callback(categoryThumbnailList_REGEXP, 'categoryThumbnailList_callback', $content));
}
function categoryThumbnailList_css() {
global $categoryThumbnailList_Path;
echo "
<style type=\"text/css\">
#import url(\"".$categoryThumbnailList_Path."categoy-thumbnail-list.css\");
</style>
";
}
add_action('wp_head', 'categoryThumbnailList_css');
add_filter('the_content', 'categoryThumbnailList',1);
?>
<?php
add_action('admin_menu', 'my_plugin_menu');
function my_plugin_menu() {
add_options_page('Category Thumbnail List Options', 'Category Thumbnail List', 'manage_options', 'category-thumbnail-list', 'my_plugin_options');
}
function my_plugin_options() {
global $categoryThumbnailList_Order;
global $categoryThumbnailList_OrderType;
if( $_POST['save_category-thumbnail-list_settings'] ) {
// update order type
if( !$_POST['category-thumbnail-list_ordertype'] )
{
$_POST['category-thumbnail-list_ordertype'] = 'date';
}
update_option('category-thumbnail-list_ordertype', $_POST['category-thumbnail-list_ordertype'] );
// update order
if( !$_POST['category-thumbnail-list_order'] )
{
$_POST['category-thumbnail-list_order'] = 'DESC';
}
update_option('category-thumbnail-list_order', $_POST['category-thumbnail-list_order'] );
$categoryThumbnailList_Order = stripslashes( get_option( 'category-thumbnail-list_order' ) );
$categoryThumbnailList_OrderType = stripslashes( get_option( 'category-thumbnail-list_ordertype' ) );
echo "<div id=\"message\" class=\"updated fade\"><p>Your settings are now updated</p></div>\n";
}
?>
<div class="wrap">
<h2>Category Thumbnail List Settings</h2>
<form method="post">
<table class="form-table">
<tr valign="top">
<th scope="row">Order by</th>
<td>
<select name="category-thumbnail-list_ordertype" id="category-thumbnail-list_ordertype">
<option <?php if ($categoryThumbnailList_OrderType == 'date') { echo 'selected="selected"'; } ?> value="date">Date</option>
<option <?php if ($categoryThumbnailList_OrderType == 'title') { echo 'selected="selected"'; } ?> value="title">Title</option>
</select>
</td>
</tr>
<tr valign="top">
<th scope="row">Display order</th>
<td>
<select name="category-thumbnail-list_order" id="category-thumbnail-list_order">
<option <?php if ($categoryThumbnailList_Order == 'DESC') { echo 'selected="selected"'; } ?> value="DESC">Descending (z-a/9-1/2010-2001)</option>
<option <?php if ($categoryThumbnailList_Order == 'ASC') { echo 'selected="selected"'; } ?> value="ASC">Ascending (a-z/1-9/2001-2010)</option>
</select>
</td>
</tr>
</table>
<div class="submit">
<!--<input type="submit" name="reset_category-thumbnail-list_settings" value="<?php _e('Reset') ?>" />-->
<input type="submit" name="save_category-thumbnail-list_settings" value="<?php _e('Save Settings') ?>" class="button-primary" />
</div>
<div>
Update the thumbnail sizes here
</div>
<div>
You may need to update your css when changing the thumbnail size
</div>
</form>
</div>
<?php
}
?>
Many Thanks,
Mike
Haven't tested your code, but a quick glance shows at least one problem... you're using this outside of the loop. When not used in the loop, has_post_thumbnail() doesn't know what post you're checking. With just that modification, your code should be this:
foreach($myposts as $post) :
setup_postdata($post);
if ( has_post_thumbnail( $post->ID ) ) {
$link = get_permalink($post->ID);
$thmb = get_the_post_thumbnail($post->ID,'thumbnail');
$title = get_the_title();
$output .= '<div class="categoryThumbnailList_item">';
$output .= '' .$thmb . '<br/>';
$output .= '' .$title . '';
$output .= '</div>';
} else {
$output .= '<p>Coming Soon</p>';
}
endforeach;

Categories