测试成功,是通过TAG关键词调用相关文章,相关效果大家可以看本篇文章末尾的文章推荐,还是挺好用的
下面是代码,在文章模板添加的地方添加如下代码即可
通过tag调用相关文章代码
<?php $post_num = 10; global $post; $tmp_post = $post; $tags = ''; $i = 0; if ( get_the_tags( $post->ID ) ) { foreach ( get_the_tags( $post->ID ) as $tag ) $tags .= $tag->name . ','; $tags = strtr(rtrim($tags, ','), ' ', '-'); $myposts =get_posts('numberposts='.$post_num.'&tag='.$tags.'&exclude='.$post->ID); foreach($myposts as $post) { setup_postdata($post); ?> <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li> <?php } } $post = $tmp_post; setup_postdata($post); ?>
上面的数字是调用文章多少篇,现在代码上的显示10篇
调用随机文章代码
<?php $args = array( 'numberposts' => 2, 'orderby' => 'rand', 'post_status' => 'publish' ); $rand_posts = get_posts( $args ); foreach( $rand_posts as $post ) : ?> <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?>
调用置顶文章开始
<?php // 获取置顶文章代码 $sticky = get_option( 'sticky_posts' ); //获得所有置顶文章的id $args = array( 'numberposts' => 6, // 最多获取6篇置顶文章 'post__in' => $sticky ); $postQuery = get_posts($args); //循环输出置顶文章 foreach( $postQuery as $post ) : setup_postdata($post); ?> <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li> <?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } endforeach; ?>
随机调用同分类文章
<!-- 调用随机同类文章 --> <?php $cat = get_the_category(); foreach($cat as $key=>$category){ $catid = $category->term_id; } $args = array('orderby' => 'rand','showposts' => 2,'cat' => $catid ); $query_posts = new WP_Query(); $query_posts->query($args); while ($query_posts->have_posts()) : $query_posts->the_post(); ?> <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li> <?php endwhile;?> <?php wp_reset_query(); ?> <!-- 调用随机同类文章结束 -->
本文作者为青丝,转载请注明。
#(滑稽),不错,不错
打字不累,微笑面对[aru_1] 查看图片