什么可能使 WordPress add_shortcode 停止工作?

发布于 2024-10-17 21:06:50 字数 1063 浏览 1 评论 0原文

我已经在 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 工作需要什么?

I've installed WordPress on top of wordpress many times, through SVN and replacing folders... The database remained always the same. Suddenly a fresh copy from SVN can't work in two different machines with the following code, from wp survey and quiz tool:

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' );

If I use echo to see where the code is being reached, add_shotcode is being reached while inside the function is not and the page just displays this:

[wpsqt_quiz name="test"]

Instead of replacing it with the expected quiz.php.

Now I just deleted the database got a fresh install of wordpress and the plugin, and of course everything worked fine. If I get the SVN version, which isn't all that modified (it just got 1 plugin - Magic Fields - and a customized theme), remove the plugin and install it again, it still doesn't work!

What could be going wrong here? What is everything needed to make add_shortcode work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初懵 2024-10-24 21:06:50

这个问题从昨天起就一直困扰着我。终于找到原因了,(现在)显然是在定制模板上。

标头包含对 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"] 无法添加到模板页面。

就这样。

That problem had been bugging me since yesterday. Finally found out the reason, (now) obviously on the customized template.

The header included a call to query_posts, which supposedly can only be called once per page load. Then there comes wp_reset_query to rescue. But wait! Seems like both of those functions are deprecated and neither should be used! Instead, we should always use WP_query object.

So, this works but it's wrong:

<?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(); ?>  

and this is the right and proper way:

<?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; ?> 

Without that, the subsequent query_posts on the page itself are not properly loaded and thus the [wpsqt_quiz name="test"] inside them (in the page post) is never called.

Also, it seems like [wpsqt_quiz name="test"] can't be added to the template page.

That's all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文