2011-11-06 140 views
0

我基本上想显示一些php,如果它是在类别工作内部,我知道如何做if else语句和输出PHP自己,但不是很确定如何结合二,如果有人能启发我,将不胜感激。基本代码如下,我猜我不允许有一个<?php inside a <?php?PHP里面的PHP if else语句 - wordpress

非常感谢!

<?php // Outputting related work if in work category 
    if (in_category('work')) { 
    echo " 

    <section class='work'> 

     <h2>Also of interest</h2> 

     <?php query_posts('category_name=work&posts_per_page=3&order=DSC&offset=1&orderby=ID'); 
     if (have_posts()) : while (have_posts()) : the_post(); ?> 

     <article> 
     <a href='<?php the_permalink() ?>'> 
      <?php the_post_thumbnail();?> 
      <h3><?php the_title(); ?></h3> 
      <span>Redesign</span> 
      <?php the_excerpt($strip_teaser); ?> 
     </a> 
     </article> 

     <?php endwhile; endif; wp_reset_query();?> 

     <a class='all' href='/work'>&rarr; View all work</a> 

    </section> <!-- end work --> 
"; 
} 
?> 

回答

1

PHP是一种模板语言,用它作为这样的:

<?php // Outputting related work if in work category 
    if (in_category('work')) { 
?> 

    <section class='work'> 

     <h2>Also of interest</h2> 

     <?php query_posts('category_name=work&posts_per_page=3&order=DSC&offset=1&orderby=ID'); 
     if (have_posts()) : while (have_posts()) : the_post(); ?> 

     <article> 
     <a href='<?php the_permalink() ?>'> 
      <?php the_post_thumbnail();?> 
      <h3><?php the_title(); ?></h3> 
      <span>Redesign</span> 
      <?php the_excerpt($strip_teaser); ?> 
     </a> 
     </article> 

     <?php endwhile; endif; wp_reset_query();?> 

     <a class='all' href='/work'>&rarr; View all work</a> 

    </section> <!-- end work --> 
<?php 
} 
?> 

不包含在<?php ?>标记中的代码原样输出到浏览器。每当网络服务器发现一个<?php开始标记时,它就会向PHP解释器提供控制,然后执行包含在该标记中的代码。

+0

谢谢,这使事情变得更容易! – Nick

0

你把php标记在echo语句,如果你想要把一个while循环在一些文字,我会退出德回声";,使像这样while(hava_posts()) {在while循环之间在这里您回应你的文章像echo "<article></article>";那么你用支架}关闭你的同时,并打电话给你的wp_reset_query();函数。

+0

谢谢,现在感觉很蠢,本来应该注意到我已经把整个事情都回声了,谢谢你的帮助:) – Nick

+0

没问题,只是希望你从中学到:) – Wesley

+0

当然有:)想想最好的计划行动是我做这个作为一个单独的PHP文件,并使用PHP包括否则代码会变得相当凌乱! – Nick

0

什么你基本上应该做的是:

$str = "Let's output some HTML here <h1>"; 
while ($x) { 
    $str .= "This is my "; 
} 
$str .= "title</h1>"; 
echo $str; 

所以你就打破$str然后用连接符.