【问题标题】:Html to php var + parsing + Wordpress -> get_the_excerpt()Html to php var + parsing + Wordpress -> get_the_excerpt()
【发布时间】:2017-05-02 06:33:29
【问题描述】:

我猜这是解析的问题,但经过几个小时后无法弄清楚。我接近解决方案,但卡住并开始让我发疯!

问题:我无法动态获取摘录(描述),因为看起来我的变量 $my_id 在以下情况下返回 null我将它推入函数 get_the_excerpt()

<?php ob_start(); ?>
    {{ data[ index ].option_id }}
<?php $my_id = ob_get_clean(); ?>

<?php $product_description = get_the_excerpt($my_id); ?>
<span class="radio_button_desc"><?php echo apply_filters( 'woocommerce_composited_product_excerpt', wpautop( do_shortcode( wp_kses_post( $product_description ) ) ), $product_id, $component_id, $composite ); ?></span>    

我已经尝试过像下面这样直接推送 id:

<?php $product_description = get_the_excerpt(123456); ?> 

你猜怎么着?它正在工作。

我还尝试将 解析 $my_id(int)。我的 gettype() 将它作为 int 返回,但在这种情况下我的变量返回“0”。

echo $my_id 正在返回正确的数字 (id),所以当我将它推入 get_the_excerpt($my_id);

有什么线索吗?

干杯!

编辑 1:

我已使用 vardumpprint_r 更新了我的代码以查看返回值。

<?php ob_start(); ?>
    {{ data[ index ].option_id }}
<?php $my_id = ob_get_clean(); ?>
<?php var_dump($my_id); ?>
<?php $product_description = get_the_excerpt( $my_id ); ?>
<?php print_r(get_the_excerpt( $my_id )); ?> <!-- return nothing */ -->
<span class="radio_button_desc"><?php echo apply_filters( 'woocommerce_composited_product_excerpt', wpautop( do_shortcode( wp_kses_post( $product_description ) ) ), $product_id, $component_id, $composite ); ?></span>

输出:

输出 当我通过添加 $my_id = is_int($my_id) 将其解析为 integer 时? $my_id : (int) $my_id; :

这基本上是返回我实际帖子的摘录,不是动态地使用 $my_id 的 id

【问题讨论】:

    标签: php html wordpress


    【解决方案1】:

    这里有一点值得注意:我们总是从 ob_get_clean(或 ob_get_contents)中得到一个字符串类型的值。因为 PHP 的类型很松散,因此通常不会对这类事情产生任何影响,它会很高兴地通过向字符串 '1' 加 0 来将它变成一个数字。

    您可以执行以下任何操作。

    $my_id = $my_id + 0;
    

    $my_id = (int) $my_id;
    

    或者更好

    $my_id = is_int($my_id) ? $my_id : (int) $my_id;
    

    【讨论】:

    • 我已经尝试了前两种解决方案,但没有成功。每当我解析它时与上面相同,它给了我(不是从 id 动态地)我的产品的摘录。因此,当我解析为“int”时,echo $my_id 返回我 0,所以我猜是因为 $my_id 是空白的,所以它正在调用“get_the_excerpt()”...而不是 get_the_excerpt($my_id);
    • 写,ob_start(); the_ID(); $id = ob_get_clean();
    • 不作为 the_ID() 工作:正在返回实际帖子的 ID。我已经更新/编辑了我的问题并添加了一些输出截图。
    【解决方案2】:

    我的想法是有额外的空白字符会破坏您的字符串到 int 的转换:

    get_the_excerpt( trim( $my_id ) );
    

    或者可能:

    get_the_excerpt( (int) trim( $my_id ) );
    

    或删除输出缓冲函数之间的空白:

    <?php ob_start(); ?>{{ data[ index ].option_id }}<?php $my_id = ob_get_clean(); ?>
    

    但我觉得第一个更安全。

    【讨论】:

    • 忘了提到我已经尝试过了,每当我删除这些额外的空白时,它都会给我(不是从 id 动态地)我产品的摘录。所以我想当我删除这些空格时,返回我的 $my_id 空白所以 "get_the_excerpt()"... 而不是 get_the_excerpt($my_id);
    • 我已经更新/编辑了我的问题并添加了一些输出截图。
    • 您的屏幕截图 var_dump 显示了一个由 45 个字符组成的字符串,表示 string(45) " 1904463 " 大量空白。比较 var_dump($my_id);var_dump(trim($my_id)); 查看您的浏览器源代码以确认空格。
    • 好的,我做到了。现在的输出是 string(29) ,它与:{{ data[ index ].option_id }} -> 29 char 相关,所以我猜错误就在这里,看起来它没有从{{ data[ index ].option_id }} 返回。我不明白为什么当我 echo / print_r / var_dump 我的号码被退回时。
    • 有可能您看到的内容实际上是在您的代码被另一个过滤器执行之后才被处理的?
    猜你喜欢
    • 2014-09-27
    • 2011-03-27
    • 1970-01-01
    • 2016-02-12
    • 1970-01-01
    • 2011-01-29
    • 2010-12-17
    • 1970-01-01
    • 2022-12-01
    相关资源
    最近更新 更多