首页 文章

如何使用wordpress API和php完成此操作

提问于
浏览
0

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

enter image description here

我认为我的问题很清楚:在wordpress管理页面我有四个类别:1,2,3,4 . 我希望类别1中的最后2个帖子显示在部分(div)1中(如上所示),类别2中的最后2个帖子显示在部分(div)2中,依此类推 .
正如我所说,我是新手,我在Wordpress文档中遇到了大量功能 .
例如,我在第2节中使用了index.php(我的自定义主题)中的代码,输出:"Sorry, no posts matched your criteria."

<?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; 
                ?>

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

1 回答

  • 2

    这应该工作:

    $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 .

相关问题