【问题标题】:how to do this with wordpress API and php如何使用 wordpress API 和 php 做到这一点
【发布时间】:2014-10-19 21:20:40
【问题描述】:

我真的是 Wordpress 和 php 的新手。我为我的网站创建了一个主题如下:

我认为我的问题很明确:在 wordpress 管理页面中,我有四个类别:1、2、3、4。我希望类别 1 中的最后 2 个帖子显示在部分 (div) 1 中(如上所示),类别 2 中的最后 2 个帖子显示在部分 (div) 2 中,依此类推。
正如我所说,我是新手,我在 Wordpress 文档中遇到了大量的功能。
例如,我在第 2 节的 index.php(我的自定义主题)中使用了以下代码,该代码输出:“抱歉,没有符合您的条件的帖子。”

   <?php 
                    $args = array( 'post' => 'post', 'posts_per_page' => 1,'category_name'=>'تازه ها');
                    $the_query = new WP_Query( $args ); 
                    ?>
                    <?php 
                    if ( $the_query->have_posts() ) : ?>
                    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
                    <h2><?php the_title(); ?></h2>
                    <div class="entry-content">
                    <?php the_content(); ?> 
                    </div>
                    <?php endwhile; ?>
                    <?php wp_reset_postdata(); ?>
                    <?php else:  ?>
                    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
                    <?php endif; 
                ?>

请提供一段代码来执行此操作。

【问题讨论】:

  • 我在帖子中将标签更改为类别并更改了代码,但仍然失败,尽管一切似乎都很好

标签: php wordpress


【解决方案1】:

这应该可行:

$posts = get_posts ("cat=1&showposts=2");
if ($posts) 
{
    foreach ($posts as $post):
    setup_postdata($post); ?>
    <h2><?php the_title(); ?></h2>
<?php endforeach;
}

cat=1更改为四个类别中每个类别的id。

【讨论】:

  • 如何获取任何类别的 id?
  • 也许通过这个? $category_id = get_cat_ID('Category Name');
  • 这也可以,但是如果你去 wordpress 的后端并编辑帖子类别,链接将类似于 http://example.com/wp-admin/edit-tags.php?action=edit&amp;taxonomy=category&amp;tag_ID=1&amp;post_type=posttag_ID=1 是类别 ID。
  • tag_ID=1 是类别 id 是什么意思?
  • 数字1是get_posts中使用的类别id
猜你喜欢
  • 2013-07-12
  • 1970-01-01
  • 2019-09-19
  • 2019-04-30
  • 2017-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多