php Genesis Framework相关文章(分类和标签)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Genesis Framework相关文章(分类和标签)相关的知识,希望对你有一定的参考价值。

add_action( 'genesis_entry_content', 'sk_related_posts', 12 );
/**
 * Outputs related posts with thumbnail
 *
 * @author Nick the Geek
 * @url http://designsbynickthegeek.com/tutorials/related-posts-genesis
 * @global object $post
 */
function sk_related_posts() {

	global $do_not_duplicate;

	if ( ! is_singular ( 'post' ) ) {
		return;
	}

	$count = 0;

	$related = '';

	$do_not_duplicate = array();

	$tags = wp_get_post_tags( get_the_ID() );

	$cats = wp_get_post_categories( get_the_ID() );

	// If we have some tags, run the tag query.
	if ( $tags ) {
		$query    = sk_related_tag_query( $tags, $count );
		$related .= $query['related'];
		$count    = $query['count'];
	}

	// If we have some categories and less than 5 posts, run the cat query.
	if ( $cats && $count <= 4 ) {
		$query    = sk_related_cat_query( $cats, $count );
		$related .= $query['related'];
		$count    = $query['count'];
	}

	// End here if we don't have any related posts.
	if ( ! $related ) {
		return;
	}

	// Display the related posts section.
	echo '<div class="related">';
		echo '<h3 class="related-title">Related Post :</h3>';
		echo '<ul class="related-posts-list">' . $related . '</ul>';
	echo '</div>';
}

function sk_related_tag_query( $tags, $count ) {

	global $do_not_duplicate;

	if ( ! $tags ) {
		return;
	}

	$postIDs = array( get_the_ID() );

	foreach ( $tags as $tag ) {
		$tagID[] = $tag->term_id;
	}

	$tax_query = array(
		array(
			'taxonomy'  => 'post_format',
			'field'     => 'slug',
			'terms'     => array(
				'post-format-link',
				'post-format-status',
				'post-format-aside',
				'post-format-quote'
				),
			'operator' => 'NOT IN'
		)
	);
	$args = array(
		'tag__in'               => $tagID,
		'post__not_in'          => $postIDs,
		'showposts'             => 5,
		'ignore_sticky_posts'   => 1,
		'tax_query'             => $tax_query,
	);

	$related  = '';

	$tag_query = new WP_Query( $args );

	if ( $tag_query->have_posts() ) {
		while ( $tag_query->have_posts() ) {
			$tag_query->the_post();

			$do_not_duplicate[] = get_the_ID();

			$count++;

			// $title = genesis_truncate_phrase( get_the_title(), 35 );
			$title = get_the_title();

			$related .= '<li class="related-post">';
		  	$related .= '<a class="related-post-title" href="' . get_permalink() . '" rel="bookmark" title="Permanent Link to ' . $title . '">' . $title . '</a>';
			$related .= '</li>';
		}
	}

	wp_reset_postdata();

	$output = array(
		'related' => $related,
		'count'   => $count
	);

	return $output;
}

function sk_related_cat_query( $cats, $count ) {

	global $do_not_duplicate;

	if ( ! $cats ) {
		return;
	}

	$postIDs = array_merge( array( get_the_ID() ), $do_not_duplicate );

	$catIDs = array();

	foreach ( $cats as $cat ) {
		if ( 3 == $cat ) {
			continue;
		}
		$catIDs[] = $cat;
	}

	$showposts = 5 - $count;

	$tax_query = array(
		array(
			'taxonomy'  => 'post_format',
			'field'     => 'slug',
			'terms'     => array(
				'post-format-link',
				'post-format-status',
				'post-format-aside',
				'post-format-quote'
				),
			'operator' => 'NOT IN'
		)
	);
	$args = array(
		'category__in'          => $catIDs,
		'post__not_in'          => $postIDs,
		'showposts'             => $showposts,
		'ignore_sticky_posts'   => 1,
		'orderby'               => 'rand',
		'tax_query'             => $tax_query,
	);

	$related  = '';

	$cat_query = new WP_Query( $args );

	if ( $cat_query->have_posts() ) {
		while ( $cat_query->have_posts() ) {
			$cat_query->the_post();

			$count++;

			// $title = genesis_truncate_phrase( get_the_title(), 35 );
			$title = get_the_title();

			$related .= '<li class="related-post">';
		  
			$related .= '<a class="related-post-title" href="' . get_permalink() . '" rel="bookmark" title="Permanent Link to ' . $title . '">' . $title . '</a>';
			$related .= '</li>';

		}
	}

	wp_reset_postdata();

	$output = array(
		'related' => $related,
		'count'   => $count
	);

	return $output;

}

以上是关于php Genesis Framework相关文章(分类和标签)的主要内容,如果未能解决你的问题,请参考以下文章

php 使用Genesis Framework自定义网站页脚。

php 使用Genesis Framework自定义网站页脚。

php 使用Genesis Framework将Ionicons排入您的网站。

php 使用Genesis Framework将Ionicons排入您的网站。

html 示例HTML使用Genesis Framework自定义网站页脚。

html 示例HTML使用Genesis Framework自定义网站页脚。