Html到php var + parsing + Wordpress - > get_the_excerpt()

时间:2017-05-02 06:33:30

标签: php html wordpress

我想这是解析的问题,但几个小时之后就无法解决。我接近解决方案,但坚持并开始让我发疯!

问题:我无法动态获取摘录(说明),因为看起来我的变量 $ my_id 正在返回当我将其推入函数 get_the_excerpt()

时为null
<?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返回,但我的变量返回&#34; 0&#34;在那种情况下。

echo $ my_id 正在返回正确的数字(ID),因此我无法理解为什么当我将其推送到我的网站时它无法正常工作变量 get_the_excerpt($ my_id);

有什么线索吗?

干杯!

编辑1:

我已使用 vardump &amp;更新了我的代码 print_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>

输出:

Output string

当我通过添加 $ my_id = is_int($ my_id)将其解析为整数时,

输出? $ my_id:(int)$ my_id; Output Int

这基本上是我的实际帖子的摘录,而不是动态地使用$ my_id 的ID。

2 个答案:

答案 0 :(得分:0)

我的想法是有额外的空格字符将你的字符串分解为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(); ?>

但我觉得第一个更安全。

答案 1 :(得分:0)

这里值得注意的一点学术事情是:我们总是从ob_get_clean(或ob_get_contents)获取字符串类型值。因为PHP是松散类型的,因此通常不会对这类事情发出噪音,所以它很乐意制作字符串&#39; 1&#39;通过添加0到数字。

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

$my_id = $my_id + 0;

OR

$my_id = (int) $my_id;

或更好

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