WordPress

niestandardowe typy postów – wybierz kategorie według identyfikatorów/wyświetlanych tylko wybranych terminów

  • 16 maja, 2023
  • 5 min read
niestandardowe typy postów – wybierz kategorie według identyfikatorów/wyświetlanych tylko wybranych terminów


Moglibyśmy wyświetlać obrazy w galerii za pomocą krótkiego kodu w ten sposób . Chciałbym osiągnąć to samo, ale dla kategorii. Mam krótki kod, który wyświetla wszystkie warunki niestandardowego typu postu o nazwie „theme_portfolio_image”. Chcę, aby wyświetlały się tylko wybrane/określone kategorie.

<?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' );

Oto mój krótki kod

Warto przeczytać!  css — Strona miga, gdy ładuje się nowy film HTML5

[portfolio columns=”3″ title=”My Title”] ale chciałbym to wykorzystać w ten sposób

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

każda pomoc byłaby mile widziana


Źródło