• Hi all, I’m desperately trying to figure out why this shortcode I created to display a custom post type which can be filtered by a custom taxonomy which I’m placing within the shortcode attributes to work.

    The code can be found below:

    /*******************************************************/
    /* Portfolio Posts Extraction
    /*******************************************************/
    
    //define shortcode
    function project_shortcode( $atts ) {
    	extract( shortcode_atts( array(
    		'limit' => '10',
    		'orderby' => 'date',
    		'catID' => 'seo',
    	), $atts ) );
    	// Creating custom query to fetch the project type custom post.
    	//query_posts(array('post_type'=>'videos', 'tax_query' => array( array('field' => 'slug', 'terms' => array( 'adult' ),'operator' => 'NOT IN',),)  ));
    
    	//get categories function
    
    	if( $categories ) {
    
    			$categories = explode( ',', $categories );
    
    			foreach ( $categories as $i => $category ) {
    
    				$category_slugs[$i] .= get_term( $category, 'portfolios-categories' )->slug;
    
    			}
    
    			$categories = implode( ',', $category_slugs );
    
    			$args = array_merge( $args, array( 'portfolios-categories' => esc_attr( $categories ) ) );
    
    		}
    
    //$loop = array('post_type' => 'portfolio', 'tax_query' => array( array( 'taxonomy' => 'portfolios-category','field' => 'slug', 'terms' => '5')	) );
    //$loop = new WP_Query( $args );
    
    $loop = new WP_Query(array('post_type' => 'portfolio', 'taxonomy' => 'portfolios-categories', 'field' => 'slug', 'terms' => 'seo', 'posts_per_page' => $limit, 'orderby' => $orderby));
    
       // Looping through the posts and building the HTML structure.
    	if($loop){
    
    		while ($loop->have_posts()){
    			$post_thumbnail_img = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'portfolio-one-fourth' );
    				$post_thumbnail_data = dbh_get_the_post_thumbnail_data( $post->ID );
    
    			 $loop->the_post();
    			 $output .= '<div class="span4"> <div class="easyBox">'; 
    
    			if( has_post_thumbnail() ) {
    				//get post thumbnail
    				$output .= ( $single_url ? '<a href="' . esc_url( $single_url ) . '" target="' . esc_attr( $single_target ) . '">' : null );
    				$output .= get_the_post_thumbnail( get_the_ID(), 'full', array('class' => 'photo') );
    				$output .= ( $single_url ? '</a>' : null );
    				}
    
    			 $output .=' <div class="contenthover">
                    <a href="/"><span class="entypo plus-squared"><i></i></span></a>
                </div>
    			<div class="inner">'
    			;
     			 $output .= '<a href="#"><span class="entypo monitor metaText"><i></i>website</span></a>';
    			 $output .= '<h3 class="light"><a href="'.get_permalink().'">'. get_the_title().'</a></h3>';
    			 $output .= '<div class="entry-content">'.get_the_excerpt().'</div></div><div></div></div></div>';
    		}
    	}
    	else
    		$output = 'Sorry, No portfolio items yet. Come back Soon.';
    	// Now we are returning the HTML code back to the place from where the shortcode was called.
    	return $output;
    }
    add_shortcode('projects','project_shortcode');

    So basically, I want to add this shortcode: [projects catID="seo"] to display only projects that have the category under the “portfolio-categories” custom taxonomy.

    Any help is truly appreciated! Thanks in advance for your help!

  • The topic ‘Help finishing a custom post type loop with custom taxonomy shortcode’ is closed to new replies.