2

我已经在 wordpress 上安装了 WordPress 很多次,通过 SVN 和替换文件夹......数据库始终保持不变。突然,来自 SVN 的新副本无法使用以下代码在两台不同的机器上工作,来自wp 调查和测验工具

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 时,页面仅显示以下内容:

[wpsqt_quiz name="test"]

而不是用预期的quiz.php.

现在我刚刚删除了数据库,重新安装了 wordpress 和插件,当然一切正常。如果我得到了 SVN 版本,它并没有完全修改(它只有 1 个插件 - Magic Fields - 和一个自定义主题),删除插件并重新安装它,它仍然不起作用!

这里可能出了什么问题?使 add_shortcode 工作所需的一切是什么?

4

1 回答 1

1

从昨天开始,这个问题就一直困扰着我。终于找到原因了,(现在)明显在自定义模板上。

标头包括对 的调用query_posts,据说每个页面加载只能调用一次。然后wp_reset_query救援。可是等等!似乎这两个功能都已弃用,也不应该使用!相反,我们应该始终使用WP_query 对象

所以,这行得通,但它是错误的:

<?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"]无法添加到模板页面。

就这样。

于 2011-02-16T23:09:54.393 回答