【问题标题】:wordpress troubleshooting of custom shortcode where no content is displayed没有显示内容的自定义简码的 wordpress 故障排除
【发布时间】:2020-07-08 15:02:37
【问题描述】:

我的functions.php中有这个自定义短代码,我通过wordpress后端使用[landingpage]Some content here[/landingpage]调用它。

add_shortcode('landingpage', function ($atts, $content = NULL, $tag = NULL) {
    $atts = shortcode_atts(['post_id' => 0], $atts, $tag);

    if (empty($atts['post_id']) || empty($post = get_post($atts['post_id']))) {
        return ''; // return nothing, a proper post_id was not passed by the user
    }

    ob_start();
    ?>
    <h2>Title: <?= $post->post_title ?></h2>
    <p><?= $content ?: $post->post_excerpt ?></p>
    <p><a href="<?= get_permalink($post->ID) ?>">Read More</a></p>
    <?php
    return ob_get_clean();
});

我用以下代码制作了一个自定义页面模板

<?php
/**
 * Template Name: Landingpage
 */

get_header(); ?>

<div class="col kb_landing_left">
    <?= do_shortcode('[landingpage post_id="56"]') ?>
</div>
<div class="col-sm-6">
    <?php foreach ([1, 86, 88] as $post_id) : ?>
        <div class="row">
            <div class="col kb_landing_right">
                <?= do_shortcode("[landingpage post_id='{$post_id}']") ?>
            </div>
        </div>
    <?php endforeach; ?>
</div>

<?php
get_footer();

显示标题和“阅读更多”(重定向到正确的帖子)。但内容本身完全缺失。出于调试目的,我添加了一个字符串来查看ID提交是否有问题:

 if (empty($atts['post_id']) || empty($post = get_post($atts['post_id']))) {
        return 'post_id is missing'; // return nothing, a proper post_id was not passed by the user
    }

这也没有显示,因此提交了 ID。

我安装了一些插件,因此将它们全部停用以确保问题与这些无关。什么都没有改变。

一些一般信息;我正在使用 Bootstrap 4 Starter 主题,分别为它制作了一个子主题。其他一切都在正常工作。我不知道短代码不显示帖子内容有什么问题。

非常感谢任何帮助!

这里是 var_dump($post); 的输出

object(WP_Post)#8044 (24) {
  ["ID"]=>
  int(56)
  ["post_author"]=>
  string(1) "1"
  ["post_date"]=>
  string(19) "2020-06-24 22:56:11"
  ["post_date_gmt"]=>
  string(19) "2020-06-24 22:56:11"
  ["post_content"]=>
  string(591) "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet."
  ["post_title"]=>
  string(6) "Test 2"
  ["post_excerpt"]=>
  string(20) "some excerpt content"
  ["post_status"]=>
  string(7) "publish"
  ["comment_status"]=>
  string(4) "open"
  ["ping_status"]=>
  string(4) "open"
  ["post_password"]=>
  string(0) ""
  ["post_name"]=>
  string(4) "test"
  ["to_ping"]=>
  string(0) ""
  ["pinged"]=>
  string(0) ""
  ["post_modified"]=>
  string(19) "2020-07-07 21:34:30"
  ["post_modified_gmt"]=>
  string(19) "2020-07-07 21:34:30"
  ["post_content_filtered"]=>
  string(0) ""
  ["post_parent"]=>
  int(0)
  ["guid"]=>
  string(31) "https://wiki.fiveseven.ch/?p=56"
  ["menu_order"]=>
  int(0)
  ["post_type"]=>
  string(4) "post"
  ["post_mime_type"]=>
  string(0) ""
  ["comment_count"]=>
  string(1) "0"
  ["filter"]=>
  string(3) "raw"
}

【问题讨论】:

  • 肯定在您的示例中$content 将是NULL,因为您对短代码的调用没有像[landingpage post_id="56"]some content[/landingpage] 那样传递给它的内容。我怀疑您的 $post-&gt;post_excerpt 也是一个空字符串。如果是这种情况,那么您的代码将按预期工作,但您可能希望输出其他内容,例如 get_the_content(null, false, $post) 等。但首先,为了调试我的假设,请在&lt;p&gt;&lt;?= $content ?: $post-&gt;post_excerpt ?&gt;&lt;/p&gt; 上方添加&lt;?php var_dump($post); ?&gt;,并在问题的末尾发布它的输出。
  • @Phil,感谢您回复我。我刚刚意识到,您的意思是后端的专用摘录框...如果我在其中输入一些文本,它就会显示出来。但是,仍然缺少实际内容。我按照您的建议添加了 var_dump 功能,输出在我的问题末尾。好吧,那里的内容是可见的(和摘录),但如果我再次删除 var_dump 功能,只有摘录仍然显示。

标签: wordpress


【解决方案1】:

根据您更新的注释和 var_dump 结果,在我看来,一切都在/正在按应有的方式工作。如您所见,您只需要在管理员中添加摘录文本。但听起来你想要显示不同的结果。根据我正在阅读的内容,您并不真正想要传递给简码的$content(在您的特定场景中始终是一个空字符串),而是您想要实际帖子的内容。所以这里有几个选项供您选择和测试,看看哪些适合您。

如果您想显示完整的内容(无论多长)。使用:

<div><?= get_the_content(null, false, $post) ?></div>

如果您想显示摘录(如果摘录恰好为空,则回退到带有省略号的截断帖子内容)。使用:

<p><?= get_the_excerpt($post) ?></p>

如果您喜欢截断帖子内容的想法,例如 get_the_excerpt($post),但您绝对不希望出现摘录,即使它存在。使用:

<p><?= wp_trim_excerpt('', $post) ?></p>
// NOTE: the first agrument MUST be a an empty string ''

如果你想坚持用户通过短代码标签传递的内容(你可能不想要这个,但我在这里为其他人添加它)。使用:

<p><?= $content ?></p>

但是,如果您确实需要此功能,则需要将其传递到您的着陆页模板中。像这样的东西:[landingpage post_id="56"]some content[/landingpage][landingpage post_id='{$post_id}']some content[/landingpage]

您还可以使用缩短三元运算?: 回退到其中任何一个,就像您原来的问题一样。

尝试这些选项,看看哪一个适合您的需求。请记住(除了我的最后一个示例)如果您仍然看到内容的空字符串,然后再次&lt;?php var_dump($post); ?&gt; 以确保该帖子实际上具有要显示的内容。对于最后一个示例,您需要添加 &lt;?php var_dump($content); ?&gt; 以查看 $content 是否在 do_shortcode() 调用中传递。希望对您有所帮助!

【讨论】:

  • 太棒了,现在一切对我来说都清楚多了。我将使用&lt;?= get_the_content(null, false, $post) ?&gt;,这是最好的解决方案。再次感谢您的耐心和大力支持!
【解决方案2】:

听起来你的问题出在这一行:

<p><?= $content ?: $post->post_excerpt ?></p>

由于标题和自述文件链接有效,您应该关注这一点。

您正在回显一个名为 $content 的变量,但该变量未在函数中的任何位置定义。所以这是空的。你应该先做一个定义,或者直接写:

<p><?= $post->post_content ?: $post->post_excerpt ?></p>

如果我得到了正确的方法,这(在我看来)是一个不太可读的 if/else,您想在其中检查帖子内容是否为空。如果它是空的,你想显示帖子摘录。我不确定您是否以正确的方式使用语法...这是我发现的:

$action = (empty($_POST['action'])) ? 'standard' : $_POST['action'];

所以有一个条件。如果为空,则使用“标准”,如果不为空,则使用“$_POST['action']”。

因此,您的代码应如下所示:

(empty($post->post_content)) ? $post->post_excerpt : $post->post_content;

我建议让它更具可读性,尤其是出于调试原因:

<h2>Title: <?= $post->post_title ?></h2>
<p>
<?php if(empty($post->post_content)) {
             echo $post->excerpt;
         } else {
             echo $post->post_content;
         } ?>
</p>
<p><a href="<?= get_permalink($post->ID) ?>">Read More</a></p>

【讨论】:

  • 非常感谢您的帮助。当我通过我的自定义landingpage.php 页面模板调用简码时,此代码部分工作。但只显示整个内容,没有摘录。当然,我在后端的文本中间添加了阅读更多内容。通过 Wordpress 后端使用简码根本不起作用。它显示 [landingpage] 短代码括号以及文本内容。
【解决方案3】:

尝试改变三进制如果

<p><?= get_the_excerpt($post) ?></p>

【讨论】:

  • 感谢您的建议,但遗憾的是没有解决问题
  • 我希望我发现了问题。我更新了我的答案。请检查
  • 感谢您的帮助。代码运行良好,但我正在尝试解决上述问题,谢谢!
猜你喜欢
  • 2017-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多