0

We could display images in gallery using shortcode just like this [gallery ids="130,126"]. I would like to achieve same thing but for categories. I have a shortcode that displays all terms of a custom post type called "theme_portfolio_image". What I want is that I would like to dislay only selected/specific categories.

<?php

function theme_shortcode_portfolio($atts , $content = '' ) {
     // setup options
    extract( shortcode_atts( array(
        'title'      => '',
        'count'      => 3,
        'columns'    => 3,
        'cat'        => '',
        'style'      => '',
        'img_style'  => '',
        'portfolio'  => ''
    ), $atts ) );

    $query_options = array(
        'post_type'   => 'theme_portfolio_image',
        'numberposts' => $count,
        'orderby'     => 'menu_order',
        'order'       => 'ASC'
    );
    $filters = get_terms( 'theme_portfolio_categories', array( 'hide_empty' => 1 ) );

    if( !empty( $portfolio ) ) {
        $portfolios = explode( ',', $portfolio );
        $query_options['tax_query'][] = array(
            'taxonomy' => 'theme_portfolio_categories',
            'field' => 'slug',
            'terms' => $portfolios
        );

        // remove portfolios from filters
        foreach( $filters as $key => $filter ) {
            // remove portfolio from filter if not needed
            if( !in_array( $filter->slug, $portfolios ) ) {
                unset( $filters[$key] );
            }
        }
    }
    $span = $columns == 3? 'span4':'span3';
    $box_size = $columns == 3? 'box-huge':'box-big';
    // fetch posts
    $projects = get_posts( $query_options );
    $projects_count = count( $projects );
    $output = '';
    if( $projects_count > 0) {
        $projects_per_row = ($span == 'span4')? 3:4;
        $project_num = 1;

        $output.='<ul class="filters_menu"><li class="active" data-filter="*">' . __('All', THEME_FRONT_TD) . '</li>';
        foreach( $filters as $filter ) {
            $output.='<li data-filter=".'.$filter->slug.'">'.$filter->name.'</li>';
        }
        $output.='</ul><div class="filters-content"><div class="row grid">';

        foreach ($projects as $project) {
            global $post;
            $post = $project;
            setup_postdata($post);

            $format = get_post_format( $post->ID );
            if( false === $format ) {
                $format = 'standard';
            }

            $image_link = $format == 'link' ? '<div class="img-box"><a class="box-inner" href="' . get_the_content() . '">' : '<div class="img-box"><a class="box-inner" href="' . get_permalink() . '">';
            $title_link = $format == 'link' ? '<a href="'. get_the_content() . '">' : '<a href="'. get_permalink() . '">';
            $extra_gallery_images = array();

            $use_fancybox = get_post_meta( $post->ID, THEME_SHORT . '_open_fancybox' , true );
            if( $use_fancybox ) {
                switch ( $format ) {
                    case 'gallery':
                        $gallery_ids = theme_get_content_gallery( $post );
                        if( $gallery_ids !== null ) {
                            if( count( $gallery_ids ) > 0 ) {
                                // ok lets create a gallery
                                $gallery_rel = 'rel="gallery' . $post->ID . '"';
                                $gallery_image = wp_get_attachment_image_src( $gallery_ids[0], 'full');
                                $image_link = '<a class="box-inner fancybox" href="' . $gallery_image[0] . '" ' . $gallery_rel . '>';

                                // remove first gallery image from array
                                array_shift( $gallery_ids );
                                foreach( $gallery_ids as $gallery_image_id ) {
                                    $gallery_image = wp_get_attachment_image_src( $gallery_image_id, 'full');
                                    $extra_gallery_images[] = '<a class="fancybox" href="' . $gallery_image[0] . '" ' . $gallery_rel . '></a>';
                                }
                            }
                        }
                    break;
                    case 'video':
                        $video_shortcode = theme_get_content_shortcode( $post, 'embed' );
                        if( $video_shortcode !== null ) {
                            if( isset( $video_shortcode[5] ) ) {
                                $video_shortcode = $video_shortcode[5];
                                if( isset( $video_shortcode[0] ) ) {
                                    $image_link = '<a href="' . $video_shortcode[0] . '" class="box-inner fancybox-media">';
                                }
                            }
                        }
                    break;
                    case 'link':
                        $link_shortcode = theme_get_content_shortcode( $post, 'link' );
                        if( $link_shortcode !== null ) {
                            if( isset( $link_shortcode[5] ) ) {
                                $link_shortcode = $link_shortcode[5];
                                if( isset( $link_shortcode[0] ) ) {
                                    $title_link = '<a href="' . $link_shortcode[0] . '">';
                                    $image_link = '<a href="' . $link_shortcode[0] . '">';
                                }
                            }
                        }
                    break;
                    default:
                    case 'standard':
                        $featured_image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
                        $image_link = '<div class="img-box" href="' . $featured_image[0] . '">';
                    break;
                }
            }

            $filter_tags = get_the_terms( $post->ID, 'theme_portfolio_categories' );
            $author_id  = get_the_author_meta('ID');
            $post_filters = array();
            if( $filter_tags && ! is_wp_error( $filter_tags ) ) {
                foreach( $filter_tags as $tag ) {
                      $post_filters[] = '' .$tag->slug;
                }
            }
            $output .= '<div class="col-sm-six col-lg-four all '. $span .' '. implode( ' ', $post_filters ) .'" >';
            $output .= '<div class="box ' . $box_size . ' ' . $img_style . '"><div>';
            $output .= $image_link;
            $output .= get_the_post_thumbnail( $post->ID, 'portfolio-thumb', array( 'class' => 'img-rounded', 'alt' => get_the_title() ) ).'<i class="plus-icon"></i>';
            $output .= '</a></div>';
            $output .= '<figcaption><h4>';
            $output .= $title_link;
            $output .= get_the_title();
            $output .= '</a></h4>';
            if( $format !== 'link' ) {
                $output .= '<p>' . theme_limit_excerpt( get_the_excerpt(), theme_get_option( 'portfolio_excerpt_words' ) ) . '</p>';
            }
            else{
                $output .= '<p>' . get_the_content() . '</p>';
            }
            $output .= '</figcaption></div></div>';
            foreach( $extra_gallery_images as $extra_image ) {
                $output .= $extra_image;
            }
            $output .= '</div>';
        }
        $output .= '</div></div>';
    }
    wp_reset_postdata();
    return theme_shortcode_section( $atts, $output );

}

add_shortcode( 'portfolio', 'theme_shortcode_portfolio' );

Here is my shortcode

[portfolio columns="3" title="My Title"] but I would like to use it like this

[portfolio columns="3" title="My Title" ids="20,22,30,35"]

any help would be appreciated

1 Answer 1

0

It looks like this code is expecting slugs, which in the shortcode would look like this:

[portfolio columns="3" title="My Title" portfolio="cat20,cat22,cat35"]

(or whatever the actual category slugs are) instead of ids.

If slugs in the shortcode are ok, it looks like this might work as written. If you wanted IDs, you'd need to add 'ids'=> '' in shortcode_atts, then rewrite the filter and query logic to use id instead of slug.

1
  • nstead of ids I used slug and it works May 17, 2023 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.