• Hi.

    I managed to hack the template file “facstaff-category-template.php” to list a single category with a shortcode option. Just replace the file in the folder ‘display-templates’. Use it for example like [fsdirectory cat='admin']

    Ivo

    ——————

    <?php
    $a = shortcode_atts( array('cat' => 'all',), $atts );
    $my_category = $a['cat'];
    $custom_terms = get_terms('profile-category');
    
    foreach($custom_terms as $custom_term) {
        wp_reset_query();
        $args = array('post_type' => 'facstaff',
            'orderby'=>'title',
    	'order'=>'ASC',
    	'posts_per_page' => -1, // Unlimited posts
            'tax_query' => array(
                array(
                    'taxonomy' => 'profile-category',
                    'field' => 'slug',
                    'terms' => $custom_term->slug,
                ),
            ),
        );
    
        $loop = new WP_Query($args);
        if($loop->have_posts() && ($custom_term->slug==$my_category || $my_category=='all')) {
            if($my_category=='all') echo '<h2>'.$custom_term->name.'</h2>';
            while($loop->have_posts()) : $loop->the_post();
                echo '<a href="'.get_permalink($facstaff_posts->ID).'">'.get_the_title().'</a><br>';
            endwhile;
         }
    }

    https://wordpress.org/plugins/faculty-and-staff-directory/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter ivokwee

    (@ivokwee)

    Yes! This version has also an ‘exclude’ option, and accepts multiple categories.

    Examples:
    [fsdirectory cat='admin,students']
    [fsdirectory exclude='former-members']

    <?php
    $a = shortcode_atts( array(
       'cat' => 'all',
       'exclude' => '',
       ), $atts );
    $cat_include = explode(",",$a['cat']);
    $cat_exclude = explode(",",$a['exclude']);
    $custom_terms = get_terms('profile-category');
    
    foreach($custom_terms as $custom_term) {
        wp_reset_query();
        $args = array('post_type' => 'facstaff',
            'orderby'=>'title',
    	'order'=>'ASC',
    	'posts_per_page' => -1, // Unlimited posts
            'tax_query' => array(
                array(
                    'taxonomy' => 'profile-category',
                    'field' => 'slug',
                    'terms' => $custom_term->slug,
                ),
            ),
        );
    
        $loop = new WP_Query($args);
        $cat_match = (in_array($custom_term->slug, $cat_include) || $cat_include[0]=='all');
        if( $loop->have_posts() && ( $cat_match && !in_array($custom_term->slug, $cat_exclude))) {
            if($cat_include[0]=='all') echo '<h2>'.$custom_term->name.'</h2>';
            while($loop->have_posts()) : $loop->the_post();
                echo '<a href="'.get_permalink($facstaff_posts->ID).'">'.get_the_title().'</a><br>';
            endwhile;
         }
    }
    ?>
    Thread Starter ivokwee

    (@ivokwee)

    This version can set the category name on/off (no heading).

    Example:
    [fsdirectory cat='admin' name='false']

    <?php
    $a = shortcode_atts( array(
       'cat' => 'all',
       'exclude' => '',
       'name' => 'true',
       ), $atts );
    $cat_include = explode(",",$a['cat']);
    $cat_exclude = explode(",",$a['exclude']);
    $cat_name    = ($a['name']==='true');
    $custom_terms = get_terms('profile-category');
    
    foreach($custom_terms as $custom_term) {
        wp_reset_query();
        $args = array('post_type' => 'facstaff',
            'orderby'=>'title',
    	'order'=>'ASC',
    	'posts_per_page' => -1, // Unlimited posts
            'tax_query' => array(
                array(
                    'taxonomy' => 'profile-category',
                    'field' => 'slug',
                    'terms' => $custom_term->slug,
                ),
            ),
        );
    
        $loop = new WP_Query($args);
        $cat_match = (in_array($custom_term->slug, $cat_include) || $cat_include[0]=='all');
        if( $loop->have_posts() && ( $cat_match && !in_array($custom_term->slug, $cat_exclude))) {
            if($cat_name) echo '<h2>'.$custom_term->name.'</h2>';
            while($loop->have_posts()) : $loop->the_post();
                echo '<a href="'.get_permalink($facstaff_posts->ID).'">'.get_the_title().'</a><br>';
            endwhile;
         }
    }
    ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘shortcode for list of one category (hack)’ is closed to new replies.