1.如何调用wordpress相关文章
方法一:文章根据标签相关(用SQL获取)相关原理:首先获取改篇文章的所有标签,接着获取这些标签下的 n 篇文章,那么这 n 篇文章就是与该文章相关的文章了。
现在可以见到的WordPress相关文章插件都是使用的这个方法。这里我们通过SQL语句来直接读取数据库,随机获取 10篇相关的文章记录。
下面是实现的代码:
该文章的相关文章
<?php$all_tags = wp_get_post_tags($post->ID);if ($all_tags) {$tag_list = '';foreach ($all_tags as $tag){// 获取标签列表$tag_list .= $tag->term_id.',';}$tag_list = substr($tag_list, 0, strlen($tag_list)-1);$related_posts = $wpdb->get_results("SELECT post_title, IDFROM {$wpdb->prefix}posts, {$wpdb->prefix}term_relationships, {$wpdb->prefix}term_taxonomyWHERE {$wpdb->prefix}term_taxonomy.term_taxonomy_id = {$wpdb->prefix}term_relationships.term_taxonomy_idAND ID = object_idAND taxonomy = 'post_tag'AND post_status = 'publish'AND post_type = 'post'AND term_id IN (" . $tag_list . ")AND ID != '" . $post->ID . "'ORDER BY RAND()LIMIT 10");// 以上代码中的 10 为限制只获取10篇相关文章// 通过修改数字 10,可修改你想要的文章数量if ( $related_posts ) {foreach ($related_posts as $related_post) {?>ID); ?>" rel="bookmark" title="<?php echo $related_post->post_title; ?>"><?php echo $related_post->post_title; ?><?php } } else { ?>暂无相关文章<?php } } ?>方法二:根据文章的分类获取相关文章本方法是通过获取该文章的分类id,然后获取该分类下的文章,来达到获取相关文章的目的。我们用SQL语句来直接读取数据库,随机获取10篇相关文章记录。下面是实现的代码:
相关阅读推荐
<?php$data = wp_get_post_categories($post->ID);if ($data) {$related = $wpdb->get_results("SELECT post_title, IDFROM {$wpdb->prefix}posts, {$wpdb->prefix}term_relationships, {$wpdb->prefix}term_taxonomyWHERE {$wpdb->prefix}posts.ID = {$wpdb->prefix}term_relationships.object_idAND {$wpdb->prefix}term_taxonomy.taxonomy = 'category'AND {$wpdb->prefix}term_taxonomy.term_taxonomy_id = {$wpdb->prefix}term_relationships.term_taxonomy_idAND {$wpdb->prefix}posts.post_status = 'publish'AND {$wpdb->prefix}posts.post_type = 'post'AND {$wpdb->prefix}term_taxonomy.term_id = '" . $data[0] . "'AND {$wpdb->prefix}posts.ID != '" . $post->ID . "'ORDER BY RAND()LIMIT 10");if ( $related ) {foreach ($related as $related_post) {?>ID); ?>" rel="bookmark" title="<?php echo $related_post->post_title; ?>"><?php echo $related_post->post_title; ?><?php } } else { ?>暂无相关文章<?php } }?>方法三:根据作者相关获取文章(这个比较少用,因为基本都是我们自己发的)该方法是获取该文章作者的其他文章来充当相关文章,代码如下:该作者的相关文章
<?php$post_author = get_the_author_meta( 'user_login' );$args = array('author_name' => $post_author,'post__not_in' => array($post->ID),'showposts' => 10, // 显示相关文章数量'orderby' => date, // 按时间排序'caller_get_posts' => 1);query_posts($args);if (have_posts()) :while (have_posts()) : the_post(); update_post_caches($posts); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?><?php endwhile; else : ?>暂无相关文章<?php endif; wp_reset_query(); ?>。2.wordpress调用某一文章分类到某一页面下
懂你的意思 是不是想用wordpress 里的 page 页面做 自定义分类页面?方法如下:首先 要做一个 page模板 就是你 要实现的分类页面效果新建一个php页面 头部写上<?php template="" name:="" page1="" 这个是page模板的名字*/?="">然后用get_posts调用相应分类文章 如下: get_posts的用法可以 到 去看看<?php unset($args);$args="array('category_name'=">'group');$postlist = get_posts($args);?> <?php foreach($postlist as $post) : ?>/////////////////////////////////// 文章循环部分///////////////<?php endforeach;="" ?="">也可以用query_posts 不过个人感觉 get_posts 效率高些 做完之后将php文件保存在你的模板目录。
然后去 wordpress后台 新建一个page 在右下角 有选择 模板文件,选择上面取的那个page模板就可以·了。
转载请注明出处51数据库 » wordpress调用个人资料
?php>?php>?php>