2011-02-16 59 views
1

我已經在wordpress上多次安裝了WordPress,通過SVN和替換文件夾...數據庫始終保持不變。突然從SVN新副本不能在兩臺不同的機器用下面的代碼工作,從WP調查和測驗工具什麼使WordPress的add_shortcode停止工作?

function wpsqt_main_site_quiz_page($atts) { 

    extract(shortcode_atts(array(
        'name' => false 
    ), $atts)); 

    if (!$name){ 
     require_once WPSQT_DIR.'/pages/general/error.php'; 
    } 

    require_once WPSQT_DIR.'/includes/site/quiz.php'; 
    ob_start(); 
    wpsqt_site_quiz_show($name); 
    $content = ob_get_contents(); 
    ob_end_clean(); 
    return $content; 
} 

add_shortcode('wpsqt_page' , 'wpsqt_main_site_quiz_page');// Deprecated and will be removed 
add_shortcode('wpsqt_quiz' , 'wpsqt_main_site_quiz_page'); 

如果我使用echo看到那裏正在達成的代碼,add_shotcode是正達而裏面的功能是不是和頁面只是顯示這個:

​​

而是與預期quiz.php替換它。

現在我剛剛刪除了數據庫得到了全新安裝的wordpress和插件,當然一切正常。如果我得到的SVN版本並不是全部修改過的(它只有1個插件 - Magic Fields和一個自定義的主題),請刪除插件並重新安裝它,但它仍然不起作用!

這裏可能會出現什麼問題?什麼是使add_shortcode工作所需的一切?

回答

0

自昨天以來,這個問題一直困擾着我。最後找出原因,(現在)顯然在自定義模板上。

頭部包含對query_posts的調用,該調用據說每頁加載只能調用一次。然後有wp_reset_queryrescue。可是等等!似乎這兩個函數都被棄用,都不應該使用!相反,我們應該始終使用WP_query object

所以,這個工作,但它的錯誤

<?php query_posts('showposts=10'); ?> 
<?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
    <li><a href="<?php the_permalink() ?>"><?php the_title() ?></a></li> 
<?php endwhile; endif; ?> 
<?php wp_reset_query(); ?> 

這是正確和恰當的方式

<?php $r = new WP_Query(array('showposts' => '10', 'what_to_show' => 'posts', 'nopaging' => 0, 'post_status' => 'publish', 'caller_get_posts' => 1)); ?> 
<?php if ($r->have_posts()) : while ($r->have_posts()) : $r->the_post(); ?> 
    <li><a href="<?php the_permalink() ?>"><?php the_title() ?></a></li>  
<?php endwhile; endif; ?> 

沒有這個,頁面本身是對後續query_posts沒有正確加載,因此內部的[wpsqt_quiz name="test"](在頁面文章中)永遠不會被調用。

此外,它似乎不能添加到模板頁面[wpsqt_quiz name="test"]

就是這樣。

+1

不建議使用'query_posts'或'wp_reset_query'。 'query_posts'旨在修改哪些帖子在主循環中,創建一個新的'WP_Query'(或者使用'get_posts')是你想要在模板中創建一個_new_獨立循環時應該做的事情。 –

+0

@richard這就是爲什麼我說「看起來」 - 懶得對它做適當深入的研究。感謝您的意見。最終我可能會修正我的答案以反映你的教導! :P – cregox