"Son mesajlar" için bu PHP script optimize etmek gerekir.

3 Cevap php

Aşağıdaki kod ~ 1500 mesaj var olduğu bir sitede bir hata neden oluyor. Mesajı sayısı, nominal olduğunda bu ağır yük kod güçsüzlüğü ve bunu optimize etmek isterim, ancak, ince yapar.

Ben bu menüyü devre dışı bırakmak ve bunun yerine "Son Mesajlar" widget kullanmak İlginçtir, mesajlar ince çizilir. Ben bunu bir post sayısı değişken geçirmeden, benim tema doğrudan widget aramak olsaydı Yani muhtemelen, en iyisi ben onu nerede bulacağını biliyordu eğer kod ödünç iyi yapmak, ya da istiyorum.

Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 16384 bytes) in /home1/est/public_html/mysite/wp-includes/post.php on line 3462

Kod aşağıda. Onun amacı, "yeni mesajları" görebilirisiniz.

global $post; 
$cat=get_cat_ID('myMenu'); 
$cathidePost=get_cat_ID('hidePost'); 
$myrecentposts = get_posts(array('post_not_in' => get_option('sticky_posts'), 'cat' => "-$cat,-$cathidePost",'showposts' => $count-of-posts));
$myrecentposts2 = get_posts(array('post_not_in' => get_option('sticky_posts'), 'cat' => "-$cat,-$cathidePost",'showposts' => -1));
$myrecentpostscount = count($myrecentposts2);

if ($myrecentpostscount > 0) 
{ ?>
    <div class="recentPosts"><h4><?php if ($myHeading !=="") { echo $myHeading; } else { echo "Recent Posts";} ?></h4><ul>
    <?php 
    $current_page_recent = get_post( $current_page );
    foreach($myrecentposts as  $idxrecent=>$post) {
        if($post->ID == $current_page_recent->ID)
            {
                $home_menu_recent = ' class="current_page_item';
            } 
            else 
            {
                $home_menu_recent = ' class="page_item';
            }
            $myclassrecent = ($idxrecent == count($myrecentposts) - 1 ? $home_menu_recent.' last"' : $home_menu_recent.'"'); 
        ?>
    <li<?php echo $myclassrecent ?>><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php } ; if (($myrecentpostscount > $count-of-posts) && $count-of-posts > -1){ ?><li><a href="<?php bloginfo('url'); ?>/recent">View All Posts</a></li><?php } ?></ul></div>

3 Cevap

Bu yanlış yoldan almayın. Bu cevap tüm bellek sorunu çözmek için gitmiyorum. Sen çok Html düzen mantığı arasında karışık olması bu gibi uzun 3x almaya gidiyor: Sana aşağıdaki sadece ben gibi yararlı almak umuyoruz bir öneri mem sorunu olabilir inanıyorum nedeni ile başka bir cevap yayınlayacağız neden kesiliyor ve anlamaya yok.

Ben bir örnek vermek ve aslında bunu kendiniz debug noktaya bu kadar temiz nasıl göstermek için gidiyorum üstlenmeden ilkelerine ardından.

Ben biraz refactor kod bölümünü alıyorum:

if ($myrecentpostscount > 0) 
{ ?>
    <div class="recentPosts"><h4><?php if ($myHeading !=="") { echo $myHeading; } else `{ echo "Recent Posts";} ?></h4><ul>`

İlk olarak, şablon ekran aynı zamanda okunabilmesi için (yeniden biçimlendirmek dışarı mantığı çekin:

if ($myrecentpostscount > 0)
{
    if ($myHeading !=="") 
    {
        $displayHeading =  $myHeading; 
    } 
    else 
    { 
        $displayHeading =  "Recent Posts";
    }

?>

İkincisi, fonksiyonu ile geçici değişkenleri değiştirmek [$ myHeading] aramaları

/**
* This function determines if a heading is null, and returns the default if it is.
*/
function getDisplayHeading($customHeading)
{
    if ($customHeading == "") 
    { 
        return "Recent Posts";
    }
    return $customHeading;
}   

if ($myrecentpostscount > 0)
{
    ?>
    <div class="recentPosts"><h4>
    <?php echo getDisplayHeading($myHeading); ?></h4><ul>

Eğer sayfanın üstündeki tüm mantığı varken Şimdi nihayet, bunu nasıl kullandığını kadar bellek görmek için her fonksiyonu profil ve hangi çok fazla bellek sayfası isteği kullanılır etmektedir. Ben bir fikrim var inanıyorum çünkü ben orada olsa, yardım etmek için denemek için başka bir cevap göndermek için gidiyorum.

Senin kodunda, aşağıdakileri şey:

$myrecentposts = get_posts(array('post_not_in' => get_option('sticky_posts'), 'cat' => "-$cat,-$cathidePost",'showposts' => $count-of-posts));
$myrecentposts2 = get_posts(array('post_not_in' => get_option('sticky_posts'), 'cat' => "-$cat,-$cathidePost",'showposts' => -1));
$myrecentpostscount = count($myrecentposts2);

Aslında tüm mesajların getiriliyor, sonra kod onları sayma ... Ben "get_posts" fonksiyonu kaynak yok, ama ben yaklaşık 10 dolar bahse girerim ki "get_post_count" adında yeni bir işlev oluşturmak eğer sayfanın içine tüm mesajlarını almak değil, bir "select count (*)" gibi bir şey yapar ve 1 sonuç verir vermez, senin bellek prob ortadan kaldıracaktır. Eğer get_posts fonksiyonun gövdesini sonrası ise biz muhtemelen get_post_count işlevi yapmak için en kolay yolu belirlemenize yardımcı olabilir.

Eğer bitkin İzin bellek boyutu gördüğünüzde, genellikle ellerinizi sonsuz bir döngü var demektir. Sonsuza döngü onu durdurmak için parametreler vardır emin olmak için kontrol edin.