【问题标题】:wordpress. show shortcodes in lightbox pluginWordPress的。在灯箱插件中显示简码
【发布时间】:2014-11-11 23:12:13
【问题描述】:

我正在开发一个 WordPress 主题。我使用插件在页面上显示图片(对于每个帖子 1 张图片),每当单击其中一张图片时,以下代码都会注册它并打开一个包含帖子内容的灯箱:

<?php
if($_REQUEST['popup']!=''){

$postObj = get_post( $_REQUEST['pid'] );


echo '<div class="ostContent">'.$postObj->post_content.'</div>';

exit;
?>

这一切都很好。

现在的问题是所有内容都显示得很好。但由于某种原因,短代码不起作用。而且当我在帖子插件中使用小部件在帖子中显示小部件时,它不会显示。

首先我认为我需要启用简码。所以我改变了这个:

echo '<div class="ostContent">'.$postObj->post_content.'</div>';

用这个:

echo '<div class="ostContent">'.do_shortcode( $postObj->post_content ).'</div>';

但还是一无所获。所以现在我不知道要改变什么来让灯箱显示小部件 希望有人知道解决方案!

编辑:当我在灯箱外打开帖子时(只需转到单个页面),短代码就会像应有的那样使用。所以不知何故,上面的代码无法识别短代码或......

【问题讨论】:

  • 不是 100% 确定您在哪里发布此代码,在小部件中,在页面模板中......但也许这会有所帮助? codex.wordpress.org/Function_Reference/add_shortcode
  • 帖子会显示在灯箱中。但不知何故,帖子中的短代码没有更改为小部件/插件

标签: php wordpress shortcode


【解决方案1】:

我想我理解你的问题。

假设有问题的帖子有post_typepost

<?php     

    // Check for existence of 'popup' & 'pid' query vars
    if ( $_REQUEST['popup'] && $_REQUEST['pid'] ) {    

        // Select single post by ID (using value of the 'pid' query var)
        $query = new WP_Query( array ( 'p' => $_REQUEST['pid'] ) );    

        // Check that the query has returned something
        if ($query->have_posts()) {    

            /* Loop through query until we run out of posts
            (should only happen once in this case!) */
            while ($query->have_posts()) {    

                // Setup post, so we can use the_content() and stuff
                the_post();    

                echo '<div class="ostContent">';    

                /* The part we've been waiting for! the_content() will
                display your post content as expected */
                the_content();

                echo '</div>';    

            }    

        }    

    }    

?>

WP_Query 是大多数时候需要检索帖子的方法:http://codex.wordpress.org/Class_Reference/WP_Query

您的代码几乎直接从 WordPress 帖子表中检索和显示数据,使 WordPress 没有机会应用其任何内部操作和过滤器(例如,双换行符的自动段落、短代码执行)。

【讨论】:

  • 嘿,是的,这看起来像是解决方案.. 但它不起作用。我收到一个错误:加载灯箱时出错。我也试过:echo '
    '。 apply_filters('the_content', $postObj->post_content).'
    ';但也没有运气......为什么我会得到这个错误的任何其他想法
  • 我发了一个新帖子,更仔细地定义了我的问题。 stackoverflow.com/questions/25913087/…
  • 刚刚回复了你的新帖子 Merijn。
【解决方案2】:

根据此处的示例:http://codex.wordpress.org/Function_Reference/do_shortcode

看来你需要改一下:echo '&lt;div class="ostContent"&gt;'.do_shortcode( $postObj-&gt;post_content ).'&lt;/div&gt;';

到:

echo '<div class="ostContent">'.do_shortcode([shortcode_in_brackets]).'</div>';

这实际上应该显示代码。我假设您已经在应用短代码的小部件中定义了实际文本。

否则,按照您当前的操作方式,PHP 会在 post_content 甚至有值之前触发。

【讨论】:

  • 嗯我不知道这个,因为短代码在帖子的内容中?所以基本上我现在拥有的代码应该查看内容并查看其中是否有短代码。如果是,它应该触发,
  • 因为我没有 1 个简码。但是页面上的简码可能不同..(最终用户可以自己添加简码)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-27
相关资源
最近更新 更多