wordpress几个我最近用到的函数

1、获取分类并输出

<?php      // 获取父级分类       $terms = get_terms('category', 'orderby=name&hide_empty=0&parent=0' );      // 获取到的分类数量       $count = count($terms);      if($count > 0){      // 循环输出所有分类信息           foreach ($terms as $term) {          ?>          <strong><span style="background-color:#000000">          <?php          echo '<a href="'.get_term_link($term, $term->slug).'" title="'.$term->name.'" style="color:#FFFFFF">'.$term->name.'</a>  ';          ?>          </span></strong>          <?php       }      }?>

2、最活跃用户(读者墙)

<?php//读者墙functionwall($date=MONTH,$limit=15){    global $wpdb;        //print_r($wpdb);    $counts = $wpdb->get_results("SELECT COUNT(comment_author) AS cnt, comment_author, comment_author_url, comment_author_email FROM (SELECT * FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->posts.ID=$wpdb->comments.comment_post_ID) WHERE comment_date > date_sub( NOW(), INTERVAL 1 $date ) AND user_id='0' AND comment_author != 'climber' AND post_password='' AND comment_approved='1' AND comment_type='') AS tempcmt GROUP BY comment_author ORDER BY cnt DESC LIMIT $limit");    foreach ($counts as $row) {      $c_url = $row->comment_author_url;      if ($c_url == '') $c_url = 'http://www.dutycode.com/';      $mostactive .= '<li title="' . $row->comment_author . ' ('. $row->cnt . ' 条评论)"><a href="'.$c_url.'" target="_blank">' .get_avatar($row->comment_author_email,32).'</a></li>';    }    echo $mostactive;   }?>

3、获取文章中第一张图片

<?phpfunctioncatch_first_image() {global$post,$posts;$first_img='';ob_start();ob_end_clean();$output=preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);$first_img=$matches[1][0];if(empty($first_img)){ //Defines a default image       $random = mt_rand(1, 10);       echo get_bloginfo ( 'stylesheet_directory' );       echo '/images/random/'.$random.'.jpg';}return$first_img;}?>


4、显示热评文章

<!--显示热评文章-->    <?php global $wpdb;    $hotposts = $wpdb->get_results("    SELECT ID, post_title, guid, comment_count    FROM $wpdb->posts    WHERE post_type = 'post'    AND post_status = 'publish'    ORDER BY comment_count DESC     LIMIT 5 ");//数据库查询    foreach($hotposts as $post):?><?php?>        <?php $category = get_the_category();// print_r($category);?><a href="<?php bloginfo('url');echo "/?cat=".$category[0]->cat_ID?>"  style="color: #66CC00"><font style="color:#66CC00">【<?php echo $category[0]->cat_name; ?>】</font></a><a href="<?php the_permalink($post->ID);?>"><?php the_title();?></a>        (<?php comments_popup_link('0', '1', '%'); ?>)        <br/>        -------------------------------------------------    <?php endforeach;?><!--热评文章显示结束-->


5、无需刷新页面往数据库中添加数据

index.php

<divid="result"></div><formname="fourm">电影名称:<input type="text" id="movie_name" name="movie_name"/>导演:<input type="text" id="movie_director" name="movie_director"/><br/>国家/地区:<input type="text" id="movie_country" name="movie_country"/>类型:<input type="text" id="movie_kind" name="movie_kind"/>上映时间:<input type="text" id="movie_showtime" name="movie_showtime"/>日期格式如下:2012-01-01<br/>下载地址:<input type="text" id="movie_downurl" name="movie_downurl" size="50"/>不要加上http://直接输入网址即可,如www.dutycode.com<br/><inputtype="hidden"name="post_id"id="post_id"value="<?php echo $post_ID?>"/><inputtype="button"name="Submit"value="添加影片信息"onClick=checkfourm("result");/><inputname="reset"type="reset"id="reset"value="重新填写"/></form>    <script language="javascript">    var http_request=false;    function send_request(url){//初始化,指定处理函数,发送请求的函数    http_request=false;    //开始初始化XMLHttpRequest对象    if(window.XMLHttpRequest){//Mozilla浏览器    http_request=new XMLHttpRequest();    if(http_request.overrideMimeType){//设置MIME类别        http_request.overrideMimeType("text/xml");    }    }    else if(window.ActiveXObject){//IE浏览器    try{       http_request=new ActiveXObject("Msxml2.XMLHttp");    }catch(e){       try{       http_request=new ActiveXobject("Microsoft.XMLHttp");       }catch(e){}    }        }    if(!http_request){//异常,创建对象实例失败    window.alert("创建XMLHttp对象失败!");    return false;    }    http_request.onreadystatechange=processrequest;    //确定发送请求方式,URL,及是否同步执行下段代码        http_request.open("GET",url,true);    http_request.send(null);    }    //处理返回信息的函数       function processrequest(){       if(http_request.readyState==4){//判断对象状态         if(http_request.status==200){//信息已成功返回,开始处理信息       document.getElementById(reobj).innerHTML=http_request.responseText;    }    else{//页面不正常       alert("您所请求的页面不正常!");    }       }    }       function checkfourm(obj){        var f=document.fourm;        var movie_name=f.movie_name.value;        var movie_director=f.movie_director.value;        var movie_country=f.movie_country.value;        var movie_kind=f.movie_kind.value;        var movie_showtime=f.movie_showtime.value;        var post_id=f.post_id.value;         document.getElementById(obj).innerHTML="添加成功";         send_request("movie.php?movie_name="+movie_name+"&movie_director="+movie_director+"&movie_country="+movie_country+"&movie_kind="+movie_kind+"&movie_showtime="+movie_showtime+"&post_id="+post_id);         reobj=obj;       }</script>

movie.php

<?phpheader("Content-Type:text/html;charset=utf8");//避免输出中文乱码,linux下不需要$movie_name=trim($_GET["movie_name"]);$movie_director=trim($_GET["movie_director"]);$movie_country=trim($_GET["movie_country"]);$movie_kind=trim($_GET["movie_kind"]);$movie_showtime=trim($_GET["movie_showtime"]);$post_id=trim($_GET["post_id"]);$post_moviedate=date("Y-m-d H:i:s");$movie_downurl=trim($_GET["movie_downurl"]); //echo $movie_country;$con=mysql_connect('localhost', 'root', '');if(!$con){die('Could not connect: ' . mysql_error());}//mysql_query("SET NAMES gb2312");mysql_select_db("wordpress", $con);//对当前电影进行判断,如果存在则进行修改,如果不存在则新建一个电影$checkmovie="SELECT count(ID) FROM `wp_movieinfo` WHERE `post_id` =".$post_id;$checkres=mysql_query($checkmovie);$res=mysql_result($checkres,0);if($res > 0){    //执行update操作,更新数据库中信息    $updatemovie = "UPDATE `wp_movieinfo` SET `movie_name`='$movie_name',`movie_director`='$movie_director',`movie_country`='$movie_country',`movie_kind`='$movie_kind',`movie_showtime`='$movie_showtime',`movie_publishtime`='$post_moviedate',`movie_downurl`='$movie_downurl' where `post_id`='$post_id'";    //echo $updatemovie;    $result = mysql_query($updatemovie);    echo "电影信息更新成功";}else{    $sql = "INSERT INTO `wp_movieinfo`( `post_id`, `movie_name`, `movie_director`, `movie_country`, `movie_kind`, `movie_showtime`, `movie_publishtime`, `movie_downurl`) VALUES ('$post_id','$movie_name','$movie_director','$movie_country','$movie_kind','$movie_showtime','$post_moviedate',$movie_downurl)";    //echo $sql;    $result = mysql_query($sql);}echo"<font color=red>电影信息已经成功添加</font>";?>


你可能感兴趣的:(wordpress几个我最近用到的函数)