【问题标题】:Pull latest posts from a specific category从特定类别中提取最新帖子
【发布时间】:2012-03-06 11:55:26
【问题描述】:

我正在尝试从特定类别中提取最新帖子。

我目前能够使用下面的代码提取所有最新帖子并以我想要的方式显示它们,但我无法从特定类别中做同样的事情。

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div id="content"><div id="circle"><div id="circle_text1"><p><?php the_time('M') ?></p></div>
<div id="circle_text2"><p><?php the_time('dS') ?></p></div></div>
<div id="text"><div id="title"><p><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></p></div>
<div id="name"><p>By <?php the_author(); ?></p></div>
<div id="blurb"><p><?php the_content('<br />Read More'); ?></p></div></div>
<div id="line_rule"><p>&nbsp;</p><hr /></div></div>
<?php endwhile; ?><?php else : ?><h2>Not Found</h2><?php endif; ?>

提前致谢

【问题讨论】:

    标签: wordpress wordpress-theming


    【解决方案1】:

    如果您从数据库中获取帖子,则使用 ID desc 排序,这将在顶部显示最新帖子。如果 ID 是自动递增的。喜欢

    select * from posts order by ID desc
    

    【讨论】:

      【解决方案2】:

      这是一个基本的 WP 查询,可自行重置,可在模板中多次使用。您可以将您的 html 添加到其中。 showposts 是要显示的帖子数; -1 显示所有帖子。

      <?php $my_query = new WP_Query('category_name=mycategoryname&showposts=10'); ?>
      
      <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
      
      <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
      
      <?php the_title(); ?></a>
      
      <?php endwhile; ?>
      

      【讨论】:

        【解决方案3】:

        我一直这样做。这将起作用: 将 cat= 数字更改为您要使用的任何类别 ID。 根据需要添加 html 标记以及您想要提取的任何其他内容。

        <?php query_posts('cat=4&order=ASC');
        if (have_posts()) : while (have_posts()) : the_post(); ?>
        <?php the_content(); ?>
        <?php endwhile; ?>
        <?php endif; ?>
        <?php wp_reset_query(); ?>
        

        在示例中,我只是抓取帖子内容,但您可以输入标题、元数据等。重要的部分都在这里。 试试看。

        【讨论】:

          【解决方案4】:

          在开始的 IF 语句上方添加这行代码。

          <?php query_posts( 'cat=1' ); ?>
          

          然后更改 1 以匹配您尝试显示的类别的 ID。

          :)

          【讨论】:

            【解决方案5】:

            您只需将第一行添加到代码中即可:

            <?php query_posts( 'category_name=slug_of_your_category&posts_per_page=10' ); ?>
            

            将“slug_of_your_category”替换为您的类别的 slug,将“10”替换为您需要的帖子数量。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-05-09
              • 1970-01-01
              • 1970-01-01
              • 2019-10-23
              • 1970-01-01
              相关资源
              最近更新 更多