2011-11-06 16 views
0

私は基本的にカテゴリ作業の中にいくつかのPHPを表示したいと思いますが、私はif文と出力PHPを自分で作成する方法を知っていますが、 2つ、誰かが私を啓発することができればそれは大いに感謝されるだろう。基本的なコードは以下の通りですが、私は私には許されていないと推測しています<?php inside a <?php?PHPがelse if文の中にPHP -

多くの感謝!

<?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のような、それを使用して、テンプレート言語である$strを打破して、連結演算子を使用します。

<?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 ?>タグで囲まれていないコードはそのままブラウザに出力されます。 Webサーバが開始タグ<?phpを検出するたびに、PHPインタプリタに制御を渡し、PHPインタプリタはそのタグで囲まれたコードを実行します。

+0

ありがとうございました。 – Nick

0

あなたは、いくつかのテキストでwhileループを入れたい場合、私はあなたがあなたの記事をエコー、ここの間で非常にwhile(hava_posts()) {のようなあなたのwhileループを作り、";とデ・エコーを終了するだろう、echo文でPHPタグを入れています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; 

だから、あなただけの.