为什么get_category_by_slug()崩溃了WordPress 4.8

时间:2017-08-06 00:18:04

标签: php wordpress

我需要隐藏"未分类"从可选择的帖子类别列表中,写了一些代码来实现这一点。 此功能正常直到WP 4.7.5,类别已成功隐藏:

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8"> 
<title>Chapter 9-1 Midwest Bridal Expo</title>
<script type="text/javascript">
<!--
function countDown() {
    var today = new Date()
    var dayOfWeek = today.toLocaleString()
    dayLocate = dayOfWeek.indexOf(" ")
    weekDay = dayOfWeek.substring(0, dayLocate)
    newDay = dayOfWeek.substring(dayLocate)
    dateLocate = newDay.indexOf(",")
    monthDate = newDay.substring(0, dateLocate+1)
    yearLocate = dayOfWeek.indexOf("2017")
    year = dayOfWeek.substr(yearLocate, 4)

    var bridalExpo = new Date("February 12, 2018")
    var daysToGo = bridalExpo.getTime()-today.getTime()
    var daysToBridalExpo = Math.ceil(daysToGo/(1000*60*60*24))

    displayCountDown.innerHTML = "<p style='font-size: 12pt; font-family:helvetica;'>Today is "+weekDay+" "+monthDay+" "+year+". We have "+daysToBridalExpo+" days until the Midwest Bridal Expo.</p>"
}

function scrollColor() {
    styleObject=document.getElementsByTagName('html')[0].style
    styleObject=scrollbarFaceColor="#ffde5b"
    styleObject=scrollbarTrackColor="#ffba00"
}

function loadInfo(myForm) {
    var menuSelect=myForm.Menu.selectedIndex
    var menuUrl=myForm.Menu.options[menuSelect].value+".html"
    window.location=menuUrl
}

function copyright() {
    var lastModDate = document.lastModified
    lastModDate = lastModDate.substring(0,10)
    displayCopyright.innerHTML = "<p style='font-size: 12pt; font-family:helvetica;'>Today is "+weekDay+" "+monthDay+" "+year+". We have "+daysToBridalExpo+" days until the Midwest Bridal Expo.</p>"
}

//-->
</script>
<style type="text/css">
    .center {
    text-align:center;
    }

img {
    border:0px;
}

.left-align {
    width: 50%;
    left: 0;
}

.right-align {
    width: 50%;
    right: 0;
    text-align: right; 
} 

table {
    width: 80%;
    margin-right: 10%;
    margin-left: 10%;
}

td {
    padding: 5px;
}

</style>
</head>
<body onLoad="scrollColor(); countDown(); copyright()">
<div class="center">
<p><img src="chapter9-1banner.jpg" width="747" height="160" alt="Bridal Expo Banner"></p>
<p style="font-family:Arial, Helvetica, sans-serif; font-size:18px; font-weight:bold;">Midwest Bridal Expo</p>
<img src="hrimg-wedding-burgundy.jpg" width="750" height="5" alt="hr">
<div id="displayCountDown">
</div>
<img src="hrimg-wedding-burgundy.jpg" width="750" height="5" alt="horizonal rule">
</div>
<table class="centerTable">
  <tr>
    <td colspan="2">
    <p style="font-weight:bold; font-family:Arial, sans-serif; font-size:14pt">Planning your summer or fall wedding</p>
    <p style="font-family: 'Times New Roman', Times, serif; font-size:12pt">The Midwest Bridal Expo opens in Michigan on February 12, 2014 at the Ford Convention Center. This Expo is for all couples planning to get married in the next year, with an emphasis on those planning a summer or fall wedding. We have assembled a group of experts from all areas of wedding planning to help you plan your perfect day.</p>
    </td>
  </tr>
  <tr>
    <td colspan="2">
    <p style="font-weight:bold; font-family:Arial, sans-serif; font-size:14pt">What awaits you</p>
<p style="font-family: 'Times New Roman', Times, serif; font-size:12pt">Our full day of events include seminars with keynote speakers, one-on-one sessions with professional wedding planners, and various vendors from bakers to photographers with experience at all levels of wedding planning. You will be able to sample wedding cakes,  see photo galleries by acclaimed photographers, and meet with hall rental representatives. And do not forget to discuss whether you want a live band or a DJ to provide music at that all-important reception.</p>
<p style="font-family: 'Times New Roman', Times, serif; font-size:12pt">When you arrive, be sure to register for the grand prize drawing. Many of the individual vendors will also have raffles at their booths for special prizes, like free wedding cakes, free limo service, or free bouquets for the bride.</p></td>
  </tr>
  <tr>
    <td class="right-align"><img src="393px-Wedding_cake_with_pillar_supports,_2009.jpg" width="197" height="300" alt="" style="background-color: #7f1100"></td>
    <td class="left-align"><img src="Western_wedding_dress_in_Taiwan.jpg" width="226" height="300" alt="" style="background-color: #7f1100"></td>
  </tr>
  <tr>
    <td colspan="2">
    <form id="announceMenu">
      <p style="font-weight:bolder">
       For more information about the Expo:
         <select name="Menu" onChange="loadInfo(this.form)">
           <option>Select a topic</option>
           <option value="chapter09vendor">Vendor List</option>
           <option value="chapter09schedule">Schedule</option>
           <option value="chapter09tips">Wedding Tips</option>
         </select>
      </p>
    </form>
    </td>
 <tr>
    <td colspan="2"><p style="font-weight:bold; font-family:Arial, sans-serif; font-size:14pt">Ticket Information: <span style="font-family: 'Times New Roman', Times, serif; font-size: 10pt">For ticket reservations </span><span style="font-family: 'Times New Roman', Times, serif; font-size:8pt">call 555-555-5555 x2205. Early bird ticket holders get a 10% discount..</span></p>    
</td>
  </tr>
</table>
<p></p>
<div id="displayCopyright">

</div>
</body>
</html>

但是,在更高版本(4.8.1是迄今为止的最新版本)中,每当我尝试编辑帖子或创建新帖子时(访问`localhost / wp-admin / post-new.php&#34;),我收到以下错误消息:

  

add_filter('list_terms_exclusions', 'myproject_hide_uncategorized',1); function myproject_hide_uncategorized( $exclude_query ) { if (function_exists('get_current_screen')) { $currentScreen = get_current_screen(); if ($currentScreen->base == 'post') { $newquery = ""; $excluidos = array(); $excluidos[] = get_category_by_slug('sem-categoria')->cat_ID; $excluidos[] = get_category_by_slug('uncategorized')->cat_ID; foreach ($excluidos as $excluido) { $newquery .= " AND t.term_id <> '$excluido' "; } $exclude_query .= $newquery; } } return $exclude_query; }

提高内存限制只会导致超时错误。我能够在WP 4.8和4.8.1的干净安装中复制行为

看起来崩溃是由这四行引起的,将其添加到任何主题的Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 130968 bytes) in /var/www/html/wp-includes/class-wp-term-query.php on line 294文件似乎都会导致崩溃:

functions.php

导致此错误的4.7.5之间发生了什么变化?我怎么能让它消失?我必须重写我的代码吗?如果是这样,如何

1 个答案:

答案 0 :(得分:-1)

尝试使用get_term_by(); https://codex.wordpress.org/Function_Reference/get_term_by

像这样

get_term_by('slug', 'uncategorized', 'category');