Viewing 5 replies - 1 through 5 (of 5 total)
  • Yo tambien deseo poner un contador al principio del listado en en el area de widgets.

    I also want to put a counter on the top of the list in the area of widgets.

    Plugin Author xnau webdesign

    (@xnau)

    I have this script that gives you this ability. You need to put it in the “plugins” folder and activate it. You will also have to edit it to match how you want it to display. Should be easy to understand, it just uses the standard WP code for creating a widget. If you want to translate it, you have to replace the English with your language.

    <?php
    /*
     * Plugin Name: Participants Database: Show Total
     * Version: 0.1
     * Description: widget and shortcode for displaying the total number of participant records
     * Author: xn*au webdesign
     * Author URI: https://xnau.com/participants-database/
     * License: GPL2
     */
    
    add_action('widgets_init', create_function('', 'return register_widget("Total_Participants");'));
    
    class Total_Participants extends WP_Widget {
    
      public function Total_Participants()
      {
        $widget_ops = array(
            'classname' => 'Total_Particpants',
            'description' => 'Widget for displaying the number of participant records',
        );
        parent::WP_Widget(false, $name = 'Display Total Participants', $widget_ops);
        // define the shortcode
        add_shortcode('total_records', array(__CLASS__, 'get_total'));
      }
    
      public function form($instance)
      {
        $defaults = array(
            'title' => 'Total Participants',
            'string' => 'There are %s registered participants.',
        );
        $instance = wp_parse_args((array) $instance, $defaults);
        $title = esc_attr($instance['title']);
        $string = esc_attr($instance['string']);
        ?>
        <p><label for="<?php echo $this->get_field_id('title'); ?>">Widget Title: <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title ?>" /></label></p>
        <p><label for="<?php echo $this->get_field_id('string'); ?>">Display String: <input class="widefat" id="<?php echo $this->get_field_id('string'); ?>" name="<?php echo $this->get_field_name('string'); ?>" type="text" value="<?php echo $string ?>" /></label><br />The string to display the total in. The "%s" will be replaced by the number of participants.</p>
        <?php
      }
    
      public function update($new_instance, $old_instance)
      {
        $instance = $old_instance;
        $instance['title'] = $new_instance['title'];
        $instance['string'] = $new_instance['string'];
        return $instance;
      }
    
      public function widget($args, $instance)
      {
        extract($args, EXTR_SKIP);
    
        $count = $this->get_total();
        $title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']);
    
        echo $before_widget;
    
        if (!empty($title))
          echo $before_title . $title . $after_title;
    
        printf($instance['string'], $count);
    
        echo $after_widget;
      }
    
      public static function get_total()
      {
    
        $count = '';
        if (class_exists('Participants_Db')) {
          global $wpdb;
    
          $sql = 'SELECT COUNT(*)  FROM ' . Participants_Db::$participants_table . ' p';
    
          $count = $wpdb->get_var($sql);
        }
        return $count;
      }
    
    }
    ?>
    Thread Starter walterboy

    (@walterboy)

    That’s great. The shortcode works a treat. Thanks.

    I’m just trying to figure out if it could be tweaked to return a count if a certain criteria is met. So for example count the number of participants that have a Yes in a certain field.

    I’m no expert on this at all but could this be changed to something like SELECT – FROM – Where “this field” equals “that”?

    $sql = ‘SELECT COUNT(*) FROM ‘ . Participants_Db::$participants_table . ‘ p’;

    Plugin Author xnau webdesign

    (@xnau)

    All I can say is try it…it’s not too complicated. Look up adding a “where” clause to a MySQL query if you don’t know how already.

    Thread Starter walterboy

    (@walterboy)

    I didnt know how to do it but your guidance, a google search for MySQL WHERE CLAUSE and a little fiddling around did the trick!

    Thanks again for a great plugin.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Number of entries shortcode’ is closed to new replies.