【问题标题】:Wordpress - How do I show only the 4 newest posts on my page?Wordpress - 如何在我的页面上仅显示 4 个最新帖子?
【发布时间】:2015-05-05 14:23:57
【问题描述】:

我正在尝试仅在我的页面上加载 4 个最新帖子,并且我发现了一行应该可以工作的代码。问题是我不知道把它放在哪里。

<?php query_posts('posts_per_page=20'); ?>

这是我在寻找解决方案时发现的(虽然我不知道这是否也适用于仅显示 4 个最新帖子。如您所见,我已经在该行有 'catname=projecten'我不知道如何将两者结合起来。

<?php
query_posts('catname=projecten');
while (have_posts()) : the_post();?>
<div class="col-md-3">
  <div class="newsfeed center">
    <h1><?php echo get_the_title( $ID ); ?> </h1>
    <p>
<?php the_content(); ?>
      </p>
  </div>
</div>
<?php endwhile;
?>

希望有人能帮帮我!

【问题讨论】:

  • 谷歌查询帖子。该板上的大多数用户可能会因为缺乏努力而对您的问题投反对票,因为通过搜索很容易找到答案。我花了不到一分钟的时间。
  • 你根本不应该使用query_posts。现在,你应该使用WP_Query()
  • Reenactor Rob,我明白了。不过,问题不在于我缺乏努力。如果我自己没有尝试找到解决方案,我就不会在这里发布我的问题。我尝试了在网上找到的多种解决方案,但没有结果。我在问如何将 'catname=projecten' 和 'posts_per_page=20' 放在一行代码中。因为我当然不会问我自己是否还没有尝试过。
  • 谢谢rnevius,我会努力寻找的。 :)

标签: wordpress get posts


【解决方案1】:

我想回答这个问题,因为需要纠正的地方很少。应该这样做,并在下面注明。

<?php
query_posts('category_name=projecten&posts_per_page=4');
while (have_posts()) : the_post(); ?>
<div class="col-md-3">
  <div class="newsfeed center">
    <h2><?php echo get_the_title(); ?> </h2>
    <?php the_content(); ?>
  </div>
</div>
<?php endwhile; wp_reset_query(); ?>

几点说明:

  • 这是category_name,而不是catname

  • 您不应为每个标题使用&lt;h1&gt;,为了更好的可访问性,请使用&lt;h2&gt;&lt;h3&gt; 等。

  • get_the_title( $ID ); $ID 似乎没有必要在那里。

  • 不要将the_content();&lt;p&gt; 包装在一起,这已经是丰富的内容,如果需要,请使用&lt;div&gt;

  • 之后不要忘记重置查询。

http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

http://codex.wordpress.org/Function_Reference/query_posts

http://codex.wordpress.org/Class_Reference/WP_Query

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-24
    • 2019-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多