• Resolved davidedante

    (@davidedante)


    Hi all,
    I am trying to build a design portfolio theme, I set up a custom post type named ‘portfolio’ and a custom taxonomy (for instance I will have wed design, product design etc.). I got a way to display a list of related taxonomy but I am not able to add the thumnail to related content to be displayed at the end of single custom post page.

    that’s the code I actually have (inside single-portfolio.php), does anyone know hot to display thumbnails too?

    <?php
    $sectors = get_the_terms( get_the_ID(), 'skills' );
    if ( ! is_wp_error( $sectors ) ) {
    	$term = array_shift( $sectors );
    
    	/* Query */
    	$jobs = null;
    	if ( isset( $term->slug ) && isset( $term->taxonomy ) ) {
    		$jobs = get_posts( array(
    			'term'        => $term->slug,
    			'taxonomy'    => $term->taxonomy,
    			'post_type'   => 'portfolio',
    			'post_status' => 'publish',
    			'exclude' => get_the_ID(), /* exclude current project */
    			) );
    	}
    
    	/* Loop */
    	if ( $jobs ) {
    		$_post = $post;
    		print '<div id="related">' . "\n". '';
    		foreach ( (array) $jobs as $post ) {
    			setup_postdata( $post );
    			the_title( '	<div class="related"><a href="' . esc_url( get_permalink() ) . '">', '</a></div>' . "\n". '' );
    		}
    		print '</div>';
    		$post = $_post;
    	}
    }
    
     ?>
Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter davidedante

    (@davidedante)

    ok, have no clue about how I did it but mixing a ‘related post by tag’ code and the one above I got what I was looking for.
    Now, since I have no php programming skills maybe you guy could just tell me if the following code is ‘correct’, I mean, it seems to provide the right output (on MAMP), but it is useful to understand if it is a ‘clean’ code that a third person searching this thread can safely use for another theme.

    <!-- related projects -->
    
    <?php $orig_post = $post;
    global $post;
    $tags = get_the_terms( get_the_ID(), 'skills' );
    if ($tags) {
    	$tag_ids = array();
    	foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
    	$args=array(
    		'term'        => $term->slug,
    		'taxonomy'    => $term->taxonomy,
    		'post_type'   => 'portfolio',
    		'post_status' => 'publish',
    		'exclude' => get_the_ID(), /* exclude current project */
    		'post__not_in' => array($post->ID),
    
    	);
    	$my_query = new wp_query( $args );
    
    	/* Loop */
    	if( $my_query->have_posts() ) {
    
    		echo '<div id="relatedposts"><h3>Related Posts</h3><ul>';
    
    		while( $my_query->have_posts() ) {
    			$my_query->the_post(); ?>
    
    			<li>
    				<div class="relatedthumb"><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_post_thumbnail(); ?></a></div>
    				<div class="relatedcontent">
    				<h3><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
    				<?php the_time('M j, Y') ?>
    				</div>
    			</li>
    		<? }
    		echo '</ul></div>';
    	}
    }
    $post = $orig_post;
    wp_reset_query(); ?>
    Thread Starter davidedante

    (@davidedante)

    mhh.. I didn’t see that actually this code shows all the custom post without filtering it by current single post taxonomy, this is the code I am using right know, can you guys help me to understand how to exclude not related taxonomy posts?

    <!-- related projects -->
    
    <?php $orig_post = $post;
    global $post;
    $tags = get_the_terms( get_the_ID(), 'skills' );
    if ($tags) {
    	$tag_ids = array();
    	foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
    	$args=array(
    		'term'        => $term->slug,
    		'taxonomy'    => $term->taxonomy,
    		'post_type'   => 'portfolio',
    		'post_status' => 'publish',
    		'post__not_in' => array($post->ID),
    
    	);
    	$my_query = new wp_query( $args );
    
    	/* Loop */
    	if( $my_query->have_posts() ) {
    
    		echo '<div id="relatedposts">
    	<h3>Related Posts</h3>';
    
    		while( $my_query->have_posts() ) {
    			$my_query->the_post(); ?>
    
    	<div class="post-thumb">
    		<a href="<? the_permalink()?>" rel="bookmark" title="">
    			<div class="view">View</div>
    			<?php
    				if(has_post_thumbnail()) {
    					the_post_thumbnail();
    				} else {
    					echo '<img src="'.get_bloginfo("template_url").'/images/default-thumbnail.png" />';
    				}
    			?>
    		</a>
    	</div>
    		<div class="relatedcontent">
    		<h4><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h4>
    	</div>
    		<? }
    		echo '</div>';
    	}
    }
    $post = $orig_post;
    wp_reset_query(); ?>
    Moderator keesiemeijer

    (@keesiemeijer)

    Try it with a tax_query: https://codex.wordpress.org/Function_Reference/WP_Query#Taxonomy_Parameters

    example [untested]:

    $args = array(
      'post_type'   => 'portfolio',
      'post_status' => 'publish',
      'post__not_in' => array($post->ID),
    	'tax_query' => array(
    		array(
    			'taxonomy' => $individual_tag->taxonomy,
    			'field' => 'slug',
    			'terms' => array($individual_tag->slug)
    		)
    	)
    );
    $my_query = new WP_Query( $args );

    taxonomy_exists?

    if taxonomy echo thumbnail

    Thread Starter davidedante

    (@davidedante)

    thank you for your reply guys,
    keesiemeijer, your tip worked!

    so, as you said, instead of

    'taxonomy'	=> $term->taxonomy,
    'field'		=> 'slug',
    'term'		=> $term->slug,

    I have to write this:

    'tax_query' => array(
    			array(
    				'taxonomy' => $individual_tag->taxonomy,
    				'field' => 'slug',
    				'terms' => array($individual_tag->slug)

    so the whole thing become

    <?php $orig_post = $post;
    global $post;
    $tags = get_the_terms( get_the_ID(), 'skills' );
    if ($tags) {
    	$tag_ids = array();
    	foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
    	$args = array(
    		'post_type'   => 'portfolio',
    		'post_status' => 'publish',
    		'post__not_in' => array($post->ID),
    		'tax_query' => array(
    			array(
    				'taxonomy' => $individual_tag->taxonomy,
    				'field' => 'slug',
    				'terms' => array($individual_tag->slug)
    			)
    		)
    	);
    	$my_query = new WP_Query( $args );
    
    	/* Loop */
    	if( $my_query->have_posts() ) {
    
    		echo '<div id="relatedposts">
    	<h3>Related Posts</h3>';
    
    		while( $my_query->have_posts() ) {
    			$my_query->the_post(); ?>
    
    	<div class="post-thumb">
    		<a href="<? the_permalink()?>" rel="bookmark" title="">
    			<div class="view">View</div>
    			<?php
    				if(has_post_thumbnail()) {
    					the_post_thumbnail();
    				} else {
    					echo '<img src="'.get_bloginfo("template_url").'/images/default-thumbnail.png" />';
    				}
    			?>
    		</a>
    	</div>
    		<div class="relatedcontent">
    		<h4><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h4>
    	</div>
    		<? }
    		echo '</div>';
    	}
    }
    $post = $orig_post;
    wp_reset_query(); ?>

    I am going to close the thread, meanwhile can you explain how this change solved the issue (I’d like to have a better PHP understandment)?

    Moderator keesiemeijer

    (@keesiemeijer)

    In your query ($args) you used “term” which isn’t a WP_Query parameter: https://codex.wordpress.org/Function_Reference/WP_Query#Parameters

    And you got the taxonomy and term slug from the foreach: foreach($tags as $individual_tag) { ... }, so you have to use “$individual_tag” and not “$term” as the variable to get the term’s taxonomy and slug.

    I’m glad you got it resolved.

    Thread Starter davidedante

    (@davidedante)

    thank you 🙂

    Hey guys this is just what I was looking for but when I use your code I get a: Parse error: syntax error, unexpected T_ENDWHILE

    I’m working on wampp with wordpress 3.3.1

    Thanks for any help.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘related taxonomy thumbnail for custom post type’ is closed to new replies.