HTML标记不在php if语句中显示

HTML标记不在php if语句中显示

问题描述:

I have a WordPress theme with a brief summary, with css styling, at the top of a post. This is wrapped in an if statement to only display if there is a summary.

Using the code below, the summary text displays, but the surrounding HTML markup is not included in the page source.

<?php if (get_smry_text($post)) { ?>
<div class="summaryWrap">
    <div class="sumText">
        <p><?php get_smry_text($post); ?></p>
     </div>
 </div>
 <?php } ?>

Can anyone offer a suggestion as to why this might be?

我有一个WordPress主题,在帖子的顶部有一个带有CSS样式的简短摘要。 这包含在if语句中,仅在有摘要时显示。 p>

使用下面的代码显示摘要文本,但页面源中不包含周围的HTML标记。 p>

 &lt;?php if(get_smry_text($ post)){?&gt; 
&lt; div class =“summaryWrap”&gt; 
&lt; div class =“sumText  “&gt; 
&lt; p&gt;&lt;?php get_smry_text($ post);  ?&gt;&lt; / p&gt; 
&lt; / div&gt; 
&lt; / div&gt; 
&lt;?php}?&gt; 
  code>  pre> 
 
 

可以 有人提出了为什么会这样的建议吗? p> div>

It is possible that get_smry_text() doesn't return any values.
Instead it echoes the content directly. If you are using this function you can do something like this:

<?php if ($smry = get_post_meta($post->ID, 'smry_text', true)) { ?>
<div class="summaryWrap">
    <div class="sumText">
        <p><? echo $smry; ?></p>
     </div>
 </div>
 <?php } ?>

Try doing this:

<?php
   $smry_text = get_smry_text($post);
   if ($smry_text) {
       $text = '<div class="summaryWrap">
               <div class="sumText">
                 <p> ' . $smry_text .  '</p>
               </div>
           </div>';
        echo $text;
   }
?>