1.wordpress获取指定形式的文章
先确定查询 参数:
$args = array(
'post_type'=> 'post',
'post_status' => 'publish',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-aside' )
)
)
);再去查询:
$asides = get_posts( $args );
if ( count($asides) ) {
foreach ( $asides as $aside ) {
// 在这里循环输出
}
}其它文章格式的查询参照上述示例代码。
当然了,你也可以其它查询函数,但是参数却类似上面列出的。
不了解再问。
祝愉快!
2.wordpress怎么在页面上添加相关的文章
方法一:标签相关 首先获取文章的所有标签,接着获取这些标签下的 n 篇文章,那么这 n 篇文章就是与该文章相关的文章了。
现在可以见到的WordPress相关文章插件都是使用的这个方法。下面是实现的代码:
- <?php global $post;$post_tags = wp_get_post_tags($post->ID); if ($post_tags) { foreach ($post_tags as $tag) { // 获取标签列表 $tag_list[] .= $tag->term_id; } // 随机获取标签列表中的一个标签 $post_tag = $tag_list[ mt_rand(0, count($tag_list) - 1) ]; // 该方法使用 query_posts() 函数来调用相关文章,以下是参数列表 $args = array( 'tag__in' => array($post_tag), 'category__not_in' => array(NULL), // 不包括的分类ID 'post__not_in' => array($post->ID), 'showposts' => 6, // 显示相关文章数量 '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 } } else { echo '* 暂无相关文章'; } wp_reset_query(); } else { echo '* 暂无相关文章'; }?> 方法二:分类相关 本方法是通过获取该文章的分类id,然后获取该分类下的文章,来达到获取相关文章的目的。
- <?php global $post;$cats = wp_get_post_categories($post->ID); if ($cats) { $args = array( 'category__in' => array( $cats[0] ), 'post__not_in' => array( $post->ID ), 'showposts' => 6, '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 } } else { echo '* 暂无相关文章'; } wp_reset_query(); } else { echo '* 暂无相关文章'; }?>。
3.如何调用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(); ?>。4.wordpress如何获取某个分类下的文章
WordPress 博客分类调用代码使用: 登陆博客后台,点击外观选项卡下的“编辑”选项进入当前主题编辑界面(也可以下载文件到本地进行编辑)在需要调用分类文章的地方添加以下调用代码 <?php $posts="get_posts(" "category="1&numberposts=10"" );="">?php> <?php if( $posts ) : ?> <?php foreach( $posts as $post ) : setup_postdata( $post ); ?> " rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?> <?php endforeach; ?> <?php endif; ?>category=1&numberposts=10:其中的1是指调用分类ID为1的文章,10是指调用该分类下最新的10篇文章提交更新文件即可。
附:WordPress 博客分类ID获取 1、登陆博客后台,点击“文章”下的分类目录选项卡; 2、找到相关分类目录,把鼠标的箭头移到分类目录名称上面; 3、这时浏览器底部的状态栏下会显示一个链接地址,地址最后面有_ID=1,其中的1便是该分类目录的ID。看图: 提醒:第二步中不需要点击目录名称,只是把鼠标放在上面即可;第三步的_ID=1中的1是本站测试目录的ID,各个目录的ID是不相同的,根据自己博客的情况选择ID。
如有帮助,请采纳,谢谢。
5.wordpress获取指定形式的文章
先确定查询 参数: $args = array( 'post_type'=> 'post', 'post_status' => 'publish', 'order' => 'DESC', 'tax_query' => array( array( 'taxonomy' => 'post_format', 'field' => 'slug', 'terms' => array( 'post-format-aside' ) ) ));再去查询: $asides = get_posts( $args );if ( count($asides) ) { foreach ( $asides as $aside ) { // 在这里循环输出 }}其它文章格式的查询参照上述示例代码。
当然了,你也可以其它查询函数,但是参数却类似上面列出的。 不了解再问。
祝愉快。
6.wordpress怎么获取最新文章与时间
这样。
<?php query_posts("showposts=10&orderby=date&order=DESC")?>
<?php while (have_posts()) : the_post(); ?>
<li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a><span><?php the_time('m-d'); ?></span></li>
<?php endwhile; ?>
7.在wordpress里从文章页面获取书名描述怎么获取
在wordpress中,不仅在首页,在其他任何地方调用页面,均可以通过get_page或get_pages这两个内置函数来实现调用一篇或多篇页面内容
1、利用get_page获取1个指定的页面内容
1
2
3
4
5
6
7
if ( is_home() ) : //这个判断非必需,这里表示将输出限定在首页
$page = get_page( 5 ); // 5为调用页面的ID
if( $page ) {
//var_dump($page);
printf("%s", apply_filters('the_content', $page->post_content) ); //输出页面内容
}
endif;
2、利用get_pages获取页面列表
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 详细的参数可参考/wp-includes/ -> post.php中get_pages函数中的说明
// 以下仅仅列出几个作为示例
$args = array(
'sort_column' => 'ID', //指定依id排序
'sort_order' => 'ASC', // 指定排序方式为升序
'number' => 6 // 指定只获取6篇页面内容
);
$pages = get_pages($args);
//var_dump($wpdb);
if(count($pages)) {
foreach ($pages as $key => $page) {
printf("%s", apply_filters('the_content', $page->post_content) ); //输出页面内容
}
}
总结:
页面其实是wordpress文章格式的一种,内置有很多较为直观的函数,主要位置/wp-includes/ -> post.php文件中,建议多看看就好。
8.wordpress通过文章来获取作者名字
显示文章的作者
显示文章作者的描述(作者个人资料中的描述)
显示文章作者的登录名
显示文章作者的firstname(名)
显示文章作者的lastname(姓)
显示文章作者的昵称
显示文章作者的ID号
显示文章作者的电子邮箱
显示文章作者的网站地址
(添加于2.1版本) 显示一个以文章作者名为链接名,链接地址为文章作者的网址的链接。
(不推荐使用) 显示文章作者的icq
显示文章作者的aim
显示文章作者的yim
(不推荐使用) 显示文章作者的msn
显示文章作者已发表文章的篇数
显示一个链接到文章作者已发表文章列表的链接
(不推荐使用) 显示blog所有作者和他们的相关信息。完整函数如下:
参数:
optioncount:是否显示各作者已发表文章的篇数,可选值为:TRUE 和 FALSE(默认值)
exclude_admin:是否不列出管理员,可选值为:TRUE(默认值) 和 FALSE
show_fullname :是否显示各作者的全名,可选值为:TRUE 和 FALSE(默认值)
hide_empty:是否不显示发表文章数为0的作者,可选值为:TRUE(默认值) 和 FALSE
feed:链接到各个作者发表文章的RSS供稿种子链接名,默认为空,不显示RSS供稿种子
feed_image:供稿种子的图片地址,如果提供此项,则覆盖上面的feed,默认为空
显示blog作者列表,如果作者发表过文章,则他的名字将链接到他发表的文章列表中。可定义是否显示其他信息。
参数:
optioncount:是否显示各个作者发表文章数,可选值:true 和 false(默认值)
exclude_admin:是否不显示”admin”用户,可选值:true(默认值) 和 false
show_fullname:是否显示各个作者的全名,如果不显示,将显示昵称。可选值:true 和 false(默认值)
hide_empty:是否不显示发表文章数为0的作者,可选值:true(默认值) 和 false
feed:链接到各个作者发表文章的RSS供稿种子链接名,默认为空,不显示RSS供稿种子
feed_image:供稿种子的图片地址,如果提供此项,则覆盖上面的feed,默认为空。
9.WordPress如何获取文章里面全部图片或者前几张图片
参考:
function wpdit_get_post_images($post_content){
preg_match_all('|<img.*?src=[\'"](.*?)[\'"].*?>|i', do_shortcode($post_content), $matches);
if($matches){
return $matches;//自行var_dump($matches)就可以看到效果了。
}else{
return false;
}
}
转载请注明出处51数据库 » wordpress获取相关文章