2016-11-08 48 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

谢谢我的朋友... –

相关问题