Making WordPress.org


Ignore:
Timestamp:
04/15/2016 09:38:17 PM (10 years ago)
Author:
coffee2code
Message:

developer.wordpress.org: Add a filter form to taxonomy archives.

Props keesiemeijer.
Fixes #601.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/inc/template-tags.php

    r2942 r2966  
    341341     * Get an array of all parsed post types.
    342342     *
     343     * @param string  $labels If set to 'labels' post types with their labels are returned.
    343344     * @return array
    344345     */
    345     function get_parsed_post_types() {
    346         return array(
    347             'wp-parser-class',
    348             'wp-parser-function',
    349             'wp-parser-hook',
    350             'wp-parser-method',
     346    function get_parsed_post_types( $labels = '' ) {
     347        $post_types = array(
     348            'wp-parser-class'    => __( 'Classes',   'wporg' ),
     349            'wp-parser-function' => __( 'Functions', 'wporg' ),
     350            'wp-parser-hook'     => __( 'Hooks',     'wporg' ),
     351            'wp-parser-method'   => __( 'Methods',   'wporg' ),
    351352        );
     353
     354        if ( 'labels' !== $labels ) {
     355            return array_keys( $post_types );
     356        }
     357
     358        return $post_types;
    352359    }
    353360
     
    13941401        return $message;
    13951402    }
     1403
     1404    /**
     1405     * Displays a post type filter dropdown on taxonomy pages.
     1406     *
     1407     * @return string HTML filter form.
     1408     */
     1409    function taxonomy_archive_filter() {
     1410        global $wp_rewrite;
     1411
     1412        $taxonomies = array( 'wp-parser-since', 'wp-parser-package', 'wp-parser-source-file' );
     1413        $taxonomy   = get_query_var( 'taxonomy' );
     1414        $term       = get_query_var( 'term' );
     1415
     1416        if ( ! ( is_tax() && in_array( $taxonomy, $taxonomies ) ) ) {
     1417            return;
     1418        }
     1419
     1420        $post_types  = get_parsed_post_types( 'labels' );
     1421        $post_types  = array( 'any' => __( 'Any type', 'wporg' ) ) + $post_types;
     1422
     1423        $qv_post_type = array_filter( (array) get_query_var( 'post_type' ) );
     1424        $qv_post_type = $qv_post_type ? $qv_post_type : array( 'any' );
     1425
     1426        $options = '';
     1427        foreach ( $post_types as $post_type => $label ) {
     1428            $selected = in_array( $post_type, $qv_post_type ) ? " selected='selected'" : '';
     1429            $options .= "\n\t<option$selected value='" . esc_attr( $post_type ) . "'>$label</option>";
     1430        }
     1431
     1432        $form = "<form method='get' class='archive-filter-form' action=''>";
     1433
     1434        if ( ! $wp_rewrite->using_permalinks() ) {
     1435            // Add taxonomy and term when not using permalinks.
     1436            $form .= "<input type='hidden' name='" . esc_attr( $taxonomy ) . "' value='" . esc_attr( $term ) . "'>";
     1437        }
     1438       
     1439        $form .= "<label for='archive-filter'>";
     1440        $form .= __( 'Filter by type:', 'wporg' ) . ' ';
     1441        $form .= '<select name="post_type[]" id="archive-filter">';
     1442        $form .= $options . '</select></label>';
     1443        $form .= "<input class='shiny-blue' type='submit' value='Filter' /></form>";
     1444
     1445        echo $form;
     1446    }
    13961447}
     1448
Note: See TracChangeset for help on using the changeset viewer.