创建一个特色博客布局WordPress主题
新的设计展示特色职位在头,在列出其余的档案在一个标准的布局。它花了相当的定制把一切都是我想要的工作,所以我觉得我应该分享这些过程,希望帮助其他人。遵循这一概述后,看看混合查询文章片段被用来创建一个定制特色邮报布局在WordPress。
该计划
该计划为我自己的博客的布局是展示特色职位在蓝色的头部区域的主页,然后其余的档案列为典型的摘录。在我的例子中,我只会展示一个特色post(没有滑块的功能),它将永远是一个视频帖子。其余的最新文章列表将会出现在一个典型的布局与文章日期、标题和摘录成垂直的列表。
WordPress粘性帖子特性
WordPress已经有一个粘稠的帖子特性构建到其核心提供的基本功能特色文章选择循环的顶部。我把我的特色邮报远离其他内容,但黏黏的帖子功能仍然可以用这样有某种钩到目标后单独。其他的方法可能是创建一个特殊的“特色”的类别或标签来识别特殊的内容。
<?php query_posts(array('post__in'=>get_option('sticky_posts'))); ?>
为了只显示在页眉的帖子, query_posts
函数可用。这段代码,检查文章的选项“粘性文章“在数据库。在我自己的网站我仅会设置一个粘稠的帖子,但如果多个便条纸将他们全部将会被显示。在一个公共主题需要额外的代码来限制数量的帖子被获取。
<?php query_posts(array('post__in'=>get_option('sticky_posts'))); ?> <?php while (have_posts()) : the_post(); ?> <article class="sticky"> <div class="desc"> <h2 class="header">Featured Post:</h2> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <?php the_content(''); ?> <ul class="post-info"> <li><?php the_time('jS F Y'); ?></li> <li>Posted in <?php the_category(', '); ?></li> <li><a href="<?php the_permalink(); ?>#comments"><?php comments_number('No Comments', '1 Comment', '% Comments' );?></a></li> </ul> </div> </article> <?php endwhile; ?><?php wp_reset_query(); ?>
这个查询职位函数之后,一个标准的WordPress循环实际显示的文章和相关的信息。注意 <?php wp_reset_query(); ?>
代码底部?这是一个重要的片段,以确保特殊查询不是也实现了在下一个循环。
<?php if(is_home() && !is_paged()) { ?> <?php query_posts(array('post__in'=>get_option('sticky_posts'))); ?> <?php while (have_posts()) : the_post(); ?> <article class="sticky"> <div class="desc"> <h2 class="header">Featured Post:</h2> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <?php the_content(''); ?> <ul class="post-info"> <li><?php the_time('jS F Y'); ?></li> <li>Posted in <?php the_category(', '); ?></li> <li><a href="<?php the_permalink(); ?>#comments"><?php comments_number('No Comments', '1 Comment', '% Comments' );?></a></li> </ul> </div> </article> <?php endwhile; ?><?php wp_reset_query(); ?><?php } ?>
我遇到的唯一问题与此代码是大型特色文章标题也会显示在你的文章分页列表导航回通过档案在首页。这使得很难看到页面刷新了新内容低于这个持久特色职位。
一个有条件的标签帮助建立循环以特色区域只出现在未分页版本的主页: <?php if(is_home() && !is_paged()) { ?>
.
更多的定制循环与查询_文章
与特色内容被成功地驶进了头我假定一个标准的WordPress循环可以被用来显示其余的档案。问题是黏黏的文章也将出现在这个较低的部分页面了,只留下页面上的重复内容。这意味着需要更多的定制的循环。
<?php query_posts(array("post__not_in" =>get_option("sticky_posts"), 'paged' => get_query_var('paged'))); ?>
查询_文章被再次使用定制第二个循环。这一次查询排除任何文章标记为粘。一个快速测试结果显示一切按预期运行,直到分页按钮在页面的底部被使用。这个 query_posts
函数是伟大的,除了它打破了分页行为正常循环。幸好这是快速固定通过添加一个额外的查询代码。
<?php query_posts(array("post__not_in" =>get_option("sticky_posts"), 'paged' => get_query_var('paged'))); ?> <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <article <?php post_class(); ?>> <div class="date"> <p><?php the_time('d'); ?> <span><?php the_time('M'); ?></span></p> </div> <div class="title"> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <ul class="post-info"> <li>Posted in <?php the_category(', '); ?></li> <li><a href="<?php the_permalink(); ?>#comments"><?php comments_number('No Comments', '1 Comment', '% Comments' );?></a></li> </ul> </div> <?php the_content(''); ?> </article> <?php endwhile; ?> <nav class="pagination"> <ul> <li class="older"><?php next_posts_link('Older posts'); ?></li> <li class="newer"><?php previous_posts_link('Newer posts'); ?></li> </ul> </nav> <?php endif; ?> <?php wp_reset_query(); ?>
这第二个查询文章围绕另一个典型的WordPress循环功能来显示填迟日期、标题与摘要的最新文章。这个分页链接允许用户回到过去通过档案一次一页地。
最后的代码
这里的一个概览的完整的代码用于索引。php的博客主题。标准的WordPress内容是淡出,离开了自定义查询用于创建这个布局更加突出的特色职位。