美文网首页
WordPress各种标签调用集合

WordPress各种标签调用集合

作者: 竞价代运营 | 来源:发表于2022-02-16 22:06 被阅读0次

    WordPress各种标签调用集合

    wordpress是一种使用PHP语言开发的博客平台,用户可以在支持PHP和MySQL 数据库的服务器上架设自己的网志,插件众多,易于扩充功能。安装和使用都非常方便。目前 WordPress 已经成为主流的 Blog 搭建平台。这个网站就用的是WordPress构建的。

    WordPress模板基本文件style.css 样式表文件index.php 主页文件single.php 日志单页文件page.php 页面文件archvie.php 分类和日期存档页文件searchform.php 搜索表单文件search.php 搜索页面文件comments.php 留言区域文件(包括留言列表和留言框)404.php 404错误页面header.php 网页头部文件sidebar.php 网页侧边栏文件footer.php 网页底部文件

    WordPress Header头部 PHP代码

    注: 也就是位于<head>和</head>之间的PHP代码

    <?phpbloginfo(‘name’);?>网站标题<?phpwp_title();?>日志或页面标题<?phpbloginfo(‘stylesheet_url’);?>WordPress主题样式表文件style.css的相对地址<?phpbloginfo(‘pingback_url’);?>WordPress博客的Pingback地址<?phpbloginfo(‘template_url’);?>WordPress主题文件的相对地址<?phpbloginfo(‘version’);?>博客的Wordpress版本<?phpbloginfo(‘atom_url’);?>WordPress博客的Atom地址<?phpbloginfo(‘rss2_url’);?>WordPress博客的RSS2地址<?phpbloginfo(‘url’);?>WordPress博客的绝对地址<?phpbloginfo(‘name’);?>WordPress博客的名称<?phpbloginfo(‘html_type’);?>网站的HTML版本<?phpbloginfo(‘charset’);?>网站的字符编码格式

    WordPress 主体模板 PHP代码

    <?phpthe_content();?>日志内容<?phpif(have_posts()):?>确认是否有日志<?phpwhile(have_posts()):the_post();?>如果有,则显示全部日志<?phpendwhile;?>结束PHP函数”while”<?phpendif;?>结束PHP函数”if”<?phpget_header();?>header.php文件的内容<?phpget_sidebar();?>sidebar.php文件的内容<?phpget_footer();?>footer.php文件的内容<?phpthe_time(‘m-d-y’)?>显示格式为”02-19-08″的日期<?phpcomments_popup_link();?>显示一篇日志的留言链接<?phpthe_title();?>显示一篇日志或页面的标题<?phpthe_permalink()?>显示一篇日志或页面的永久链接/URL地址<?phpthe_category(‘,‘)?>显示一篇日志或页面的所属分类<?phpthe_author();?>显示一篇日志或页面的作者<?phpthe_ID();?>显示一篇日志或页面的ID<?phpedit_post_link();?>显示一篇日志或页面的编辑链接<?phpget_links_list();?>显示Blogroll中的链接<?phpcomments_template();?>comments.php文件的内容<?phpwp_list_pages();?>显示一份博客的页面列表<?phpwp_list_cats();?>显示一份博客的分类列表## 分类目录函数wp_list_cats() 或 分类列表函数wp_list_categories()用法举例

    1、按照字母排序,并只显示 ID 为16、3、9和5的指定分类:

    <ul>

    <?php

    wp_list_categories(‘orderby=name&include=3,5,9,16‘); ?>

    </ul>

    2、按照字母排序,显示每个分类的日志数,但不显示 ID 为10的分类

    <ul>

    <?php

    wp_list_categories(‘orderby=name&show_count=1&exclude=10‘); ?>

    </ul>

    3、显示或隐藏列表头,在分类函数 wp_list_categories 中,title_li 这个参数用于设置或者隐藏分类列表的头或者标题。它的默认值是:‘(__(’Categories’)’ ,这也就是为什么我们在不另设置分类列表标题的时候,它会显示“Categories”的原因。如果你在这里不设置任何参数,那么它将什么都不会显示。下 面的例子是排除 ID 为4和7并且隐藏列表头的分类列表:

    <ul>

    <?php

    wp_list_categories(‘exclude=4,7&title_li=‘); ?>

    </ul>

    接下来的例子是仅仅只显示 ID为5、9和23,并且列表头显示为“诗歌”的分类列表:

    <ul>

    <?php

    wp_list_categories(‘include=5,9,23&title_li=<h2>‘ . __(‘诗歌‘) . ‘</h2>‘ ); ?>

    </ul>

    4、仅显示某个分类下的子分类,下面的示例代码生成了 ID 为8的父分类下的子分类根据其 ID 进行排序的链接列表(读起来真绕口 -__-|||),它会显示每个分类下的文章数,并且隐藏链接的 title 标签中的分类描述,注意:如果父分类下没有任何文章,那么父分类将不会显示

    <ul>

    <?php wp_list_categories(‘orderby=id&show_count=1

    &use_desc_for_title=0&child_of=8‘); ?>

    </ul>

    这个函数里设置的参数比较多,这里我稍作说明:我们可以看到不同参数之间使用了“&”这个“与符号”来进行区分连接,orderby=id 按照 ID 排序,show_count=1 显示分类下的文章数,use_desc_for_title=0 隐藏分类描述,child_of=8 指定 ID 为8的子分类。

    5、显示带有 RSS Feed 链接的分类列表,下面代码根据分类名对分类列表排序,并显示每个分类下的文章数和 RSS 的 Feed 链接。

    <ul>

    <?php

    wp_list_categories(‘orderby=name&show_count=1&feed=RSS‘); ?>

    </ul>

    还可以使用 RSS 图标代替 RSS 链接

    <ul>

    <?php

    wp_list_categories(‘orderby=name&show_count=1

    &feed_image=/images/rss.gif‘); ?>

    </ul>

    6、标记和样式化分类列表,从上面的例子中可以看到,我们将分类列表函数: wp_list_categories() 套用在 ul 和 li 标签里,除此外我们还可以对其进行其它的样式化,个人认为这些工作直接在 CSS 里设置即可,原文档中的方法实际作用并不是很大,这里我就不多做介绍,有兴趣的朋友可以 参考这里

    7、style

    (字符串)分类列表显示的样式。将分类列表的方式显示(使用
    标签分隔列表项)。默认设置为列表(无序列表)。有效值:

    list – 默认

    none

    例子:

    <ul>

    <?php

    wp_list_categories(‘orderby=name&style=none‘); ?>

    </ul>

    <?phpnext_post_link(‘%link ‘)?>下一篇日志的URL地址<?phpprevious_post_link(‘%link’)?>上一篇日志的URL地址<?phpget_calendar();?>调用日历<?phpwp_get_archives()?>显示一份博客的日期存档列表<?phpposts_nav_link();?>显示较新日志链接(上一页)和较旧日志链接(下一页)<?phpbloginfo(‘description’);?>显示博客的描述信息

    其它的一些Wordpress模板代码

    /%postname%/ 显示博客的自定义永久链接<?php the_search_query(); ?>搜索表单的值<?php _e(‘Message’); ?>打印输出信息<?php wp_register(); ?>显示注册链接<?php wp_loginout(); ?>显示登入/登出链接<!–nextpage–>在日志或页面中插入分页<!–more–>截断日志<?php wp_meta(); ?>显示管理员的相关控制信息<?php timer_stop(1); ?>显示载入页面的时间<?php echo get_num_queries(); ?>显示载入页面查询

    wordpress调用最新文章

    WordPress最新文章的调用可以使用一行很简单的模板标签wp_get_archvies来实现. 代码如下:

    <?phpget_archives(‘postbypost’,10);?>(显示10篇最新更新文章)或者<?phpwp_get_archives(‘type=postbypost&limit=20&format=custom’);?>

    后面这个代码显示你博客中最新的20篇文章,其中format=custom这里主要用来自定义这份文章列表的显示样式。具体的参数和使用方法你可 以参考官方的使用说明- wp_get_archvies。(fromat=custom也可以不要,默认以UL列表显示文章标题。)

    补充: 通过WP的query_posts()函数也能调用最新文章列表, 虽然代码会比较多一点,但可以更好的控制Loop的显示,比如你可以设置是否显示摘要。具体的使用方法也可以查看官方的说明。

    wordpress调用随机文章

    <?php$rand_posts=get_posts(‘numberposts=10&orderby=rand’);foreach($rand_postsas$post):?><!–下面是你想自定义的Loop–><li><ahref=”<?phpthe_permalink();?>”><?phpthe_title();?></a></li><?phpendforeach;?>

    wordpress调用最新留言

    下面是我之前在一个Wordpress主题中代到的最新留言代码,具体也记不得是哪个主题了。该代码直接调用数据库显示一份最新留言。其中 LIMIT 10限制留言显示数量。绿色部份则是每条留言的输出样式。

    <?phpglobal$wpdb;$sql=“SELECTDISTINCTID,post_title,post_password,comment_ID,comment_post_ID,comment_author,comment_date_gmt,comment_approved,comment_type,comment_author_url,SUBSTRING(comment_content,1,30)AScom_excerptFROM$wpdb->commentsLEFTOUTERJOIN$wpdb->postsON($wpdb->comments.comment_post_ID=$wpdb->posts.ID)WHEREcomment_approved=’1′ANDcomment_type=”ANDpost_password=”ORDERBYcomment_date_gmtDESCLIMIT10″;$comments=$wpdb->get_results($sql);$output=$pre_HTML;foreach($commentsas$comment){$output.=“n<li>”.strip_tags($comment->comment_author).”:”.”<a href=””.get_permalink($comment->ID).“#comment-” . $comment->comment_ID . “” title=”on ” .$comment->post_title.“”>”.strip_tags($comment->com_excerpt).”</a></li>”;}$output.=$post_HTML;echo$output;?>

    4.wordpress调用相关文章

    在文章页显示相关文章

    <?php$tags=wp_get_post_tags($post->ID);if($tags){$first_tag=$tags[0]->term_id;$args=array(‘tag__in’=>array($first_tag),‘post__not_in’=>array($post->ID),‘showposts’=>10,‘caller_get_posts’=>1);$my_query=newWP_Query($args);if($my_query->have_posts()){while($my_query->have_posts()):$my_query->the_post();?><li><ahref=”<?phpthe_permalink()?>”rel=”bookmark”title=”<?phpthe_title_attribute();?>”><?phpthe_title();?><?phpcomments_number(‘ ‘,’(1)’,’(%)’);?></a></li><?phpendwhile;}}wp_reset_query();?>

    5.wordpress调用指定分类的文章

    方法1:

    <?php $posts = get_posts( “category=4&numberposts=10″ ); ?><?php if( $posts ) : ?><ul><?php foreach( $posts as $post ) : setup_postdata( $post ); ?><li><a href=”<?php the_permalink() ?>” rel=”bookmark” title=”<?php the_title(); ?>”><?php the_title(); ?></a></li><?php endforeach; ?></ul><?php endif; ?>

    方法2:

    <?php $posts = get_posts( "category=1&numberposts=10" ); ?><?php if( $posts ) : ?><ulclass="news_ful"><?php while (have_posts()) : the_post(); ?><li><aclass="animated an4"href=""title=""><label><? echo wp_trim_words( get_the_title(),24); ?></label><span><?php  the_time('Y-m-d'); ?></span></a></li><?php endwhile;wp_reset_query(); ?><?php endif; ?>

    <?php$thiscat=get_category($cat);echo$thiscat->name;?>调用指定分类名称标题<?php$cat=get_category($cid);echo$cat->slug;?>调用指定分类别名<?phpechoget_cat_ID($cat_name)?>调用指定分类名称对应的分类ID<?phpechoget_category_link($cid)?>调用指定分类url链接//$cid为整型变量wp_list_categories(‘orderby=name&include=3,5,9,16‘); ?>

    WordPress获取指定分类下指定子分类

    <ulclass="busul"><?php $categories = array(9,10,11,12,13);

    foreach ($categories as $cid) { ?>

    <?php query_posts("showposts=8&cat=$cid") ?><li><aclass="img  animated an6"href=""title=""><?php single_cat_title() ?></a></li><?php } wp_reset_query(); ?></ul>

    `或者

    cat),

    'hide_empty' => 0,

    );

    categories=get_categories(categories=getcategories(args);

    foreach(

    category) {

    if((category->term_id)==category−>termid)==cat){

    echo '

  1. ';
  2. }else{

    echo '

  3. ';
  4. }

    echo 'category->term_id ).'">'.category−>termid).′">′.category->name.'';

    echo '';

    echo "\r";

    }

    ?>`

    WordPress获取指定分类下全部子分类

    <?phpwp_list_cats('sort_column=name&optioncount=1&hierarchical=1&hide_empty=0&child_of=51');?>

    代码解释:sort_column=name为调用分类名;optioncount=1为调用分类下的文章数量;hide_empty=0为是否隐藏没有文章的分类;child_of=51为调用的父分类ID号,需要改为你自己的父分类的ID号。

    以上的代码是同时调用出子分类名和子分类下的文章数,如果只想调用子分类名,不需要显示文章数量,可以使用以下的代码:

    <?phpwp_list_cats('sort_column=name&optioncount=0&hierarchical=1&hide_empty=0&child_of=51');?>

    一、指定分类下的子分类获取方法:

    <?phpwp_list_cats('sort_column=name&optioncount=1&hierarchical=1&hide_empty=0&child_of=10');?>

    说明:

    child_of=10中的10是指某个分类的ID号。

    sort_column:ID 或 name,预设为ID,设定依 ID 值或分类名称排序

    sort_order:asc 或 desc,预设为递增 asc,设定排序递增或递减 (&sort_column=ID&sort_order=desc )

    二、指定页面下的子页面获取方法:

    <?phpwp_list_pages(‘hide_empty=0&child_of=5&title_li=’);?>

    说明:

    child_of=5中的5是指某个页面的ID号。

    6.wordpress去评论者链接的评论输出

    <?phpglobal$wpdb;$sql=“SELECTDISTINCTID,post_title,post_password,comment_ID,comment_post_ID,comment_author,comment_date_gmt,comment_approved,comment_type,comment_author_url,SUBSTRING(comment_content,1,14)AScom_excerptFROM$wpdb->commentsLEFTOUTERJOIN$wpdb->postsON($wpdb->comments.comment_post_ID=$wpdb->posts.ID)WHEREcomment_approved=’1′ANDcomment_type=”ANDpost_password=”ORDERBYcomment_date_gmtDESCLIMIT10″;$comments=$wpdb->get_results($sql);$output=$pre_HTML;foreach($commentsas$comment){$output.=“<li>”.strip_tags($comment->comment_author).”:”.”<a href=””.get_permalink($comment->ID).“#comment-” . $comment->comment_ID . “” title=”on ” .$comment->post_title.“”>”.strip_tags($comment->com_excerpt).”</a></li>”;}$output.=$post_HTML;echo$output;?>

    7.wordpress调用含gravatar头像的评论输出

    <?phpglobal$wpdb;$sql=“SELECTDISTINCTID,post_title,post_password,comment_ID,comment_post_ID,comment_author,comment_date_gmt,comment_approved,comment_author_email,comment_type,comment_author_url,SUBSTRING(comment_content,1,10)AScom_excerptFROM$wpdb->commentsLEFTOUTERJOIN$wpdb->postsON($wpdb->comments.comment_post_ID=$wpdb->posts.ID)WHEREcomment_approved=’1′ANDcomment_type=”ANDcomment_author!=‘郑 永’ANDpost_password=”ORDERBYcomment_date_gmtDESCLIMIT10″;$comments=$wpdb->get_results($sql);$output=$pre_HTML;foreach($commentsas$comment){$output.=“<li>”.get_avatar(get_comment_author_email(‘comment_author_email’),18).”<a href=””.get_permalink($comment->ID).“#comment-” . $comment->comment_ID . “” title=”” . $comment->post_title . ” 上的评论”>”. strip_tags($comment->comment_author) .”: “. strip_tags($comment->com_excerpt) .”</a></li>”;}$output.=$post_HTML;$output=convert_smilies($output);echo$output;?>

    上面代码把comment_author的值改成你的ID,18是头像大小,10是评论数量。

    8.wordpress调用网站统计大全

    1、日志总数:

    <?php$count_posts=wp_count_posts();echo$published_posts=$count_posts->publish;?>

    2、草稿数目:

    <?php$count_posts=wp_count_posts();echo$draft_posts=$count_posts->draft;?>

    3、评论总数:

    <?phpecho$wpdb->get_var(“SELECTCOUNT(*)FROM$wpdb->comments”);?>

    4、成立时间:

    <?phpechofloor((time()-strtotime(“2008-8-18″))/86400);?>

    5、标签总数:

    <?phpecho$count_tags=wp_count_terms(‘post_tag’);?>

    6、页面总数:

    <?php$count_pages=wp_count_posts(‘page’);echo$page_posts=$count_pages->publish;?>

    7、分类总数:

    <?phpecho$count_categories=wp_count_terms(‘category’);?>

    8、链接总数:

    <?php$link=$wpdb->get_var(“SELECTCOUNT(*)FROM$wpdb->linksWHERElink_visible=‘Y’”);echo$link;?>

    9、用户总数:

    <?php$users=$wpdb->get_var(“SELECTCOUNT(ID)FROM$wpdb->users”);echo$users;?>

    10、最后更新:

    <?php$last=$wpdb->get_results(“SELECTMAX(post_modified)ASMAX_mFROM$wpdb->postsWHERE(post_type=‘post’ORpost_type=‘page’)AND(post_status=‘publish’ORpost_status=‘private’)”);$last=date(‘Y-n-j’,strtotime($last[0]->MAX_m));echo$last;?>

    9.wordpress判断语句

    is_single()判断是否是具体文章的页面is_single(’2′)判断是否是具体文章(id=2)的页面is_single(‘Beef Stew’)判断是否是具体文章(标题判断)的页面is_single(‘beef-stew’)判断是否是具体文章(slug判断)的页面comments_open()是否留言开启pings_open()是否开启pingis_page()是否是页面is_page(’42′)id判断,即是否是id为42的页面is_page(‘About Me’)判断标题is_page(‘about-me’)slug判断is_category()是否是分类is_category(’6′)id判断,即是否是id为6的分类is_category(‘Cheeses’)分类title判断is_category(‘cheeses’)分类 slug判断in_category(’5′)判断当前的文章是否属于分类5is_author()将所有的作者的页面显示出来is_author(’1337′)显示author number为1337的页面is_author(‘Elite Hacker’)通过昵称来显示当前作者的页面is_author(‘elite-hacker’)下面是通过不同的判断实现以年、月、日、时间等方式来显示归档is_date()is_year()is_month()is_day()is_time()判断当前是否是归档页面is_archive()判断是否是搜索is_search()判断页面是否404is_404()

    判断是否翻页,比如你当前的blog是http://domain.com显示http://domain.com?paged=2的时候,这个判断将返 回真,通过这个函数可以配合is_home来控制某些只能在首页显示的界面,

    例如:

    <?phpif(is_single()):?>//这里写你想显示的内容,包括函数<?phpendif;?>或者:<?phpif(is_home()&&!is_paged()):?>//这里写你想显示的内容,包括函数<?phpendif;?>

    10.wordpress非插件同步twitter

    set_feed_url(‘http://feeds.feedburner.com/agting′);$feed->set_file_class(‘WP_SimplePie_File’);$feed->set_cache_duration(600);$feed->init();$feed->handle_content_type();$items = $feed->get_items(0,1);foreach($items as $item) {echo ‘@用户名: ‘.$item->get_description();}?>

    代码中的agting改成你的twitter用户名,改成你的名字。

    另一种调用方法需要你的空间是国外主机:

    <?php// Your twitter username.$username=“wange1228″;// Prefix – some text you want displayed before your latest tweet.// (HTML is OK, but be sure to escape quotes with backslashes: for example href=”link.html”)// Suffix – some text you want display after your latest tweet. (Same rules as the prefix.)$suffix=“”;$feed=“http://search.twitter.com/search.atom?q=from:” . $username . “&rpp=1″;functionparse_feed($feed){$stepOne=explode(“<content type=”html”>”,$feed);$stepTwo=explode(“</content>”,$stepOne[1]);$tweet=$stepTwo[0];$tweet=str_replace(“<”,“<”,$tweet);$tweet=str_replace(“>”,“>”,$tweet);return$tweet;}$twitterFeed=file_get_contents($feed);echostripslashes($prefix).parse_feed($twitterFeed).stripslashes($suffix);?>

    总结一下这个方法的特点:

    1、非插件.

    2、不用验证用户名和密码,也就是说你可以指定调用任何一个人的 tweet.

    3、可以自定义 tweet 信息后显示的文字,就是 $suffix = “”; 这里.

    4、只能调用最新的一条 tweet,刚好满足我的需求。

    5、大概只有国外空间才能使用.(经我验证,确实如此)

    11.wordpress 非插件调用评论表情

    <!–smilies–><?php

    function wp_smilies() {

    global $wpsmiliestrans;

    if ( !get_option(‘use_smilies’) or (empty($wpsmiliestrans))) return;

    $smilies = array_unique($wpsmiliestrans);

    $link=”;

    foreach ($smilies as $key => $smile) {

    $file = get_bloginfo(‘wpurl’).’/wp-includes/images/smilies/’.$smile;

    $value = ” “.$key.” “;

    $img = “<img src=”{$file}” alt=”{$smile}” />”;

    $imglink = htmlspecialchars($img);

    $link .= “<a href=”#commentform” title=”{$smile}” onclick=”document.getElementByIdx_x(‘comment’).value += ‘{$value}’”>{$img}</a> ”;

    }

    echo ‘<div class=”wp_smilies”>’.$link.’</div>’;

    }

    ?><?php wp_smilies();?>

    将以上代码复制到 comments.php 中合适的位置。

    相关文章

      网友评论

          本文标题:WordPress各种标签调用集合

          本文链接:https://www.haomeiwen.com/subject/tvtjlrtx.html