首页 > 解决方案 > 如何在 html 标签中正确包装 php 变量?

问题描述

我不是真正的 php 专家,我想做的是将 $duration 变量包装在 span 标签内的最后一行,这样我就可以设置它的样式。

  <?php
$duration = '';
if( function_exists( 'cbc_get_video_data' ) ){
    global $post;
    $video = cbc_get_video_data( $post );
    if( $video ) {
        $duration = $video->get_human_duration();
    }
}
$title_content .= '<div class="post-title-wrapper"><h1 class="post-title">' . $duration . get_the_title() . '</h1>';
?>

当我做这样的事情时,网站会中断

  <?php
    $duration = '';
    if( function_exists( 'cbc_get_video_data' ) ){
        global $post;
        $video = cbc_get_video_data( $post );
        if( $video ) {
            $duration = $video->get_human_duration();
        }
    }
    $title_content .= '<div class="post-title-wrapper"><h1 class="post-title">' . '<span class="video-duration">'$duration'</span>' . get_the_title() . '</h1>';
?>

有什么建议么?

谢谢

标签: php

解决方案


在您的第二个示例中,您.缺少$duration. 您也没有关闭 a<div>并且不需要打开跨度之间的额外连接。

要优化此代码,请尝试以下操作: $title_content .= '<div class="post-title-wrapper"><h1 class="post-title"><span class="video-duration">' . $duration . '</span>' . get_the_title() . '</h1></div>';


推荐阅读