🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
新建主题,新建index.php和style.css: 在index.php加入以下循环输出post的代码: ~~~ <?php if(have_posts()) : while (have_posts()) : the_post(); ?> <h2>文章的标题</h2> <?php endwhile; else : echo '<p>No content found</p>'; endif; ?> ~~~ 网站有6遍文章,动态输出: ![](https://box.kancloud.cn/d70bb0e426a2d38ec35cf1a724f7bb5e_768x332.png) 动态输出具体标题和内容: ~~~ <?php if(have_posts()) : while (have_posts()) : the_post(); ?> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <?php the_content(); ?> <?php endwhile; else : echo '<p>No content found</p>'; endif; echo paginate_links(); ?> ~~~ ![](https://box.kancloud.cn/2a63a0ff4ea2ca9cabe4f5041aae890d_1064x454.png) 使用WP_Query函数输出文章分类: ~~~ <!-- home-columns --> <div class="home-colums clearfix"> <!-- one-half --> <div class="one-half"> <?php // opinion posts loop begins here $opinionPosts = new WP_Query('cat=10&posts_per_page=2'); if ($opinionPosts->have_posts()) : while ($opinionPosts->have_posts()) : $opinionPosts->the_post();?> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <?php the_excerpt(); ?> <?php endwhile; else : // fallback no content message here endif; wp_reset_postdata(); ?> </div><!-- /one-half --> <!-- one-half --> <div class="one-half last"> <?php // opinion posts loop begins here $newPosts = new WP_Query('cat=8&posts_per_page=2'); if ($newPosts->have_posts()) : while ($newPosts->have_posts()) : $newPosts->the_post();?> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <?php the_excerpt(); ?> <?php endwhile; else : // fallback no content message here endif; wp_reset_postdata(); ?> </div><!-- /one-half --> </div><!-- /home-columns --> ~~~ ![](https://box.kancloud.cn/ca91e62096ceb31c304cd4e2417b2469_1756x768.png) 带有特色图输出特定类别的文章: ~~~ <section class="class"> <ul class="class"> <li> <a href="#" target="_blank"> <img src="<?php bloginfo('template_directory'); ?>/picture/picture.jpg" alt="123" /> <p>类别<br /><strong>123</strong><br /></p></a> </li> <?php $cats = new WP_Query('cat=18&posts_per_page='); if ($cats->have_posts()) : while ($cats->have_posts()) : $cats->the_post();?> <li> <a href="<?php the_permalink(); ?>"> <?php the_post_thumbnail('cases&posts-image'); ?> <p>类别<br /><strong><?php the_title(); ?></strong><br /></p></a> </li> <?php endwhile; else : // fallback no content message here endif; wp_reset_postdata(); ?> </ul> </section> ~~~ 【1】全部循环 ~~~ <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <!--需要循环的模块--> <?php endwhile; ?> <?php endif; wp_reset_query(); ?> ~~~ 【2】调用某个分类,显示多少遍的循环 ~~~ <?php if (have_posts()) : ?> <?php query_posts('cat=3' . $mcatID. '&caller_get_posts=1&showposts=6'); ?> <?php while (have_posts()) : the_post(); ?> <!--需要循环的模块--> <?php endwhile; ?> <?php endif; wp_reset_query(); ?> ~~~ 【3】特定页面/文章 ~~~ <?php query_posts('page_id=2');//修改页面ID ?> <?php while (have_posts()) : the_post(); ?> <?php echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 300,"……"); //修改显示字数 ?> <?php endwhile;wp_reset_query();?> ~~~ 调用某个分类下的文章 ~~~ <?php $rand_posts = get_posts('category=ID&numberposts=5&orderby=date');foreach($rand_posts as $post) : ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endforeach;?> ~~~