How do I send this dynamic form in PHP? - php

I have a Wordpress loop and I have built a dynamic form from it, how would I now grab all the form data and send it?
$items = array();
$queryBasket = "SELECT * FROM baskets";
$gotBasket = $connect->query($queryBasket);
while ($basket = $gotBasket->fetch_assoc()) {
//echo $basket['item'];
$items[] = $basket['item'];
}
$args = array(
'post_type' => array('product' ),
'order' => 'ASC',
'orderby' => 'title',
'post__in' => $items
);
$loop = new WP_Query($args);
$i = 0;
if ( $loop->have_posts() ) {
echo '<ul id="basket" class="row">';
while ( $loop->have_posts() ) {
$loop->the_post();
$i++;
?>
<li class="col-sm-12">
x
<?php the_title(); ?>
Quantity: <input type="number" name="item-<?php echo $i; ?>-count" id="item-<?php echo $i; ?>-count" class="item-count"/>
<input type="hidden" name="item-<?php echo $i; ?>-name" id="item-<?php echo $i; ?>-name" value="<?php the_title(); ?>">
<?php }
echo '</ul>';
} else {
}
wp_reset_postdata();
?>
<input type="submit" id="submit" name="submit" value="Submit" />
</form>

make it an array:
name="item[<?php echo $i; ?>][name]"
you can then iterate over it
foreach ($_POST['item'] as $i => $values) {
echo "Name for $i: " . $values['name'];.
}

Related

Echo array terms

I'm looking to echo part of an array, what is the best method of echo'ing part of an array into a data field
I have an array that has the terms => subscription and need to pull that field into my input data-subscription="[echo subscription in here ]"
$quantites_required = false;
$previous_post = $post;
$count = 0;
$tax_query_args = [
'post_type' => 'product',
'tax_query' => [
[
'taxonomy' => 'product_type',
'field' => 'name',
'terms' => 'subscription',
],
],
];
$tax_query = get_posts($tax_query_args);
global $grouped_product;
foreach ($tax_query as $post) {
global $post;
$price = get_post_meta(get_the_ID(), '_price', true);
?>
<li id="product-<?php the_ID();?>" <?php post_class();?>>
<div class="prod-name">
<div class="radio">
<input type="checkbox" id="product-<?php the_ID();?>" value="1" name="quantity[<?php the_ID();?>]" class="wc-grouped-product-add-to-cart-checkbox js-group-item-sel" <?php echo ($count != 0 ? '' : ' checked="checked"'); ?> data-subscription="[echo terms subrciptions here]" />
<label for="product-<?php the_ID();?>">
<?php echo the_title(); ?>
</label>
</div>
</div>
<div class="prod-price" data-price="<?php echo number_format($price, 2); ?>">
<p>
<?php echo wc_price($price); ?>
</p>
</div>
<div class="prod_flag">
<?php get_product_flags(true); //Hide featured for grouped child items. ?>
</div>
<?php
$count = $count + 1;
}
$count = 0;
Replace this line
<input type="checkbox" id="product-<?php the_ID();?>" value="1" name="quantity[<?php the_ID();?>]" class="wc-grouped-product-add-to-cart-checkbox js-group-item-sel" <?php echo ($count != 0 ? '' : ' checked="checked"'); ?> data-subscription="[echo terms subrciptions here]" />
with
<input type="checkbox" id="product-<?php the_ID();?>" value="1" name="quantity[<?php the_ID();?>]" class="wc-grouped-product-add-to-cart-checkbox js-group-item-sel" <?php echo ($count != 0 ? '' : ' checked="checked"'); ?> data-subscription="<?php echo ($tax_query_args['tax_query'][0]['terms']); ?>" />

My PHP form isn't returning anything from $_POST

I have a relatively simple PHP form. When I'm using print_r($_POST); I'm not receiving anything back. Just this result:
`Array
(
)
1`
My form is pretty simple:
<form action="" method="post" enctype="multipart/form-data" class="zip-<?php echo $atts['class']; ?>">
<div class="row ">
<div class="<?php echo $column1; ?>" <?php echo $inlineStyle ?>>
<div class="lds-facebook hide"><div></div><div></div><div></div><div></div><div></div></div>
<input type="text" name="zip" class="zip">
</div>
<div class="<?php echo $column2 ?>">
<button type="submit" class="submit" name="submit">
<?php echo $buttonValue; ?>
<i class='fa fa-caret-right' aria-hidden='true'></i>
</button>
</div>
</div>
</form>
The goal of the form is to simply post and then redirect to a page depending on the value received from the input. Any thoughts?
public static function find_zip_widget( $atts, $content = null ) {
ini_set('display_errors', '1');
extract(shortcode_atts(array(
'class' => 'class'
), $atts));
// Set classes for rows
if($atts['class'] == 'home'){
$column1 = 'col-md-6 col-12 my-auto text-center border-underline';
$column2 = 'col-md-6 col-12';
$buttonValue = 'FIND HELP RIGHT NOW';
$inlineStyle = '';
}
if($atts['class'] == 'widget'){
$column1 = 'col-md-5 col-5 offset-2 offset-md-0 my-auto';
$column2 = 'col-md-7 col-5 my-auto';
$buttonValue = 'SUBMIT';
$inlineStyle = 'style="border-bottom: 3px solid #9EA2A4 !important;min-height: 30px;"';
}
?>
<form action="" method="post" enctype="multipart/form-data" class="zip-<?php echo $atts['class']; ?>">
<div class="row ">
<div class="<?php echo $column1; ?>" <?php echo $inlineStyle ?>>
<div class="lds-facebook hide"><div></div><div></div><div></div><div></div><div></div></div>
<input type="text" name="zip" class="zip">
</div>
<div class="<?php echo $column2 ?>">
<button type="submit" class="submit" name="submit">
<?php echo $buttonValue; ?>
<i class='fa fa-caret-right' aria-hidden='true'></i>
</button>
</div>
</div>
</form>
<?php
echo "<pre>";
echo "Before if: ";
echo print_r($_POST);
echo "</pre>";
?>
<?php
$states = get_categories( array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false,
'taxonomy' => 'endeavors_location_state'
) );
if (isset($_POST['submit'])) {
$postalZip = $_POST["zip"];
echo "<pre>";
echo "After if: ";
echo print_r($_POST);
echo "</pre>";
echo '<br> <br> PostalZip: ' . $postalZip;
///REDIRECT FOR CITY, STATE, or other/////
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://maps.googleapis.com/maps/api/geocode/json?address='.$postalZip.'&key=AIzaSyBX_0qZmGBtiHrZMcjZfv6yL7NAbLiwnjc");
// curl_setopt($ch, CURLOPT_URL, "https://maps.googleapis.com/maps/api/geocode/json?address=78210&key=AIzaSyBX_0qZmGBtiHrZMcjZfv6yL7NAbLiwnjc");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
$jsonResults = json_decode($output, true);
$cityName = $jsonResults['results'][0]['address_components'][1]['long_name'];
$stateName = $jsonResults['results'][0]['address_components'][3]['long_name'];
$cityName = strtolower(str_replace(" ","-",$cityName));
$stateName = strtolower(str_replace(" ","-",$stateName));
$args = array(
'post_type' => 'endeavors_locations',
'post_status' => 'publish',
'numberposts' => -1
);
$cityFound = 0;
$allCityLocations = get_posts( $args );
foreach ($allCityLocations as $location) {
$eachCity = strtolower(str_replace(" ","-",$location->post_name));
echo $eachCity;
if ($location->post_name === $cityName) {
$cityFound = 1;
echo $cityFound;
}
}
if ($cityFound === 1) {
wp_safe_redirect('/locations/'.$cityName);
} else {
foreach ($states as $state) {
if ($state->slug === $stateName) {
$stateFound = 1;
}
}
if ($stateFound === 1) {
wp_safe_redirect('/state/'.$stateName);
} else {
wp_safe_redirect('/all-locations/');
}
}
}
///END OF REDIRECT FOR CITY, STATE, or other/////
} //end function
Your script post to the server just fine, but before hitting the print_r($_POST) it gets redirect to the same location. so print_r($_POST) was not executed . Please refer below image for additional info. There are two requests , one is post with 302 redirect. I suspect there maybe a javascript redirect involved.
I just ran your script,(xampp , php 7.1.26 , mac Mojave) and it prints
Array ( [zip] => testvalue [submit] => )
I added <?php print_r($_POST); ?> on top of the tag.( you might have done the same) and for me it worked as expected.
silly but did just wanted to know whether you submitted the form? if not it will show an empty array

wordpress get_terms and WP_Query not working as expected

i've created a colophon for my website in which i post all the logos of the different sponsors i have. I add all the sponsors via custom post type. i also added a specific custom taxonomy to distinguish between the different typologies of sponsorships.
I use this code in the footer.php to display them:
<?php $terms = get_terms('sponsor_tipology');
$count = count( $terms );
if ( $count > 0 ) {
foreach ( $terms as $term ) { ?>
<div class="col-xs-12 <?php echo $term->slug ;?>">
<h3><?php echo $term->name;?></h3>
<?php $arg = array (
'post_type' => 'colophone',
'post_per_page' => -1,
'sponsor_edition' => 'current',
'sponsor_tipology' => $term->slug,
);
$pesca_post = new WP_Query ($arg);
$quanti_post = $pesca_post->post_count;
if(have_posts()){
while ($pesca_post->have_posts()) : $pesca_post->the_post();
$featured = get_the_post_thumbnail_url(get_the_ID(),'large');
if ($quanti_post == 5){
$classe_bootstrap = 15;
}elseif ($quanti_post > 5){
$classe_bootstrap = "2 text-center";
}elseif($quanti_post < 5){
$classe_bootstrap = 12/$quanti_post;
}
echo '<div class="col-md-' . $classe_bootstrap . '">';
if (isset($featured)){
$img = $featured;
}else{
$img = get_template_directory_uri() . '/img/placeholder.png';
} ?>
<a href="<?php echo esc_attr(get_permalink($msd_settings['partner_page'])); ?>" title="<?php echo get_the_title($post->ID);?>" >
<div class="col-xs-12" style="background-image:url(<?php echo esc_url($img); ?>); height:100px;background-size:contain;background-repeat:no-repeat;background-position:center center;"></div>
</a>
<?php echo '</div>';
endwhile;
}?>
</div>
<?php }
}?>
my problem is that this code is completely working just on some pages, on other it shows the contents avoiding the ones belonging to the first term, no matter which it will be.
I have noticed that it works in pagaes where i use other queries.
What am i doing wrong?
i changed it in this way and now it's working!
$terms = get_terms('sponsor_tipology');
$count = count( $terms );
if ( $count > 0 ) {
foreach ( $terms as $term ) { //per ogni termine presente
$nome = $term->slug;?>
<div class="col-xs-12 <?php echo $term->slug ;?>">
<h3><?php echo $term->name;?></h3>
<?php $arg = array (
'post_type' => 'colophone',
'post_per_page' => -1,
'sponsor_edition' => 'current',
'sponsor_tipology' => $nome,
);
$elementi = get_posts($arg);
$quanti_post = count( $elementi );
if ($quanti_post == 5){
$classe_bootstrap = 15;
}
elseif ($quanti_post > 5){
$classe_bootstrap = "2 text-center";
}
elseif($quanti_post < 5){
$classe_bootstrap = 12/$quanti_post;
}
foreach($elementi as $elemento){
$featured = get_the_post_thumbnail_url($elemento->ID,'large');
if (isset($featured)){
$img = $featured;
}
else{
$img = get_template_directory_uri() . '/img/placeholder.png';
} ?>
<div class="col-md-<?php echo $classe_bootstrap; ?>">
<a href="<?php echo esc_attr(get_permalink($msd_settings['partner_page'])); ?>" title="<?php echo get_the_title($elemento->ID);?>" >
<div class="col-xs-12" style="background-image:url(<?php echo esc_url($img); ?>); height:100px;background-size:contain;background-repeat:no-repeat;background-position:center center;"></div>
</a>
</div>
<?php }?>
</div>
<?php }
}?>

Class WP_Widget not found

This is from an unsupported WordPress plugin that I'm trying to revive. I'm getting an error in the following code which states
Fatal Error class WP_Widget not found on Line 7
It works with PHP 5.3.3 but not on 5.6 or any version of PHP 7. Any suggestions would be appreciated.
<?php
/**
*
* Widget Class
*/
class Twitter_Like_Box_Widget extends WP_Widget
{
var $_options;
function __construct( ) {
global $tlb;
$this->wpb_prefix = $tlb->get_domain();
$widget_ops = array( 'classname' => 'tlb_widget', 'description' => ' Display your Twitter followers along with a follow me button' ); // Widget Settings
$control_ops = array( 'id_base' => 'tlb_widget' ); // Widget Control Settings
parent::__construct( 'tlb_widget', 'Twitter Like Box', $widget_ops, $control_ops );
$this->_options = $tlb->getOptions();
}
//Function to init the widget values and call the display widget function
function widget($args,$instance)
{
$title = apply_filters('widget_title', $instance['title']); // the widget title
$username = $instance['username']; // the widget title
$total_number = $instance['total_number']; // the number of followers to show
$show_followers = $instance['show_followers']; // show followers or users i follow
$link_followers = $instance['link_followers']; // link followers to profile
$width = $instance['width']; // link followers to profile
$widget = array ( 'username' => $username ,'total' => $total_number , 'show_followers' => $show_followers ,'link_followers'=> $link_followers, 'width' => $width, 'options' => $this->_options);
echo $args['before_widget'];
if ( $title )
echo $args['before_title'] . $title . $args['after_title'];
$this->display_widget($widget);
echo $args['after_widget'];
}
//function that display the widget form
function form($instance)
{
global $tlb;
$defaults = array( 'total_number' => 10, 'show_followers' => 'followers','link_followers'=> 'on','title' => 'My Followers', 'username' => 'chifliiiii');
$instance = wp_parse_args( (array) $instance, $defaults ); ?>
<?php if( $tlb->error != '' && defined('DOING_AJAX')):?>
<div class="error">
<p><?php echo sprintf(__('Check you OAuth settings, there is a problem with the connection.',$this->wpb_prefix),admin_url('options-general.php?page=twitter-like-box-reloaded'));?></p>
</div>
<?php endif;?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:',$this->wpb_prefix);?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>'" type="text" value="<?php echo $instance['title']; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('username'); ?>"><?php _e('Username (without #):',$this->wpb_prefix);?></label>
<input class="widefat" id="<?php echo $this->get_field_id('username'); ?>" name="<?php echo $this->get_field_name('username'); ?>'" type="text" value="<?php echo $instance['username']; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('show_thumbs'); ?>"><?php _e('Show Followers or people you follow?',$this->wpb_prefix);?></label>
</p>
<ul>
<li>
<input type="radio" class="radio" <?php checked( $instance['show_followers'], 'followers' ); ?> id="<?php echo $this->get_field_id('show_followers'); ?>" name="<?php echo $this->get_field_name('show_followers'); ?>" value="followers"/> <?php _e('Followers',$this->wpb_prefix);?>
</li>
<li>
<input type="radio" class="radio" <?php checked( $instance['show_followers'], 'nofollowers' ); ?> id="<?php echo $this->get_field_id('show_followers'); ?>" name="<?php echo $this->get_field_name('show_followers'); ?>" value="nofollowers" /> <?php _e('People I follow',$this->wpb_prefix);?>
</li>
</ul>
<p>
<label for="<?php echo $this->get_field_id('total_number'); ?>"><?php _e('How many you want to show?',$this->wpb_prefix); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('total_number'); ?>" name="<?php echo $this->get_field_name('total_number'); ?>" type="text" value="<?php echo $instance['total_number']; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('link_followers'); ?>"><?php _e('Link followers to their profiles?',$this->wpb_prefix); ?></label>
<input type="checkbox" class="checkbox" <?php checked( $instance['link_followers'], 'on' ); ?> id="<?php echo $this->get_field_id('link_followers'); ?>" name="<?php echo $this->get_field_name('link_followers'); ?>" value="on" />
</p>
<p>
<label for="<?php echo $this->get_field_id('width'); ?>"><?php _e('Widget width:',$this->wpb_prefix); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('width'); ?>" name="<?php echo $this->get_field_name('width') ; ?>'" type="text" value="<?php echo isset($instance['width']) ? $instance['width'] : '100%'; ?>" />
</p>
<p>
<?php _e('More settings',$this->wpb_prefix);?>
</p>
<?php
}
//function that save the widget
function update($new_instance, $old_instance)
{
$instance['title'] = strip_tags($new_instance['title']);
$instance['username'] = strip_tags( $new_instance['username'] )== '' ? 'chifliiiii' : strip_tags( $new_instance['username'] );
$instance['total_number'] = strip_tags($new_instance['total_number']);
$instance['show_followers'] = $new_instance['show_followers'];
$instance['link_followers'] = $new_instance['link_followers'];
$instance['width'] = $new_instance['width'];
//Delete transient in case exist
$key = 'tlb_widgets_' . $instance['username'];
delete_transient($key);
return $instance;
}
//Finally thevfunction that create the widget
static function get_tlb_widget($widget)
{
global $tlb,$you;
$wpb_prefix = $tlb->get_domain();
$twitter = self::fetch_twitter_followers($widget);
ob_start();
if( !empty($you['error']) && '32' == $you['code']) {
echo $you['error'];
}
else
{
?>
<style type="text/css">
<?php echo $widget['options']['custom_css'];?>
</style>
<div id="tlb_container" style="width: <?php echo isset($widget['width']) ? $widget['width'] : 'auto';?>">
<?php if(isset($twitter['error']) ) :?>
<?php echo $twitter['error'];?>
<?php else : ?>
<div>
<div id="tlb_profile_img">
<a target="_blank" href="http://twitter.com/<?php echo $widget['username'];?>">
<img src="<?php echo $twitter['profile_image_url'];?>" width="44" height="44" align="left" alt="<?php echo $widget['username'];?>">
</a>
</div>
<div id="tlb_name">
<a target="_blank" href="http://twitter.com/<?php echo $widget['username'];?>">
<?php echo $widget['username'];?><span> <?php _e('on Twitter',$wpb_prefix);?></span>
</a>
</div>
<div id="tlb_follow">
<?php _e('Follow #',$wpb_prefix);?><?php echo $widget['username'];?>
</div>
</div><br>
<div style="padding:0; color:#637746;">
<div id="tlb_follow_total">
<?php
if ( $widget['show_followers'] == 'followers')
{
echo $twitter['followers_count'].' '.__('people follow',$wpb_prefix).' <strong>'. $widget['username'].'</strong>';
}
else
{
echo __('You follow ',$wpb_prefix). $twitter['friends_count'].__(' users',$wpb_prefix);
}
?>
</div>
<?php for($i=0; $i < $widget['total']; $i++) :?>
<span class="tlb_user_item">
<?php if($widget['link_followers'] == 'on' ): ?>
<a target="_blank" href="http://twitter.com/<?php echo $twitter['followers'][$i]['screen_name'];?>" title="<?php echo $twitter['followers'][$i]['screen_name'];?>" rel="nofollow">
<?php endif;?>
<img src="<?php echo $twitter['followers'][$i]['profile_image_url'];?>" width="48" height="48" alt="<?php echo $twitter['followers'][$i]['screen_name'];?>">
<span><?php echo substr($twitter['followers'][$i]['screen_name'], 0, 8);?></span>
<?php if($widget['link_followers'] == 'on' ): ?>
</a>
<?php endif;?>
</span>
<?php endfor;?>
<br style="clear:both">
</div>
<?php if ( $widget['options']['credits'] == 'true' ) echo '<div style="font-size:9px;text-align:right;">Widget By Timersys</div>';?>
<?php endif;//twitter error ?>
</div>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");
</script>
<?php
}
$widget_code = ob_get_contents();
ob_end_clean();
return $widget_code;
}
/**
* Display widget
*/
static function display_widget($options){
echo Twitter_Like_Box_Widget::get_tlb_widget($options);
}
static function fetch_twitter_followers($options)
{
global $tlb,$you;
$cache_time = $tlb->_options['cache-time'];
$id = isset($options['id']) ? $options['id'] : 'widgets';
$key = 'tlb_'.$id.'_' . $options['username'];
// Let's see if we have a cached version
$followers = get_transient($key);
if ($followers !== false)
return $followers;
else
{
$tlb->connect();
$response = $tlb->connection->get("users/lookup", array('screen_name' => $options['username']));
if (Twitter_Like_Box_Widget::is_twitter_error($response))
{
// In case Twitter is down we return the last successful count
return get_option($key);
}
else
{
$json = $response;
#$you['name'] = $json[0]->name;
#$you['screen_name'] = $json[0]->screen_name;
#$you['followers_count'] = $json[0]->followers_count;
#$you['profile_image_url'] = $json[0]->profile_image_url;
#$you['friends_count'] = $json[0]->friends_count;
if ( $options['show_followers'] == 'followers' )
{
$fans = $tlb->connection->get('followers/ids',array('screen_name' => $options['username']));
}
else
{
$fans = $tlb->connection->get('friends/ids',array('screen_name' => $options['username']));
}
if (!Twitter_Like_Box_Widget::is_twitter_error($fans))
{
if ($options['total'] > 90 )
{
$fans_ids = array_chunk($fans->ids, 90);
$fans = array();
foreach ( $fans_ids as $ids_a )
{
$fans_ids = (string)implode( ',', $ids_a );
#$result = $tlb->connection->get('users/lookup',array('user_id' => $fans_ids ));
#$fans = array_merge($fans , $result );
}
}
else
{
$fans_ids = (string)implode( ',', array_slice($fans->ids, 0, $options['total']) );
#$fans = $tlb->connection->get('users/lookup',array('user_id' =>$fans_ids));
}
}
if( !Twitter_Like_Box_Widget::is_twitter_error($fans) && isset($fans[0]->screen_name) )
{
$followers = array();
for($i=0; $i < $options['total']; $i++)
{
$followers[$i]['screen_name'] = (string)$fans[$i]->screen_name;
$followers[$i]['profile_image_url'] = (string)$fans[$i]->profile_image_url;
}
$you['followers'] = $followers;
// Store the result in a transient, expires after 1 hour
// Also store it as the last successful using update_option
set_transient($key, $you, 60*60* $cache_time);
update_option($key, $you);
}
return $you;
}
}
}
static function is_twitter_error($response){
global $you,$tlb;
if(is_object($response) && isset($response->errors) )
{
$you['error'] = 'Error code: '. $response->errors[0]->code .'<br>Error message: '.$response->errors[0]->message;
$you['code'] = $response->errors[0]->code;
$tlb->log_error($you['error']);
return true;
}
if(is_object($response) && isset($response->ids) && empty($response->ids))
{
$you['error'] = '<br>Error message: You got no users to show. check if you have followers';
$tlb->log_error($you['error']);
return true;
}
return false;
}
} //end of class
Wordpress have ABSPATH variable defined in wp-config.php file, which contains the project's root path. Now we can include any file of wordpress codebase. eg.
require_once ABSPATH.'wp-includes/class-wp-widget.php';
I don't know the reason for the error, propably something with autoloading, but you can solve it if you include the file for the WP_Widget class in the first line.
require_once get_home_path()."wp-includes/class-wp-widget.php";

Remove post count from Wordpress custom widget

I'm a noob running a project theme Wordpress website. There is a widget that shows a "Browse by Category" list in columns.
The widget shows the post count in brackets next to the category heading. I want to remove that.
Any help would be greatly appreciated.
This is the code for the widget:
add_action('widgets_init', 'register_browse_by_category_widget');
function register_browse_by_category_widget() {
register_widget('ProjectTheme_browse_by_category');
}
class ProjectTheme_browse_by_category extends WP_Widget {
function ProjectTheme_browse_by_category() {
$widget_ops = array( 'classname' => 'browse-by-category', 'description' => 'Show all categories and browse by category' );
$control_ops = array( 'width' => 200, 'height' => 250, 'id_base' => 'browse-by-category' );
$this->WP_Widget( 'browse-by-category', 'ProjectTheme - Browse by Category', $widget_ops, $control_ops );
}
function widget($args, $instance) {
extract($args);
echo $before_widget;
if ($instance['title']) echo $before_title . apply_filters('widget_title', $instance['title']) . $after_title;
$loc_per_row = $instance['loc_per_row'];
$widget_id = $args['widget_id'];
$nr_rows = $instance['nr_rows'];
$only_these = $instance['only_these'];
$only_parents = $instance['only_parents'];
if($only_parents == "on") $only_parents = true;
else $only_parents = false;
$nr = 4;
if(!empty($loc_per_row)) $nr = $loc_per_row;
echo '<style>#'.$widget_id.' #location-stuff li>ul { width: '.(round(100/$nr)-0.5).'%}</style>';
if($nr_rows > 0) $jk = "&number=".($nr_rows * $loc_per_row);
$terms_k = get_terms("project_cat","parent=0&hide_empty=0");
$terms = get_terms("project_cat","parent=0&hide_empty=0".$jk);
//$term = get_term( $term_id, $taxonomy );
if($only_these == "1")
{
$terms = array();
foreach($terms_k as $trm)
{
if($instance['term_' . $trm->term_id] == $trm->term_id)
array_push($terms, $trm);
}
}
//-----------------------------
if(count($terms) < count($terms_k)) $disp_btn = 1;
else $disp_btn = 0;
$count = count($terms); $i = 0;
if ( $count > 0 ){
echo "<ul id='location-stuff'>";
foreach ( $terms as $term ) {
if($i%$nr == 0) echo "<li>";
$total_ads = 0;
$terms2 = '';
$terms2 = get_terms("project_cat","parent=".$term->term_id."&hide_empty=0");
$mese = '';
$mese .= '<ul>';
$mese .= "<img src=\"".get_bloginfo('template_url')."/images/posted.png\" width=\"20\" height=\"20\" />
<h3><a class='parent_taxe' rel='taxe_project_cat_".$term->term_id."' href='".get_term_link($term->slug,"project_cat")."'>" . $term->name;
//."</a></h3>";
$total_ads = ProjectTheme_get_custom_taxonomy_count('project',$term->slug);
$mese2 = '';
if($terms2 && $only_parents == false)
{
foreach ( $terms2 as $term2 )
{
$tt = ProjectTheme_get_custom_taxonomy_count('project',$term2->slug);
$total_ads += $tt;
//$mese2 .= "<li><a href='".get_term_link($term2->slug,"project_cat")."'>" . $term2->name." (".$tt.")</a></li>";
$mese2 .= "<li><a href='".get_term_link($term2->slug,"project_cat")."'>" . $term2->name." </a></li>";
}
}
echo $mese."(".$total_ads.")</a></h3>";
echo '<ul id="_project_cat_'.$term->term_id.'">'.$mese2."</ul>";
echo '</ul>';
if(($i+1) % $nr == 0) echo "</li>";
$i++;
}
//if(($i+1) % $nr != 0) echo "</li>";
echo "</ul>";
}
if($disp_btn == 1)
{
echo '<br/><b>'.__('See More Categories','ProjectTheme').'</b>';
}
echo $after_widget;
}
function update($new_instance, $old_instance) {
return $new_instance;
}
function form($instance) { ?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title'); ?>:</label>
<input type="text" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>"
value="<?php echo esc_attr( $instance['title'] ); ?>" style="width:95%;" />
</p>
<p>
<label for="<?php echo $this->get_field_id('only_parents'); ?>"><?php _e('Only show parent categories'); ?>:</label>
<input type="checkbox" id="<?php echo $this->get_field_id('only_parents'); ?>" name="<?php echo $this->get_field_name('only_parents'); ?>"
<?php echo (esc_attr( $instance['only_parents'] ) == "on" ? "checked='checked'" : ""); ?> />
</p>
<p>
<label for="<?php echo $this->get_field_id('loc_per_row'); ?>"><?php _e('Number of Columns'); ?>:</label>
<input type="text" id="<?php echo $this->get_field_id('loc_per_row'); ?>" name="<?php echo $this->get_field_name('loc_per_row'); ?>"
value="<?php echo esc_attr( $instance['loc_per_row'] ); ?>" style="width:20%;" />
</p>
<p>
<label for="<?php echo $this->get_field_id('nr_rows'); ?>"><?php _e('Number of Rows'); ?>:</label>
<input type="text" id="<?php echo $this->get_field_id('nr_rows'); ?>" name="<?php echo $this->get_field_name('nr_rows'); ?>"
value="<?php echo esc_attr( $instance['nr_rows'] ); ?>" style="width:20%;" />
</p>
<p>
<label for="<?php echo $this->get_field_id('nr_rows'); ?>"><?php _e('Only show categories below'); ?>:</label>
<?php echo '<input type="checkbox" name="'.$this->get_field_name('only_these').'" value="1" '.(
$instance['only_these'] == "1" ? ' checked="checked" ' : "" ).' /> '; ?>
</p>
<p>
<label for="<?php echo $this->get_field_id('nr_rows'); ?>"><?php _e('Categories to show'); ?>:</label>
<div style=" width:220px;
height:180px;
background-color:#ffffff;
overflow:auto;border:1px solid #ccc">
<?php
$terms = get_terms("project_cat","parent=0&hide_empty=0");
foreach ( $terms as $term ) {
echo '<input type="checkbox" name="'.$this->get_field_name('term_'.$term->term_id).'" value="'.$term->term_id.'" '.(
$instance['term_'.$term->term_id] == $term->term_id ? ' checked="checked" ' : "" ).' /> ';
echo $term->name.'<br/>';
}
?>
</div>
</p>
<?php
}
}
?>
this is the part of code that writes the number of posts by category ...
change this
echo $mese."(".$total_ads.")</a>
with
echo $mese."</a>";

Categories