4

我为 wordpress 构建了一个非常独特且 javascript 密集型主题,现在短代码不起作用。我没有安装任何插件,所以不是这样。我从使用简码所需的 wordpress 模板文件中删除了什么(即:[图库])。

我了解如何制作简码,但是当 WP 将其吐回以供显示时,它如何获取您的帖子并替换“[gallery]”?

编辑:这是我目前正在使用的:

    $pagepull = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_type = 'page' AND post_status = 'publish' ORDER BY menu_order", ARRAY_A);
    $i = 1;
    foreach ($pagepull as $single_page){
     echo "<div class=\"section\"><ul><li class=\"sub\" id=\"" . $i  . "\"><div class=\"insection\">";
         echo $single_page['post_content'];
$i++;
// more code that is irrelevant...
// more code that is irrelevant...
// more code that is irrelevant...
    }
4

5 回答 5

11

好的,试试这个

 $pagepull = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_type = 'page' AND post_status = 'publish' ORDER BY menu_order", ARRAY_A);
    $i = 1;
    foreach ($pagepull as $single_page){
     echo "<div class=\"section\"><ul><li class=\"sub\" id=\"" . $i  . "\"><div class=\"insection\">";
         echo apply_filters('the_content',$single_page['post_content']);
$i++;

Wordpress 获取您的内容并对其应用过滤器。您必须注册一个过滤器并让解析您的内容。

如果您的主题没有显示您的简码,可能是您在没有让 Wordpress 过滤的情况下输出了帖子的内容。

为帖子调用函数 get_the_content() 不会运行短代码过滤器(如果有)。

有申请

<?php apply_filters('the_content',get_the_content( $more_link_text, $stripteaser, $more_file )) ?>

参考:http ://codex.wordpress.org/Function_Reference/get_the_content

注意:许多插件在内容中注册过滤器以实现简码!

于 2011-01-27T12:11:19.057 回答
1

如果您想要变量内的内容,请使用它:

ob_start();
the_content();
$content = ob_get_clean();

现在你可以做 echo $content; 或使用正则表达式或任何你想让内容看起来像你想要的样子。

于 2013-01-25T12:30:16.103 回答
1

我遇到过同样的问题。

简码取决于 WP Loop,但这是一个不同的问题。长话短说,我the_post();在应该显示短代码的页面上添加了(例如articles.php)。

此外,请确保您正在使用the_content()以显示文本($post->post_data例如,使用不会显示短代码)。

于 2013-10-01T13:18:00.147 回答
0

我的解决方案是更换

<?= get_the_content() ?>

<?= the_content() ?>

正如 keatch 已经提到的,它在返回内容之前应用过滤器。

仔细阅读关于 the_content 的内容

于 2012-06-04T12:41:46.900 回答
0

请使用

ob_start();

在功能和使用的开始

return ob_get_clean();

在关闭函数之前。

希望这对您有所帮助。

干杯

于 2013-09-06T10:29:21.603 回答