WP populate select dropdown - php

I have two html selects with data from WordPress:
a) select list with cities
<?php
$args = array( 'child_of' => 50, 'meta_key' => 'E-mail' );
$pages = get_pages( $args );
echo '<select id="towns" name="towns">';
echo '<option value="" selected="selected">Please choose a town...</option>';
foreach($pages as $post) {
$page_id = get_the_ID();
$page_meta = get_post_meta(get_the_ID(),'E-mail',true);
$page_title = get_the_title($ID);
echo '<option value="' . $page_meta .'">'. $page_title . '</option>';
}
echo '</select>';
?>
b) select list with street names
<?php
//$page_id = 62;
echo '<select id="streets" streets="kierunki">';
echo '<option value="" selected="selected">Please choose a street</option>';
$tags = wp_get_post_tags($page_id);
foreach ($tags as $tag) {
echo '<option value="' . $tag->name . '">'. $tag->name . '</option>';
}
echo '</select>';
}
?>
Is it possible to pass value of $page_id to second select so when I choose for example first option in first select dropdown, second select list will appear with corresponding streets?

Related

PHP: Wordpress cycle through categories and output all posts images

I have a bootstrap "tab" system going on where each tab is it's own category name:
<?php $categories= get_categories();
$firstCat = 1;
foreach ($categories as $cat) {
$trimmedCatName = str_replace(' ', '', $cat->cat_name);
echo '<li';
if ($firstCat == 1) {
echo ' class="active"';
}
echo '>'.''.$cat->cat_name.' <small style="color:#447294">('.$cat->category_count.')</small></li>';
$firstCat++;
}
?>
This above ^ code works fine and sets the tabs up nicely.
The problem I have is with cycling through the categories as "tab-content" and then for each separate category, showing all the post titles/images for that category. Here's what I have so far:
<div class="tab-content">
<?php $categories= get_categories();
$firstCat = 1;
foreach ($categories as $cat) {
$trimmedCatName = str_replace(' ', '', $cat->cat_name);
echo '<div class="tab-pane ';
if ($firstCat == 1) {
echo 'active';
}
echo '" id="#'.$trimmedCatName.'">'.
'<select class="image-picker">';
$posts = get_posts($cat);
if ($posts) {
foreach ($posts as $p) {
echo '<option>';
echo get_the_post_thumbnail( $p->ID, 'medium' ).'<br>';
echo '</option>';
}
}
echo '</select>';
$firstCat++;
}
?>
</div>
I'm confused on how to get this code correctly.
<div class="tab-content">
<?php $categories= get_categories();
$firstCat = 1;
foreach ($categories as $cat) {
$trimmedCatName = str_replace(' ', '', $cat->cat_name);
echo '<div class="tab-pane ';
if ($firstCat == 1) {
echo 'active';
}
echo '" id="#'.$trimmedCatName.'">'.
'<select class="image-picker">';
$posts = get_posts(array('category' => $cat->term_id));
if ($posts) {
foreach ($posts as $p) {
echo '<option>';
echo get_the_post_thumbnail( $p->ID, 'medium' ).'<br>';
echo '</option>';
}
}
echo '</select>';
$firstCat++;
}
?>
</div>
notice i replaced $cat->term_id
if you are still getting nothing try hardcoding the category ID and see if you get any results.
try this..
add_action('init','test');
function test(){
var_dump(get_posts(array('category' => 1)));
}

make child categories dropdown

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>

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>

Osclass change category style

The Osclass is a really good tool, but I have some problems with it.
I have this code:
<div class="cell selector">
<?php osc_categories_select('sCategory', null, __('Select country/city', 'bender')) ; ?>
</div>
and this code renders this HTML:
<div class="cell selector">
<div class="select-box undefined"><select name="sCategory" id="sCategory" style="opacity: 0;"><option value="">Select a category</option><option value="98">Key West</option><option value="101"> Miami</option><option value="99">Italy</option><option value="102"> Rome</option><option value="97">Madives</option><option value="96">France</option><option value="4">United States</option><option value="43"> Houses - Apartments for Sale</option><option value="44"> Houses - Apartments for Rent</option><option value="45"> Rooms for Rent - Shared</option><option value="46"> Housing Swap</option><option value="47"> Vacation Rentals</option><option value="48"> Parking Spots</option><option value="49"> Land</option><option value="50"> Office - Commercial Space</option><option value="51"> Shops for Rent - Sale</option></select><span class="select-box-label">Select a category</span><span class="select-box-icon">0</span></div> </div>
SO as you can see I don't have control over: <?php osc_categories_select('sCategory', null, __('Select country/city', 'bender')) ; ?>
How and where I can change this? I need to put my classes because I want to create a new theme with a new style? Anybody knows?
This should be in oc-includes > osclass > fm > Category.form.class.php
under:
static public function category_select($categories, $category, $default_item = null, $name = "sCategory")
{
echo '<select name="' . $name . '" id="' . $name . '">';
if(isset($default_item)) {
echo '<option value="">' . $default_item . '</option>';
}
foreach($categories as $c) {
echo '<option value="' . $c['pk_i_id'] . '"' . ( ($category['pk_i_id'] == $c['pk_i_id']) ? 'selected="selected"' : '' ) . '>' . $c['s_name'] . '</option>';
if(isset($c['categories']) && is_array($c['categories'])) {
CategoryForm::subcategory_select($c['categories'], $category, $default_item, 1);
}
}
echo '</select>';
}

Use foreach loop with counter

I want to output the results of a foreach statement but I want them grouped into a div in 3's
So like:
<div>image image image</div>
<div>image image image</div>
<div>image image image</div>
Here is my code so far:
$args = array( 'numberposts' => 3, 'offset'=> 0, 'category' => 9 );
$myrows = get_posts($args);
foreach($myrows as $row) { ?>
<div>
<?php if ( has_post_thumbnail($row->ID)) {
echo '<a href="' . get_permalink( $row->ID ) . '" title="' . esc_attr($row->post_title ) . '">';
echo get_the_post_thumbnail($row->ID);
echo '</a>';
}?>
</div>
<?php } ?>
$myrows = get_posts($args);
$chunks = array_chunk($myrows,3);
?>
<?php foreach($chunks as $myrows): ?>
<div>
<?php foreach($myrows as $row): ?>
<div>
<?php if(has_post_thumbnail($row->ID)): ?>
<a href="<?=get_permalink($row->ID)?>" title="<?=esc_attr($row->post_title)?>">
<?=get_the_post_thumbnail($row->ID)?>
</a>
<?php endif ?>
</div>
<?php endforeach ?>
</div>
<?php endforeach ?>
You can create blocks using array_chunk():
foreach (array_chunk($myrows) as $mychunk) {
echo '<div>';
foreach ($mychunk as $row) {
// print your entries
if (has_post_thumbnail($row->ID)) {
echo sprintf('%s',
get_permalink( $row->ID ),
esc_attr($row->post_title ),
get_the_post_thumbnail($row->ID)
);
}
}
echo '</div>';
}
Granted, if the if condition isn't met, you would get blocks of zero, one or two items instead of the expected three.
<div>
<?php
$args = array('numberposts' => 3, 'offset' => 0, 'category' => 9);
$myrows = get_posts($args);
foreach($myrows as $idx => $row) {
if ($idx % 3 == 0) echo "</div><div>";
if (has_post_thumbnail($row->ID)) {
echo '<a href="' . get_permalink($row->ID) . '" title="' . esc_attr($row->post_title) . '">';
echo get_the_post_thumbnail($row->ID);
echo '</a>';
} ?>
</div>
Why not use the modulo operator?
$counter = 0;
echo '<div>';
foreach ($myrows as $row)
{
$counter++;
if ($counter % 3 == 0) echo '</div><div>';
echo $row;
}
echo '</div>';
Try this code.
<?php
$args = array( 'numberposts' => 3, 'offset'=> 0, 'category' => 9 );
$myrows = get_posts($args);
$tempCnt=0;
foreach($myrows as $row) {
//12
if($tempCnt==3)
{
$tempCnt=0;
//do your reset code here.
}
$tempCnt++;
?>
<div>
<?php if ( has_post_thumbnail($row->ID)) {
echo '<a href="' . get_permalink( $row->ID ) . '" title="' . esc_attr($row->post_title ) . '">';
echo get_the_post_thumbnail($row->ID);
echo '</a>';
}?>
</div>
<?php } ?>

Categories