子比主题-帖子内部加上历史上的今天功能

20240306085408

新建func.php

在/www/wwwroot/rvich.com/wp-content/themes/zibll目录下新建文件func.php,然后加入以下内容(如果有这个文件的直接第2步):

<?php
//历史上的今天-rvich.com
 function today_in_history() {

  // $title = "历史上的今天"; 
  ?>
  <style>
    .today-in-history {
        display: block;
        position: relative;
        width: 100%;
        height: auto;
        margin: 0 auto;
        border: 0;
        padding: 0;
    }
    
    .today-in-history fieldset {
        display: block;
        width: 100%;
        height: auto;
        margin: 0 auto;
        border: 1px dashed #999;
        padding: 0;
    }
    
    .today-in-history fieldset legend {
        display: block;
        width: auto;
        height: 36px;
        margin: 0 50px;
        border: 0;
        padding: 0;
    }
    
    .today-in-history fieldset legend .today-date {
        float: left;
        display: block;
        width: 36px;
        height: 100%;
        margin: 0 auto;
        border: 0;
        padding: 0;
        line-height: 18px;
        border-radius: 5px;
        overflow: hidden;
    }
    
    .today-in-history fieldset legend .today-date .month {
        display: block;
        width: 100%;
        height: 50%;
        margin: 0;
        border: 0;
        padding: 0;
        font-size: 12px;
        text-align: center;
        color: #fff;
        background: #c10e0e;
    }
    
    .today-in-history fieldset legend .today-date .day {
        display: block;
        width: 100%;
        height: 50%;
        margin: 0;
        border: 0;
        padding: 0;
        text-align: center;
        font-size: 12px;
        font-weight: bold;
        color: #000;
        background: #eee;
    }
    
    .today-in-history fieldset legend h3 {
        display: inline-block;
        width: auto;
        height: 40px;
        margin: 0;
        border: 0;
    padding: 0 10px;
        line-height: 40px;
        font-size: 18px;
        color:#cccc00;
        background: none;
    }
    
    .today-in-history fieldset ul {
        width: auto;
        height: auto;
        margin: 15px 20px;
        border: 0;
        padding: 0;
    }
    
    .today-in-history fieldset ul li label {
        margin: 0 5px;
        border: 0;
        padding: 0;
    }
</style><?php
  $limit = 5;
  global $wpdb;
  $post_year = get_the_time('Y');
  $post_month = get_the_time('m');
  $post_day = get_the_time('j');
  $sql = "select ID, year(post_date_gmt) as h_year, post_title, comment_count FROM 
    $wpdb->posts WHERE post_password = '' AND post_type = 'post' AND post_status = 'publish' 
        AND year(post_date_gmt)!='$post_year' AND month(post_date_gmt)='$post_month' AND day(post_date_gmt)='$post_day' 
        order by post_date_gmt DESC limit $limit";
  $histtory_post = $wpdb->get_results($sql);
  if( $histtory_post ) {
    foreach( $histtory_post as $post ) {
      $h_year = $post->h_year;
      $h_post_title = $post->post_title;
      $h_permalink = get_permalink( $post->ID );
      $h_comments = $post->comment_count;
      $h_post .= "<li><lable>$h_year</lable>:<a href='".$h_permalink."' style='color:blue' title='Permanent Link to ".$h_post_title."'>$h_post_title <span>($h_comments)</span></a></li>";
    }
  }
  $arr_month = array( 1=>"一月",2=>"二月",3=>"三月",4=>"四月",5=>"五月",6=>"六月",7=>"七月",8=>"八月",9=>"九月",10=>"十月",11=>"十一",12=>"十二" );
  foreach($arr_month as $key => $value) {
    if($post_month == $key) {
      $month_l = $value;
    }
  }
  if ( $h_post ) {
    $result = ' 
        <section class="today-in-history">
            <fieldset> 
                <legend> 
                   <div class="today-date">
                        <span class="month">'.$month_l.'</span> 
                        <span class="day">'.$post_day.'</span>
                    </div> <h3>本站历史上的今天</h3> 
                </legend>
                <ul>'.$h_post.'</ul> 
            </fieldset>
        </section>';
  } else {
    $result = '<section class="today-in-history"> 
        <fieldset> 
            <legend>
                 <div class="today-date">
                    <span class="month">'.$month_l.'</span>
                    <span class="day">'.$post_day.'</span>
                 </div> 
                 <h3>本站历史上的今天</h3> 
            </legend>
            <ul>"吼吼~~~,往年的今天站长不知道跑哪里偷懒去了~~~"</ul>
         </fieldset> 
        </section>';
  }
  echo $result;
}
//历史上的今天-rvich.com

已经有func.php

针对已经有func.php文件的,在最后增加以下代码:

//历史上的今天
 function today_in_history() {

  // $title = "历史上的今天"; 
  ?>
  <style>
    .today-in-history {
        display: block;
        position: relative;
        width: 100%;
        height: auto;
        margin: 0 auto;
        border: 0;
        padding: 0;
    }
    
    .today-in-history fieldset {
        display: block;
        width: 100%;
        height: auto;
        margin: 0 auto;
        border: 1px dashed #999;
        padding: 0;
    }
    
    .today-in-history fieldset legend {
        display: block;
        width: auto;
        height: 36px;
        margin: 0 50px;
        border: 0;
        padding: 0;
    }
    
    .today-in-history fieldset legend .today-date {
        float: left;
        display: block;
        width: 36px;
        height: 100%;
        margin: 0 auto;
        border: 0;
        padding: 0;
        line-height: 18px;
        border-radius: 5px;
        overflow: hidden;
    }
    
    .today-in-history fieldset legend .today-date .month {
        display: block;
        width: 100%;
        height: 50%;
        margin: 0;
        border: 0;
        padding: 0;
        font-size: 12px;
        text-align: center;
        color: #fff;
        background: #c10e0e;
    }
    
    .today-in-history fieldset legend .today-date .day {
        display: block;
        width: 100%;
        height: 50%;
        margin: 0;
        border: 0;
        padding: 0;
        text-align: center;
        font-size: 12px;
        font-weight: bold;
        color: #000;
        background: #eee;
    }
    
    .today-in-history fieldset legend h3 {
        display: inline-block;
        width: auto;
        height: 40px;
        margin: 0;
        border: 0;
    padding: 0 10px;
        line-height: 40px;
        font-size: 18px;
        color:#cccc00;
        background: none;
    }
    
    .today-in-history fieldset ul {
        width: auto;
        height: auto;
        margin: 15px 20px;
        border: 0;
        padding: 0;
    }
    
    .today-in-history fieldset ul li label {
        margin: 0 5px;
        border: 0;
        padding: 0;
    }
</style><?php
  $limit = 5;
  global $wpdb;
  $post_year = get_the_time('Y');
  $post_month = get_the_time('m');
  $post_day = get_the_time('j');
  $sql = "select ID, year(post_date_gmt) as h_year, post_title, comment_count FROM 
    $wpdb->posts WHERE post_password = '' AND post_type = 'post' AND post_status = 'publish' 
        AND year(post_date_gmt)!='$post_year' AND month(post_date_gmt)='$post_month' AND day(post_date_gmt)='$post_day' 
        order by post_date_gmt DESC limit $limit";
  $histtory_post = $wpdb->get_results($sql);
  if( $histtory_post ) {
    foreach( $histtory_post as $post ) {
      $h_year = $post->h_year;
      $h_post_title = $post->post_title;
      $h_permalink = get_permalink( $post->ID );
      $h_comments = $post->comment_count;
      $h_post .= "<li><lable>$h_year</lable>:<a href='".$h_permalink."' style='color:blue' title='Permanent Link to ".$h_post_title."'>$h_post_title <span>($h_comments)</span></a></li>";
    }
  }
  $arr_month = array( 1=>"一月",2=>"二月",3=>"三月",4=>"四月",5=>"五月",6=>"六月",7=>"七月",8=>"八月",9=>"九月",10=>"十月",11=>"十一",12=>"十二" );
  foreach($arr_month as $key => $value) {
    if($post_month == $key) {
      $month_l = $value;
    }
  }
  if ( $h_post ) {
    $result = ' 
        <section class="today-in-history">
            <fieldset> 
                <legend> 
                   <div class="today-date">
                        <span class="month">'.$month_l.'</span> 
                        <span class="day">'.$post_day.'</span>
                    </div> <h3>本站历史上的今天</h3> 
                </legend>
                <ul>'.$h_post.'</ul> 
            </fieldset>
        </section>';
  } else {
    $result = '<section class="today-in-history"> 
        <fieldset> 
            <legend>
                 <div class="today-date">
                    <span class="month">'.$month_l.'</span>
                    <span class="day">'.$post_day.'</span>
                 </div> 
                 <h3>本站历史上的今天</h3> 
            </legend>
            <ul>"吼吼~~~,往年的今天站长不知道跑哪里偷懒去了~~~"</ul>
         </fieldset> 
        </section>';
  }
  echo $result;
}
//历史上的今天 single.php插入today_in_history();

调用

在你需要显示的地方插入如下代码:

today_in_history();

例如我上面的图片显示位置,在single.php插入上面代码:如图:

20240306085304

© 版权声明
THE END
如果喜欢,可以【点赞】【分享】【收藏】
点赞10赞赏 分享
评论 抢沙发
头像
非注册用户需审核通过后才能查看。友好交流,勿发纯表情,勿恶意灌水!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容