2016-11-08 54 views
0

我寫了下面的代碼,用於顯示我在ID = 2的類別中編寫的最後4篇文章。它的作品,但問題是,雖然帖子的標題在WordPress的「所有文章」選項卡中不相同,但它每個帖子顯示相同的「標題」。問題是什麼 ?謝謝。爲什麼我的帖子標題與向用戶展示的內容相同?

<?php 
$Args = array('posts_per_page' => 4, 'order'=> 'DESC', 'category' => 2); 
$Posts = get_posts($Args); 
foreach ($Posts as $Post) : setup_postdata($Post); 
?> 

<div class="Post"> 
    <div class="Image"> 
     <img src="<?php bloginfo('template_url'); ?>/Images/Fruits.png" class="responsive-img"> 
    </div> 
    <div class="Context"> 
     <a href="<?php the_permalink(); ?>" class="Title"><?php the_title(); ?></a> 
     <p class="Text"><?php the_content(); ?></p> 
    </div> 
</div> 

<?php 
endforeach; 
wp_reset_postdata(); 
?> 

回答

0

你不應該使用大寫字母作爲變量名稱,在這種情況下,它必須是$ post而不是$ Post才能正常工作。您的更新代碼將如下:

<?php 
$args = array('posts_per_page' => 4, 'order'=> 'DESC', 'category' => 2); 
$posts = get_posts($args); 
foreach ($posts as $post) : setup_postdata($post); 
?> 

<div class="Post"> 
    <div class="Image"> 
     <img src="<?php bloginfo('template_url'); ?>/Images/Fruits.png" class="responsive-img"> 
    </div> 
    <div class="Context"> 
     <a href="<?php the_permalink(); ?>" class="Title"><?php the_title(); ?></a> 
     <p class="Text"><?php the_content(); ?></p> 
    </div> 
</div> 

<?php 
endforeach; 
wp_reset_postdata(); 
?> 
+0

謝謝我的朋友... –

相關問題