WordPress.org

Ready to get started?Download WordPress

Forums

WordPress Category Archive
Derivative plugin for Custom Post Type Archive (3 posts)

  1. yoav.aner
    Member
    Posted 3 years ago #

    Hi Hugh,

    First of all, thanks for the plugin. It's really neat and useful. I started using it recently and it's exactly what I needed.

    However, I came across the exact same requirement, but instead of narrowing down by category, by custom post types. I have one custom post type that I already have an archive page for, but couldn't find a widget to only display the breakdown by month/year for THAT custom type.

    After searching for a while and not finding anything I've decided to clone your plugin and modify it to work with custom post types. The code was clear and neat enough for me to be able to work it out, even with limited skills.

    I'm not that experienced with PHP or wordpress, and have never created a plugin, but I was able to create something that actually works. I thought that instead of posting it myself I can send it to you, so you can have a look/comment and potentially post it yourself?? Or perhaps you can merge both into one plugin??

    What's the best way to post you the code?

    Cheers
    Yoav

    http://wordpress.org/extend/plugins/wp-category-archive/

  2. Hugh Mandeville
    Member
    Plugin Author

    Posted 3 years ago #

    I guess if the plugin is really similar, just post the code changes here, to this forum topic.

    Maybe I can update the plugin, so in the widget control, you can either choose the category or custom post type from a pulldown.

    If the code is quite different, you'll probably want to create a new plugin.

    http://plugins.trac.wordpress.org/browser/wp-category-archive/trunk/wp-category-archive.php

  3. yoav.aner
    Member
    Posted 3 years ago #

    Thanks Hugh,

    Yes, very similar, this is why I preferred to contribute to yours rather than to issue a separate plugin.

    Here's the full modified code:

    <?php
    /*
        Plugin Name: WordPress Custom Post Type Archive
        Plugin URI: http://www.gingerlime.com
        Description: Displays a monthly or yearly archive of posts for one specific custom post type.
        Version: 1.0
        Requires at least: 3.1
        Author: Yoav Aner (based on WordPress Category Archive by Hugh Mandeville)
        License: GPL
    
        == Description ==
    
        The WordPress Custom Post Type Archive Widget Plugin displays a monthly or yearly archive of posts for one specific custom post type.
        It is very similar to the WordPress Archive except that it just displays one specific custom post type's posts.
        It can be configured to either show a listing or a pulldown of months or years with or without post counts.
    
        == Installation ==
    
        1. Upload "wp-custom-post-type-archive.php" to the "/wp-content/plugins/" directory.
        2. Activate the plugin through the "Plugins" menu in WordPress.
        3. From the "Widgets" page in WordPress drag the "Custom Post Type Archive" widget to the "Sidebar".
        4. Configure the "Custom Post Type Archive" widget controls, setting the "Title", "Custom Type", "Display Style", "Group By" and "Show Post Counts" fields.
    
    */
    
    class WP_CustomType_Archive_Widget extends WP_Widget
    {
        /**
         * Contstructor.  Set description and call parent class.
         */
        function WP_CustomType_Archive_Widget()
        {
            /* Widget settings. */
            $widget_ops = array("description" => "Display an archive listing of one specific custom-type.");
    
            /* Create the widget. */
            $this->WP_Widget("wp-customtype-archive", "Custom Type Archive", $widget_ops);
        }
    
        /**
         * Creates URL for archive links to the yearly or monthly custom type listing.
         * The URL has the year, month and custom post type in the query string
         *      ?m=201001&post_type=2
         */
        function create_url($interval, $year, $month, $customtype)
        {
            global $wp_rewrite;
            $year_month_value = $year . $month;
            $year_month_path = $year . "/" . $month;
            if ($interval == "year") {
                $year_month_value = $year;
                $year_month_path = $year . "/00";
            }
    
            $url = get_option('home') . "/?m=" . $year_month_value . "&post_type=" . $customtype; 
    
    /*	if ($wp_rewrite->using_permalinks()) {
                $cat_pos = strpos($wp_rewrite->permalink_structure, "%" . $customtype . "%");
                if ($cat_pos != false) {
                    // if %category% is in the permalink structure, figure out if year is before or after it
                    $year_pos = strpos($wp_rewrite->permalink_structure, "%year%");
                    if ($year_pos != false) {
                        $url = get_option('home') . "/";
                        if ($cat_pos < $year_pos) {
                            $url .= $customtype . "/" . $year_month_path . "/";
                        } else {
                            $url .=  $year_month_path . "/" . $customtype . "/";
                        }
                    }
                }
            }
    */
            return ($url);
        }
    
        /**
         * Display widget.
         */
        function widget($args, $instance)
        {
            extract($args);
    
            /* User-selected settings. */
            //$title = apply_filters('widget_title', $instance['title'] );
            $title = esc_attr($instance['title']);
    	$customtype = esc_attr($instance['customtype']);
            $display_style = $instance['display_style'];
            $interval = $instance['interval'];
            $show_counts = intval($instance['show_counts']);
            echo $before_widget;
            /* Title of widget (before and after defined by themes). */
            if ( $title ) {
                echo $before_title . $title . $after_title;
            }
    
            // TBD: could change to call sql statement that uses group by
            $myposts = get_posts("numberposts=-1&offset=0&post_type=" . $customtype . "&orderby=date&order=DESC");
            $previous_year_month_display = "";
            $previous_year_month_value = "";
            $previous_year = "";
            $previous_month = "";
            $count = 0;
    
            $display_format = "F Y";
            $compare_format = "Ym";
            $select_str = __("Select Month");
            if ($interval == "year") {
                $display_format = "Y";
                $compare_format = "Y";
                $select_str = __("Select Year");
            }
    
            if ($display_style == "pulldown") {
                echo "<select name=\"wp-customtype-archive-dropdown\" onchange=\"document.location.href=this.options[this.selectedIndex].value;\">";
                echo " <option value=\"\">" . $select_str . "</option>";
            } else if ($display_style == "list") {
                echo "<ul>";
            }
    
            foreach($myposts as $post) {
                $post_date = strtotime($post->post_date);
                $current_year_month_display = date_i18n($display_format, $post_date);
                $current_year_month_value = date($compare_format, $post_date);
                $current_year = date("Y", $post_date);
                $current_month = date("m", $post_date);
                if ($previous_year_month_value != $current_year_month_value) {
                    if ($count > 0) {
                        $url = $this->create_url($interval, $previous_year, $previous_month, $customtype);
                        if ($display_style == "pulldown") {
                            echo " <option value=\"" . $url . "\"";
                            if ($_GET['m'] == $previous_year_month_value) {
                                echo " selected=\"selected\" ";
                            }
                            echo ">" . $previous_year_month_display;
                            if ($show_counts == 1) {
                                echo  " (" . $count . ")";
                            }
                            echo "</option>";
                        } else if ($display_style == "list") {
                            echo "<li><a href=\"". $url . "\">" . $previous_year_month_display . "</a>";
                            if ($show_counts == 1) {
                                echo  " (" . $count . ")";
                            }
                            echo "</li>";
                        } else {
                            echo "<a href=\"". $url . "\">" . $previous_year_month_display . "</a>";
                            if ($show_counts == 1) {
                                echo  " (" . $count . ")";
                            }
                            echo "<br/>";
                        }
                    }
                    $count = 0;
                }
                $count++;
                $previous_year_month_display = $current_year_month_display;
                $previous_year_month_value = $current_year_month_value;
                $previous_year = $current_year;
                $previous_month = $current_month;
    
            }
            if ($count > 0) {
                $url = $this->create_url($interval, $previous_year, $previous_month, $customtype);
                if ($display_style == "pulldown") {
                    echo " <option value=\"" . $url . "\">" . $previous_year_month_display;
                    if ($show_counts == 1) {
                        echo " (" . $count . ")";
                    }
                    echo "</option>";
                } else if ($display_style == "list") {
                    echo "<li><a href=\"". $url . "\">" . $previous_year_month_display . "</a>";
                    if ($show_counts == 1) {
                        echo " (" . $count . ")";
                    }
                    echo "</li>";
                } else {
                    echo "<a href=\"". $url . "\">" . $previous_year_month_display . "</a>";
                    if ($show_counts == 1) {
                        echo " (" . $count . ")";
                    }
                    echo "<br/>";
                }
            }
            if ($display_style == "pulldown") {
                echo "</select>";
            } else if ($display_style == "list") {
                echo "</ul>";
            }
    
            echo $after_widget;
        }
    
        /**
         * Called when widget control form is posted.
         */
        function update( $new_instance, $old_instance )
        {
            // global $post;
            if (!isset($new_instance['submit'])) {
                return false;
            }
    
            $instance = $old_instance;
    
            /* Strip tags (if needed) and update the widget settings. */
            $instance["title"] = strip_tags($new_instance["title"]);
            $instance["customtype"] = strip_tags($new_instance["customtype"]);
            $instance["display_style"] = strip_tags($new_instance["display_style"]);
            $instance["interval"] = strip_tags($new_instance["interval"]);
            $instance["show_counts"] = intval($new_instance["show_counts"]);
            return $instance;
        }
    
        /**
         * Display widget control form.
         *  Title:
         *  Custom Type:
         *  Display Style:    Lines | (List) | Pulldown
         *  Group By:         (Month) | Year
         *  Show Post Counts: Yes | (No)
         */
        function form( $instance )
        {
            global $wpdb;
            /* Set up some default widget settings. */
            $defaults = array( "title" => "Archive", "customtype" => "", "display_style" => "list", "interval" => "month", "show_counts" => 1 );
            $instance = wp_parse_args( (array) $instance, $defaults );
            $title = esc_attr($instance['title']);
            $customtype = esc_attr($instance['customtype']);
            $display_style = $instance['display_style'];
            $interval = $instance['interval'];
            $show_counts = intval($instance['show_counts']);
    
            // Title
            echo "<p>";
            echo "<label for=\"" . $this->get_field_id("title") . "\">Title:";
            echo "<input id=\"" . $this->get_field_id("title") . "\" " .
                 "name=\"" . $this->get_field_name("title") . "\" " .
                 "value=\"" . $title . "\" style=\"width:100%;\" />";
            echo "<label></p>";
    
            // Custom Post Type (as string). TODO: add fancy dropdown option
            echo "<p>";
            echo "<label for=\"" . $this->get_field_id("customtype") . "\">Custom Type:";
            echo "<input id=\"" . $this->get_field_id("customtype") . "\" " .
                 "name=\"" . $this->get_field_name("customtype") . "\" " .
                 "value=\"" . $customtype . "\" style=\"width:100%;\" />";
            echo "<label></p>";
    
            // Display Style: Lines, List or Pulldown
            echo "<p>";
            echo "<label for=\"" . $this->get_field_id("display_style") . "\">Display Style:<br/>";
            echo "<input type=\"radio\" name=\"" . $this->get_field_name("display_style") . "\" value=\"lines\"";
            if ($display_style == "lines") {
                echo " checked=\"checked\" ";
            }
            echo "> Lines ";
            echo "<input type=\"radio\" name=\"" . $this->get_field_name("display_style") . "\" value=\"list\"";
            if ($display_style == "list") {
                echo " checked=\"checked\" ";
            }
            echo "> List ";
            echo "<input type=\"radio\" name=\"" . $this->get_field_name("display_style") . "\" value=\"pulldown\"";
            if ($display_style == "pulldown") {
                echo " checked=\"checked\" ";
            }
            echo "> Pulldown";
            echo "</label></p>";
    
            // Interval: Month or Year
            echo "<p>";
            echo "<label for=\"" . $this->get_field_id("interval") . "\">Group By:<br/>";
            echo "<input type=\"radio\" name=\"" . $this->get_field_name("interval") . "\" value=\"month\"";
            if ($interval != "year") {
                echo " checked=\"checked\" ";
            }
            echo "> Month ";
            echo "<input type=\"radio\" name=\"" . $this->get_field_name("interval") . "\" value=\"year\"";
            if ($interval == "year") {
                echo " checked=\"checked\" ";
            }
            echo "> Year";
            echo "</label></p>";
    
            // Show Counts: Yes or No
            echo "<p>";
            echo "<label for=\"" . $this->get_field_id("show_counts") . "\">Show Post Counts:<br/>";
            echo "<input type=\"radio\" name=\"" . $this->get_field_name("show_counts") . "\" value=\"1\"";
            if ($show_counts == 1) {
                echo " checked=\"checked\" ";
            }
            echo "> Yes ";
            echo "<input type=\"radio\" name=\"" . $this->get_field_name("show_counts") . "\" value=\"0\"";
            if ($show_counts != 0) {
                echo " checked=\"checked\" ";
            }
            echo "> No";
            echo "</label></p>";
    
            // Submit (hidden field)
            echo "<input type=\"hidden\" id=\"" . $this->get_field_id("submit") ."\" name=\"" .
                 $this->get_field_name("submit") . "\" value=\"1\" />";
        }
    
        /**
         * Register widget.
         */
        function register()
        {
            register_widget("WP_CustomType_Archive_Widget");
        }
    
    }
    
    // widgets_init hook, calls load widget function.
    add_action("widgets_init", array('WP_CustomType_Archive_Widget', 'register'));
    
    ?>

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic