2016-04-28 27 views
0

我有一個小問題,爲什麼我的內聯PHP代碼不起作用:WordPress的在線PHP不工作

如果我寫我的代碼是這樣的:這是非常醜陋BTW(IMO),它的偉大工程。

<?php 
$cat = get_terms('logietype'); 
?> 
<section class="paddings"> 
    <div class="container-full"> 
     <?php 


     foreach ($cat as $catVal) { 
      echo "<div class='row'>"; 
      echo "<h2 class='logieheader'>".$catVal->name."</h2>"; 
      $postArg = array('post_type'=>'logie','posts_per_page'=>-1,'order'=>'desc', 
           'tax_query' => array(
                array(
                 'taxonomy' => 'logietype', 
                 'field' => 'term_id', 
                 'terms' => $catVal->term_id 
                ) 
          )); 

      $getPost = new wp_query($postArg); 
      global $post; 

      if($getPost->have_posts()){ 

        while ($getPost->have_posts()):$getPost->the_post(); 
         echo "<div class='col-md-4 no-padding'>"; 
         echo "<h3 class='logieheader'>".$post->post_title."</h3>"; 
         echo "<p>".$post->post_content."</p>"; 
         echo "</div>"; 
        endwhile; 

      } 
      echo '</div>'; 
     } 
    ?> 
    </div> 
</section> 

但是當我嘗試這樣的:這是更清潔(IMO),沒有什麼工作:(

<?php 
$cat = get_terms('logietype'); 
?> 
<section class="paddings"> 
    <div class="container-full"> 

     <?php foreach ($cat as $catVal): ?> 
      <div class='row'> 
      <h2 class='logieheader'><?php $catVal->name ?></h2> 
      <?php $postArg = array('post_type'=>'logie','posts_per_page'=>-1,'order'=>'desc', 
           'tax_query' => array(
                array(
                 'taxonomy' => 'logietype', 
                 'field' => 'term_id', 
                 'terms' => $catVal->term_id 
                ) 
          )); 

      $getPost = new wp_query($postArg); 
      global $post; ?> 

      <?php if($getPost->have_posts()): ?> 

        <?php while ($getPost->have_posts()):$getPost->the_post(): ?> 
         <div class='col-md-4 no-padding'> 
         <h3 class='logieheader'><?php $post->post_title ?></h3> 
         <p><?php $post->post_content ?></p> 
         </div> 
        <?php endwhile; ?> 

      <?php endif; ?> 
      </div> 
     <?php endforeach; ?> 

    </div> 
</section> 

任何人有任何想法,爲什麼一工程和其他沒有

? !

很多感謝

+2

這將是更容易幫助你,如果你(1)告訴我們你想要什麼(2)突出顯示了代碼塊之間的不同之處,(3)解釋了「沒有任何事情發生」的含義,以及(4)包含錯誤日誌中的任何錯誤消息。目前,你的問題面臨着風險,並被關閉。 –

+0

在'<?php $ catVal-> name?>中缺少echo和分號,' post_title?>''和' post_content?>' –

回答

1

這個錯誤是因爲下面一行:

<?php while ($getPost->have_posts()):$getPost->the_post(): ?> 
                 ^see this colon here 

它應該是,

<?php while ($getPost->have_posts()):$getPost->the_post(); ?> 
+0

無法正常工作:( –

+0

呵呵wauw我很笨,忘了實際回聲在php標籤:D –

+0

@FrankLucas請接受答案,如果它解決了你的問題。*乾杯!* :-) –

1

我覺得你應該把後分號,像這樣:

<?php $catVal->name; ?> 
<h3 class='logieheader'><?php $post->post_title; ?></h3> 
<p><?php $post->post_content; ?></p>