当前位置:首页 > 电脑知识 > 正文内容

干货分享(wordpress调用指定文章)wordpress如何添加栏目,wordpress如何调用指定以及全部栏目下的tag标签相关文章列表,wordpress如何调用指定以及全部栏目下的tag标签相关文章列表,

sauo3年前 (2022-12-15)电脑知识182

WordPress 如何调用网站所有tag标签一般有二种方法,第一种是使用WORDPRESS提供的标签直接调用。

<?phpwp_tag_cloud(smallest=12&largest=18&unit=px&number=0&number=30&orderby=count&order=DESC);?>

上面这种可以直接调用出网站中所有的tag标签,但是样式比较单调,比较难控制它的样式。如果想更灵活的调用tag标签,可以使用以下的代码:

<?php$tags=get_tags(array(orderby=>count,order=>DESC,number=>30));foreach($tagsas$tag){$tag_link=get_tag_link($tag->term_id);echo<li><a href=".$tag_link.">.$tag->name.</a></li>;}?>

排序预设是以名称(name)做排序,若是自己写函数取标签清单,我们有两个栏位可以做排序,一是count、二是term_id。orderby的后面若是留空,则对term_id 排序。

我们可以通过TAG标签的ID来获取这个标签下的所有文章。

<?php$tag=$tag->term_id;//标签名/ID$args=array(tag=>$tag->term_id,showposts=>5,//输出的文章数量caller_get_posts=>1);$my_query=newWP_Query($args);if($my_query->have_posts()){while($my_query->have_posts()):$my_query->the_post();?><ahref="<?phpthe_permalink()?>"><?phpthe_title();?></a><?phpendwhile;}wp_reset_query();?>

还可以直接从数据库中读取所有的TAG标签。

<?php$varstag=$wpdb->get_results("SELECT * FROM `wp_term_taxonomy` where taxonomy=post_tag LIMIT 4000",ARRAY_A);foreach($varstagas$var){?><ahref="<?phpechoget_tag_link($var[term_id]);?>"target="_blank"><?php$tagnqme=get_tag($var[term_id]);echo$tagnqme->name;?></a><?php}?>

调用指定栏目下的tag标签相关文章列表

<?phpglobal$post;$post_tags=wp_get_post_tags($post->ID);if($post_tags){foreach($post_tagsas$tag){// 获取标签列表$tag_list[].=$tag->term_id;}$cat=get_the_category();foreach($catas$key=>$category){$catid=$category->term_id;}// 随机获取标签列表中的一个标签$post_tag=$tag_list[mt_rand(0,count($tag_list)-1)];// 该方法使用 query_posts() 函数来调用相关文章,以下是参数列表$args=array(tag__in=>array($post_tag),cat=>$catid,// 不包括的分类IDpost__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);?><divid="post-<?phpthe_ID();?>"class="col-md-4 mb-4"><divclass="portfolio"><ahref="<?phpthe_permalink()?>"title="<?phpthe_title();?>"><divclass="image"><imgsrc=<?phpif(has_post_thumbnail()){?><?php$large_image_url=wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),full);echo$large_image_url[0];?><?php}else{?><?phpbloginfo(template_url);?>/images/noneimg-portfolio.png <?php}?>class=alt=<?phpthe_title();?>><divclass="hover-effect"><divclass="hover-effect-inn"></div></div></div><h1><?phpthe_title();?></h1><p><?phpthe_excerpt();?></p></a></div></div><?php}}else{echo<li>* 暂无相关文章</li>;}wp_reset_query();}else{echo<li>* 暂无相关文章</li>;}?>
(window.slotbydup = window.slotbydup || []).push({ id: "u6835052", container: "_96bek913mdb", async: true });

本文链接:http://blog.sauo.top/?id=514 感谢分享!

分享到:

扫描二维码推送至手机访问。

版权声明:本文由冬眠先生个人博客发布,如需转载请注明出处。

本文链接:http://blog.sauo.top/?id=514

分享给朋友:

“干货分享(wordpress调用指定文章)wordpress如何添加栏目,wordpress如何调用指定以及全部栏目下的tag标签相关文章列表,wordpress如何调用指定以及全部栏目下的tag标签相关文章列表,” 的相关文章

干货分享(php file_get_contents curl)php file_get_contents post,php file_get_contents(): SSL operation failed with code 1. OpenSSL Error message.....,php file_get_contents(): SSL operation failed with code 1. OpenSSL Error message.....,

免费领取腾讯云服务器! 在调试php脚本代码时,发现使用 file_get_contents() 函数请求HTTPS的网址链接时出现了报错,其报错代码如下面所示“file_get_contents(): SSL operation failed with...

这都可以(PHP百度百科)pHp是什么,php中关于strtotime函数31日取前几个月日期的BUG,php中关于strtotime函数31日取前几个月日期的BUG,

免费领取腾讯云服务器! 在使用php脚本中的 strtotime 函数取前几个月的日期时,发现每到31日时,取出的前几个月的日期都会出现错误。仔细检查了一下,发现在利用 strtotime 函数取前几个月的日期时,给 strtotime 函数的参数并不规范...

一篇读懂(PHP for循环)php跳出循环的语句,php中的几种循环语句,php中的几种循环语句,

免费领取腾讯云服务器! 列举几种php脚本中常用的几种循环语句,比如,for,foreach,do....while 等。 php for循环 for循环在很多语言中都经常用到,比如js语言,c语言,java语言等。...

全程干货(查看php是否启动)查看php是否支持rar解压,查看php是ts版本还是nts版本的方法,查看php是ts版本还是nts版本的方法,

全程干货(查看php是否启动)查看php是否支持rar解压,查看php是ts版本还是nts版本的方法,查看php是ts版本还是nts版本的方法,

免费领取腾讯云服务器! 今天有用户来询问php的ts版本与php的nts版本的区别是什么,怎么去查看自己安装的php服务是ts版本还是nts版本,下面博文就来详细的说一下。 php ts版本与nts版本的区别 php官方提供...

干货分享(php输出json中文乱码)json数据中文乱码怎么办,解决 php json中文乱码的问题,解决 php json中文乱码的问题,

免费领取腾讯云服务器! php输出使用json_encode函数生成的json数据的时候,发现数据中的中文被转义成了unicode编码,如果使用ajax请求的json数据还好一点,浏览器会自动将unicode编码转义回来,但如果直接输出到浏览器上,那就会直...

没想到(imagettftext函数)imagettftext函数支持的编码类型,imagettftext(): any2eucjp(): invalid code in input string 错误解决方法,imagettftext(): any2eucjp(): invalid code in input string 错误解决方法,

免费领取腾讯云服务器! 最近在研究关于php生成图片的东西,发现了 imagettftext() 函数中一个不是错误的错误,具体的错误情况如下所示。 错误代码:...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。