Question: Wordpress shortcode woocommerce projects plugin

Question

Wordpress shortcode woocommerce projects plugin

Answers 1
Added at 2016-12-28 19:12
Tags
Question

I am using the shortcode of the Woocommerce Projects plugin and I am stuck how can I only can show the projects that are in the same category as the project that the user is viewing.

I use this code:

Visit more projects of 

<?php
    $terms_as_text  = get_the_term_list( $post->ID, 'project-category' );   

    if ( $terms_as_text ) {
        echo wp_strip_all_tags( $terms_as_text);
    }
?>?

<?php echo do_shortcode( '[projects limit="3" columns="3" orderby="rand" order="desc" exclude_categories=""]' ); ?>

The text on top is working but how can I exclude all but the active category in the shortcode?

Answers to

Wordpress shortcode woocommerce projects plugin

nr: #1 dodano: 2017-01-01 06:01

Try this:

<?php
        $terms = get_terms( array(
            'taxonomy' => 'project-category',
        ) );
        //$post_terms = wp_get_post_terms( $post->ID, 'project-category' );
        $post_terms = wp_get_post_terms( $post->ID, 'project-category' );

        $cat_string = array();
        $in_string = true;
        if ( !empty( $terms ) ) {
            foreach($terms as $term){
                $in_string = true;
                foreach($post_terms as $post_term){
                    if( $post_term->name == $term->name ) {
                        $in_string = false;
                    }
                }
                if($in_string)
                    $cat_string[] = $term->term_id ;
            }
        }
        if(sizeof($cat_string) > 0)
            $cat_string = implode(',',$cat_string);
        else
            $cat_string = '';
        echo $cat_string;
        ?>

        <?php echo do_shortcode( '[projects limit="3" columns="3" orderby="rand" order="desc" exclude_categories="' . $cat_string . '"]' ); ?>

I'm surprised the extension doesn't come with that functionality. Seems a pretty obvious thing to include.

Also I got to say, this is a poor use of WooCommerce. There are much better ways to display portfolios.

Anyway, hope that helps.

Source Show
◀ Wstecz