【问题标题】:WordPress Shortcode function - replacing echo with returnWordPress 简码函数 - 用 return 替换 echo
【发布时间】:2016-06-22 14:21:37
【问题描述】:

我正在尝试在我的 WordPress 博客中创建一个带有 Google Plus cmets 的框。我的博客在 2016 年 1 月从 http 迁移到 https,所以如果发布日期在迁移日期之前,我想用不同的永久链接调用评论框。

这是原始的 G+ 评论框:

<div class="g-comments"
    data-href="<?php the_permalink(); ?>"
    data-width="700"
    data-first_party_property="BLOGGER"
    data-view_type="FILTERED_POSTMOD">
</div>

我使用的是 Studiopress Genesis,G+ 代码放置在原始 WP cmets 与 Genesis Simple Hooks 之前的循环中。这就是我写的,它出现在内容之前。我怎样才能从回声移动到返回?有什么宝贵的帮助吗?

<?php
$permalink = get_permalink();
$now = time();
$compare_time = mktime(0, 0, 0, 1, 1, 2016);
$post_time = get_post_time('U');
$url03 = str_replace('https://', 'http://', $permalink );

if ($post_time < $compare_time) {
echo '<div class="g-comments" data-href="';
echo $url03 . '"';
echo ' data-width="700" ';
echo 'data-first_party_property="BLOGGER" ';
echo 'data-view_type="FILTERED_POSTMOD">';
echo '</div> ';
}
else {

echo '<div class="g-comments" data-href="';
echo the_permalink() . '"';
echo ' data-width="700" ';
echo 'data-first_party_property="BLOGGER" ';
echo 'data-view_type="FILTERED_POSTMOD">';
echo '</div> ';
}
?>

【问题讨论】:

  • 那么您将 url 从 http 转换为 https 吗?我认为您的 str_replace 可能是倒退的。
  • 现在博客使用 HTTPS,在 2016 年 1 月 1 日之前使用 HTTP。所以我想在 G+ 评论框中调用正确的永久链接 --> $url3
  • 您为什么要这样做?您在该日期之前发布的内容现在可以通过 https 访问了吗?
  • 我想这样做是因为迁移后博客丢失了所有的 cmets(因为 G+ 在 HTTPS URL 上搜索 cmets)。更改 G+ cmets 调用中的旧帖子 URL,我可以回调所有旧 cmets。该插件有效,但它在任何内容之前执行短代码,这就是问题所在。我红色我应该使用 return 而不是 echo 但我不是专家,我需要一些帮助:)

标签: php wordpress google-plus shortcode genesis


【解决方案1】:

您只需要将 HTML 字符串保存到一个变量并返回该变量。这样的事情应该可以工作。

<?php
function google_comments_html() {
  $permalink = get_permalink();
  $compare_time = mktime(0, 0, 0, 1, 1, 2016);
  $post_time = get_post_time('U');

  if ($post_time < $compare_time) {
    $permalink = str_replace('https://', 'http://', $permalink );
  }

  $html = <<<HTML
    <div class="g-comments"
      data-href="$permalink"
      data-width="700"
      data-first_party_property="BLOGGER"
      data-view_type="FILTERED_POSTMOD">
    </div>
HTML;

  return $html;
}

// Example calling the method
echo google_comments_html();

注意结尾HTML;的左边不能有空格。

【讨论】:

  • 谢谢亚伯拉罕。我需要从 echo 更改为 return,因为在 WordPress 中,echo 在其他所有内容之前打印内容,这是不正确的。您的代码的工作方式与我的工作方式相同。如果我使用您提供的代码创建了一个简码,并且我输入了循环文本 + 简码:“这是插件 [简码]”,那么简码内容会在“这是插件”之前打印。希望我的解释正确。
  • 我不太明白你的评论。除非您将其更改为,否则我的答案中的方法不会回显任何内容。
【解决方案2】:

只需使用 ob_get_contents:http://php.net/manual/en/function.ob-get-contents.php

ob_start();

$permalink = get_permalink();
$now = time();
$compare_time = mktime(0, 0, 0, 1, 1, 2016);
$post_time = get_post_time('U');
$url03 = str_replace('https://', 'http://', $permalink );

if ($post_time < $compare_time) {
echo '<div class="g-comments" data-href="';
echo $url03 . '"';
echo ' data-width="700" ';
echo 'data-first_party_property="BLOGGER" ';
echo 'data-view_type="FILTERED_POSTMOD">';
echo '</div> ';
}
else {

echo '<div class="g-comments" data-href="';
echo the_permalink() . '"';
echo ' data-width="700" ';
echo 'data-first_party_property="BLOGGER" ';
echo 'data-view_type="FILTERED_POSTMOD">';
echo '</div> ';
}

$out = ob_get_contents();

ob_end_clean();

return $out;

【讨论】:

    猜你喜欢
    • 2014-02-27
    • 2014-06-23
    • 1970-01-01
    • 2014-06-23
    • 2012-03-26
    • 2011-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多