douxing8323 2014-10-19 21:20
浏览 51
已采纳

怎么用wordpress API和php做到这一点

I am really a newbie in Wordpress and php. I created a theme for my website as follows:

enter image description here

I think my question is clear: in wordpress admin page I have four categories: 1,2,3,4. I want last 2 posts in category 1 to be displayed in section (div) 1 (shown above), last 2 posts in category 2 to be displayed in section (div) 2, and so on.
As I said, I am novice and I faced a tons of functions in Wordpress documentation.
for example I used the code below inside index.php (of my custom theme) in section 2 which outputs: "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; 
                ?>

Please give a piece of code to do this.

  • 写回答

1条回答 默认 最新

  • dongxiaofa6359 2014-10-20 00:25
    关注

    This should work:

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

    Change cat=1 to the id of each of the four categories.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler