Wordpress page.php查询

时间:2014-02-13 12:18:57

标签: php wordpress

我正在尝试在Wordpress中执行page.php:

我想要有新文章的列表,然后我想展示文章,但它不起作用。

你能告诉我我做错了什么吗?

<li>Recent articles 
    <ul id="submenu_ul">
    <?php if (have_posts()) : ?>
    <?php $blog_query = 'showposts=5&cat=2&paged='.$paged;
    $posts = query_posts($blog_query);
       while (have_posts()) : the_post(); ?>
       <li><a href="<?php the_permalink()?>"><?php the_title(); ?></a></li>
       <?php endwhile; ?>
       <?php endif ?>              
     </ul>
</li>
<?php wp_reset_postdata(); ?>
<?php wp_reset_query(); ?>
<li>Comments</li>
<li>Ads</li>

</div>

<section id="the_section">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<h2><a href="<?php the_permalink()?>"><?php the_title(); ?></a></h2>
<p class="text"><?php the_content(''); ?></p>
<?php endwhile; ?>
<?php else: ?>
<h2>Žádne príspěvky</h2>
<p class="text">Litujeme, ale nenašli se žádne príspěvky</p>
<?php endif ?>

2 个答案:

答案 0 :(得分:0)

在查询后调用have_posts(),此函数检查当前WordPress查询是否有任何结果要循环。并按setup_postdata()

设置您的数据
      <?php $blog_query = 'showposts=5&cat=2&paged='.$paged;
      $post = query_posts($blog_query);
      setup_postdata($post);
      <?php if (have_posts()) : ?>
      while (have_posts()) : the_post(); ?>
      <li><a href="<?php the_permalink()?>"><?php the_title(); ?></a></li>
      <?php endwhile; ?>
      <?php endif ?>

试试这个

答案 1 :(得分:0)

<?php query_posts('showposts=4'); ?><?php while (have_posts()) : the_post(); ?><span><?php the_time('M d Y') ?></span><a style="text-decoration:none;" href="<?php the_permalink() ?>"><p><?php the_title(); ?></p></a><?php endwhile;?>

只需使用此代码