Instantly share code, notes, and snippets.
Created
August 13, 2014 09:04
-
Star
(1)
1
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save caercam/b26e64859abdc995adfc to your computer and use it in GitHub Desktop.
relatedposts.php from WordPress moviestar theme: add movies support
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
global $SMTheme; | |
if ($SMTheme->get( 'layout', 'related' )) { ?> | |
<div class="related-posts"> | |
<h3><?php echo $SMTheme->_( 'relatedposts' )?>:</h3><ul> | |
<?php | |
$postid=$post->ID; | |
$showed_posts=array(); | |
$tags=get_the_tags($postid); | |
$categories = get_the_category($postid); | |
$post_cnt=0; | |
$first=0; | |
// Add support for collections and actors | |
$collections = array(); | |
$actors = array(); | |
// If collection taxonomy exists, get the terms | |
if ( taxonomy_exists( 'collection' ) ) { | |
if ( $collections = get_the_terms( $postid, 'collection' ) ) | |
foreach ( $collections as $i => $term ) | |
$collections[ $i ] = $term->slug; | |
} | |
// If actor taxonomy exists, get the terms | |
if ( taxonomy_exists( 'actor' ) ) { | |
if ( $actors = get_the_terms( $postid, 'actor' ) ) | |
foreach ( $actors as $i => $term ) | |
$actors[ $i ] = $term->slug; | |
} | |
if ($tags || $collections || $actors) { | |
$tags_ids = array(); | |
if ($tags) | |
foreach($tags as $tag) $tags_ids[] = $tag->term_id; | |
$args=array( | |
'post_type' => array( 'post', 'movie' ), // Add movie custom post type | |
'tag__in' => $tags_ids, | |
'post__not_in' => array($postid), | |
'showposts'=>$SMTheme->get( 'layout', 'relatedcnt' ), | |
'caller_get_posts'=>1); | |
// Add collections and actors custom taxonomies to the query | |
$args['tax_query'] = array( | |
'relation' => 'OR', | |
array( | |
'taxonomy' => 'collection', | |
'field' => 'slug', | |
'terms' => $collections, | |
), | |
array( | |
'taxonomy' => 'actor', | |
'field' => 'slug', | |
'terms' => $actors, | |
), | |
); | |
$posts=get_posts($args); | |
if( count($posts)>0 ) { | |
foreach ($posts as $p) { | |
$post_cnt++; | |
$showed_posts[]=$p->ID; | |
?> | |
<li<?php echo (!($first%4))?" class='first'":""?>><?php | |
if ($SMTheme->get( 'layout', 'colors' )) { | |
$r=rand(1,150); | |
$g=rand(1,150); | |
$b=rand(1,150); | |
} else { | |
$r=$g=$b=1; | |
} | |
$first++; | |
if(has_post_thumbnail($p->ID)) { | |
?><a href="<?php echo get_permalink($p->ID); ?>" title="<?php printf( $SMTheme->_( 'permalink' ), $p->post_title ); ?>" class='img'><?php | |
echo get_the_post_thumbnail($p->ID,array(126,126), array('title'=>$p->post_title, 'alt'=>$p->post_title)); | |
?></a><?php | |
} else { | |
?><a href="<?php echo get_permalink($p->ID); ?>" title="<?php printf( $SMTheme->_( 'permalink' ), $p->post_title ); ?>" class='img'><img src='<?php echo get_template_directory_uri(); ?>/images/smt/article_related.png' width='126' height='126' alt='article' /></a><?php | |
} | |
?><br /><a href="<?php echo get_permalink($p->ID) ?>" title="<?php printf( $SMTheme->_( 'permalink' ), $p->post_title ); ?>" rel="bookmark" class="related-ttl"><div class='related-bg' style='background:rgb(<?php echo $r.",".$g.",".$b?>)'></div><span><?php echo $p->post_title ?></span></a></li> | |
<?php | |
} | |
} | |
} | |
if ($categories&&($post_cnt<$SMTheme->get( 'layout', 'relatedcnt' ))) { | |
$category_ids = array(); | |
foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id; | |
$showed_posts[]=$postid; | |
$args=array( | |
'category__in' => $category_ids, | |
'post__not_in' => $showed_posts, | |
'showposts'=>$SMTheme->get( 'layout', 'relatedcnt' )-$post_cnt, | |
'caller_get_posts'=>1); | |
$posts=get_posts($args); | |
if( count($posts)>0 ) { | |
foreach ($posts as $p) { | |
$ttl=iconv_substr( $p->post_title, 0, 150, 'utf-8' ); | |
?> | |
<li<?php echo (!($first%4))?" class='first'":""?>><?php | |
$post_cnt++; | |
if ($SMTheme->get( 'layout', 'colors' )) { | |
$r=rand(1,150); | |
$g=rand(1,150); | |
$b=rand(1,150); | |
} else { | |
$r=$g=$b=1; | |
} | |
$first++; | |
if(has_post_thumbnail($p->ID)) { | |
?><a href="<?php echo get_permalink($p->ID); ?>" title="<?php printf( $SMTheme->_( 'permalink' ), $p->post_title ); ?>" class='img'><?php | |
echo get_the_post_thumbnail($p->ID,array(126,126), array('title'=>$p->post_title, 'alt'=>$p->post_title)); | |
?></a><?php | |
} else { | |
?><a href="<?php echo get_permalink($p->ID); ?>" title="<?php printf( $SMTheme->_( 'permalink' ), $p->post_title ); ?>" class='img'><img src='<?php echo get_template_directory_uri(); ?>/images/smt/article_related.png' width='126' height='126' alt='article' /></a><?php | |
} | |
?><br /><a href="<?php echo get_permalink($p->ID) ?>" title="<?php printf( $SMTheme->_( 'permalink' ), $p->post_title ); ?>" rel="bookmark" class="related-ttl"><div class='related-bg' style='background:rgb(<?php echo $r.",".$g.",".$b?>)'></div><span><?php echo $p->post_title ?></span></a></li> | |
<?php | |
} | |
} | |
} | |
if (!$post_cnt) { | |
?><p><?php echo $SMTheme->_( 'norelatedposts' )?></p><?php | |
} | |
?></ul></div> | |
<?php } ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment