国产三级农村妇女在线,国产精品毛片a∨一区二区三区,国产乱子伦视频大全,国产精品色拉拉,国产欧美日韩一区二区三区,

首頁 > 技術(shù) > cms教程

WordPress主題開發(fā)過程常用功能代碼

cms教程 2022-10-26 09:23:31

今天為大家分享幾個(gè)WordPress主題開發(fā)過程常用功能代碼。
WordPress主題開發(fā)過程常用功能代碼 (https://www.wpzt.net/) WordPress開發(fā)教程 第1張
1. 在 wordpress主題中顯示最熱文章的 PHP代碼

get_results("SELECT comment_count,ID,post_title FROM

$wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 10");

foreach ($result as $post) {

setup_postdata($post);

$postid = $post->ID;

$title = $post->post_title;

$commentcount = $post->comment_count;

if ($commentcount != 0) { ?>

  • ()

  • 2. wordpress主題–相關(guān)文章代碼

      $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 = new WP_Query($args);

      if( $my_query->have_posts() ) {

      while ($my_query->have_posts()) : $my_query->the_post(); ?>

    • title="Permanent Link to

      ">

    • realate post';} ?>

      3. WordPress主題使用 PHP代碼輸出最新文章

      $limit = get_option('posts_per_page');

      $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

      query_posts('showposts=' . $limit=7 . '&paged=' . $paged);

      $wp_query->is_archive = true; $wp_query->is_home = false;

      ?>

      ID)) : ?>

      4. 最新評論:

      global $wpdb;

      $sql= "SELECT DISTINCT ID, 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_excerpt FROM $wpdb->comments LEFT OUTER

      JOIN $wpdb->posts ON($wpdb->comments.comment_post_ID = $wpdb->posts.ID)

      WHEREcomment_approved = '1' AND comment_type = " AND post_password = " ORDERBY

      comment_date_gmt DESC LIMIT 10";

      $comments = $wpdb->get_results($sql);

      $output = $pre_HTML;

      foreach ($comments as $comment) {

      $output.= "n

    • ". "ID)."#comment-" .

      $comment->comment_ID ."" title="on

      ".$comment->post_title ."">".strip_tags($comment->comment_author)."" .

      ": ".strip_tags($comment->com_excerpt)."

    • ";

      }

      $output .= $post_HTML;

      echo $output;

      ?>

      5. wordpress主題–相關(guān)文章代碼

      基本sql是這樣:$sql = “SELECT p.ID, p.post_title, p.post_date, p.comment_count,

      count(t_r.object_id) as cnt FROM $wpdb->term_taxonomy t_t, $wpdb->term_relationships

      t_r, $wpdb->posts p WHERE t_t.taxonomy =’post_tag’ AND t_t.term_taxonomy_id =

      t_r.term_taxonomy_id AND t_r.object_id = p.ID AND (t_t.term_id IN ($taglist)) AND

      p.ID != $post->ID AND p.post_status = ‘publish’ GROUP BY t_r.object_id ORDER BY cnt

      DESC, p.post_date_gmt DESC LIMIT $limit;”;

      一點(diǎn)一點(diǎn)的解釋:term_taxonomy 、term_relationship、posts這三張表存的什么我不多說,網(wǎng)上一般都可以查到,維基百科貌似都有。把他們連起來,做個(gè) sql,注意 group by以及l(fā)imit,這樣就可以提取結(jié)果了$related_post = $wpdb->get_results($sql);

      之前要把$taglist 做好,利用wp_get_post_tags($post->ID);可以將該篇文章的的 tag 取到一個(gè)數(shù)組中,然后再鏈接就可以了 最后怎么處理輸出就看個(gè)人愛好了,這個(gè)功能我寫的大概也就十幾行,我比較喜歡簡短的說,呵呵。

      function related_post($limit = 10) {

      global $wpdb, $post;

      $tags = wp_get_post_tags($post->ID);

      $taglist = "'" . $tags[0]->term_id. “‘”;

      $tagcount = count($tags);

      if ($tagcount > 1) {

      for ($i = 1; $i < $tagcount; $i++) {

      $taglist .= “, ‘”.$tags[$i]->term_id.”‘”;

      }

      }

      $sql = “SELECT p.ID, p.post_title, p.post_date, p.comment_count, count(t_r.object_id) as cnt

      FROM $wpdb->term_taxonomy t_t, $wpdb->term_relationships t_r, $wpdb->posts p WHERE

      t_t.taxonomy =’post_tag’ AND t_t.term_taxonomy_id = t_r.term_taxonomy_id AND t_r.object_id =

      p.ID AND (t_t.term_id IN ($taglist)) AND p.ID != $post->ID AND p.post_status = ‘publish’ GROUP

      BY t_r.object_id ORDER BY cnt DESC, p.post_date_gmt DESC LIMIT $limit;”;

      $related_post = $wpdb->get_results($sql);

      $output = “”;

      foreach ($related_post as $tmp){

      $output .= 這個(gè)就看個(gè)人愛好了

      }

      if($output == “”)$output = “No Related Post Yet”;

      echo $output;

      }

    TAg

    加載中~

    本網(wǎng)站LOGO受版權(quán)及商標(biāo)保護(hù),版權(quán)登記號:國作登字-2022-F-10126915,未經(jīng)湖南木星科技官方許可,嚴(yán)禁使用。
    Copyright ? 2012-2022 湖南木星科技有限公司(木星網(wǎng))版權(quán)所有
    轉(zhuǎn)載內(nèi)容版權(quán)歸作者及來源網(wǎng)站所有,本站原創(chuàng)內(nèi)容轉(zhuǎn)載請注明來源,商業(yè)媒體及紙媒請先聯(lián)系:aishangyiwan@126.com

    工信部備案號:湘ICP備19012813號-5