duanlun2827 2017-05-02 06:33
浏览 62

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

I guess this is a problem of parsing but couldn't figure it out after few hours on it. I am close to the solution but stuck and starting to drive me crazy!

The issue: I can't get my excerpt (description) dynamically as it looks like my variable $my_id is returning null when I am pushing it into the function 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>    

I have already tried to push the id directly like the following:

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

And guess what? It's working.

I also tried to parse $my_id to an (int). My gettype() is returning it as an int but my variable return "0" in that case.

echo $my_id is returning the right numbers (id) so I can't get why it doesn't work when I push it through my variable inside get_the_excerpt($my_id);

Any clue on it?

Cheers!

EDIT 1:

I've updated my code with a vardump & print_r to see the return.

<?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:

Output string

Output when I parse it to an integer by adding $my_id = is_int($my_id) ? $my_id : (int) $my_id; : Output Int

This is basically returning me the excerpt of my actual post and not dynamically with the id of $my_id.

  • 写回答

2条回答 默认 最新

  • dongposhi8677 2017-05-02 06:39
    关注

    My thought is there is extra white space characters breaking your string to int conversion:

    get_the_excerpt( trim( $my_id ) );
    

    or possibly:

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

    or remove the whitespacing between the output buffering functions:

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

    but I feel the first is safer.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。