Unlimited $1.99 domain names - We use goDaddy 无限的1.99美元的域名-我们使用g oDaddy
Powered by MaxBlogPress 本站由MaxBlogPress

WordPress的哈克# 1 -q uery_posts| J TPRATT的博客误区
JTPratt的博客错误





Home » Wordpress Hack #1 - query_posts » WordPress的哈克# 1 -q uery_posts



Wordpress Hack #1 - query_posts WordPress的哈克# 1 -q uery_posts

1,692 views - 1692意见者:管

Posted in: 张贴于:

博客安装类形象WordPress的图像类
1,692 views 1692意见


Please note: This page was originally written in English.请注意:此网页最初是用英文写的。

The original post can be viewed原来的文章可被视为 here这里 .

Please note: This page was originally written in English.

The text has been translated using an online service such as Google or Babelfish.

The original post can be viewed here.


This is the very first post in the这是第一次在后 30 Wordpress Hacks in 30 Days Series 30 WordPress的技巧在30天系列 !

If you use Wordpress - then this is a series you will want to bookmark right now!如果您使用WordPress的-那么这是一个系列你会想现在的书签! You might even want to subscribe by email at the top of the page.您甚至可能想订阅通过电子邮件在页面顶部。 If you’re like me your sick and tired of googling the crap out of the web trying to find easy ways to hack and customize your Wordpress blog or web site.如果你像我这样的您感到厌倦使用Google的废话了网页试图寻找简便的方法来破解和自定义您的WordPress的博客或网站。 I’m going to give you a very useful and easy to perform Wordpress hack every day for a month!我给你一个非常有用的,易于执行WordPress的黑客每天都在一个月! I think that it’s easier to do something like this than write a humongous post with more information than you’ll never read or digest in one sitting.我认为,这更容易做这样的事情多写humongous后更多的信息比你将永远不会阅读或消化在一坐。 Instead I’m going to give you one very simple and easy task to do each and every day.相反我要去给你一个非常简单和容易的工作做的每一天。 Depending on your skill level, you may or may not want to take a look at last week’s post根据您的技能水平,您可能会或可能不希望来看看在上周的后 How to Create Your Own Wordpress Theme 如何创建自己的WordPress的主题 .

Wordpress Hack #1 - query_posts WordPress的哈克# 1 -q uery_posts

Today in our very first Wordpress Hack in our 30-day “Hack-a-thon” we’re going to learn about the Wordpress “template tag” query_posts.今天,在我们的第一个WordPress的哈克在我们30天的“砍一村”我们要了解WordPress的“模板标签” query_posts 。 You can您可以 read about query_posts in the Wordpress Codex here阅读有关query_posts在这里WordPress的法典 . To use any of these examples you must place the PHP code snippet before “the loop”.使用任何这些例子你必须把PHP代码片段在“循环” 。 If you don’t know what “the loop” is, just follow the link in the previous paragraph to create your own wordpress theme for an explanation.如果您不知道什么“循环”是,只需按照以下的联系前一段创建自己的WordPress的主题,一个解释。 Each and every code snippet can used any any page that uses “the loop”, such as your index.php, category.php, archive.php, tag.php, or search.php.每一个代码段可以使用任何任何网页,使用“循环” ,如您的index.php , category.php , archive.php , tag.php ,或search.php 。

Exclude posts that belong to a category 排除职位,属于一类

Maybe you don’t want posts from a certain category showing up on your index or another archive page.也许你不想员额从某一类出现在您的索引或其他存档页。 You could even use this to keep certain categories from showing in search results if you needed to.你甚至可以用它来保持某些类别显示在搜索结果中如果你需要。 You need to know your category “ID” to do this, just retrieve it from your dashboard under “Manage->Categories”, and the “-1″ with your category ID# like this…您需要知道您的类别“编号”要做到这一点,只是找回它从您的仪表盘下的“管理”类别“和” -1 “与您的类别编号:像这样...

 <?php query_posts('cat=-1'); ?> 

You can exclude multple categories like this…您可以排除multple类这样的...

 <?php query_posts("cat=-1,-2,-3"); ?> 

Retrieve a Post or Page 检索某一职位或网页

To retrieve a particular post you can either call it by ID (listed in your dashboard) or it’s slug like this (use one line of code or the other, not both)…要检索某个职位,您可以调用它的编号(列在您的仪表板) ,或者它这样段塞(使用一个代码行或其他,而不是两个) ...

 <?php query_posts('p=1'); //using post id query_posts('name=first-post'); //using post slug ?> 

You can do the same thing to retrieve pages as well like this…你可以做同样的事情检索的网页,以及这样的...

 <?php query_posts('page_id=7'); //retrieves page 7 only query_posts('pagename=about'); //retrieves the about page only ?> 

When you use those examples it retrieves the entire post.当您使用这些例子检索整个职位。 When you are notorious for writing extremely long posts (like I am) you may only want to get a partial post with the “read more” link.当您在臭名昭著的书面极长的职位(如我) ,你可能只是想后部分与“阅读更多”链接。 This is especialy useful if you’re going ot feature certain posts on your index or other pages.这是especialy有用如果你加时赛某些职位特点的指数或其他网页。 Here’s the code for that…这里的代码是...

 <?php query_posts('p=5'); //get post with id of 5 global $more; $more = 0; //gets partial post with read more link ?> 

You know that you can create pages, and you can also create “child pages” like I have on ths site.您知道,您可以建立网页,还可以创造“儿童网页”就像我曾在奥运会上。 I have a parent page called “series”, and then all the individual series index pages are children of that page.我有一个所谓的母公司页“系列” ,然后所有的个人网页系列指数是儿童的该网页。 If you want to get a child page - that’s possible as well, but you can’t call it by id - you have to call it by double-slug as in the example below (parent slug slash child slug).如果你想获得儿童网页-这是可能的,但你不能要求它的编号-你不得不调用它的双塞在下面的例子(母公司塞斜线儿童蛞蝓)。

 <?php query_posts('pagename=parent/child'); ?> 

Retrieve Post by Certain Authors 回收后的某些作者

If your blog has multiple authors you can retrive them by name or author id like this…如果您的博客有多个作者,您可以检索他们的名称或标识的作者这样的...

 <?php query_posts('author_name=John'); query_posts('author=3'); ?> 

Retrieve Every Single Post 检索每一个职位
Maybe you want to make some kind of an archive page or sitemap.也许你想要某种存档网页或网站。 Whether or not you create a page with this query of course depends on how many posts you really have, but inserting this code before the loop will list ever post you have all on one page.您是否创建一个网页,此查询当然取决于多少员额你真的有,但插入此代码前将名单循环后你永远都在一个页面上。 While the code below shows all posts on one page, you can change the “-1″ to just 1 post, or 5, or 10, or however many posts you want to display.虽然下面的代码显示所有职位在一个页面上,您可以变更“ -1 ”仅1个或5或10 ,或但许多职位要显示。

 <?php query_posts('posts_per_page=-1'); ?> 

Change the Order or Sequence of Posts 变更顺序或序列的帖子
By default a Wordpress blog shows you posts in a journal fashion or reverse date order.默认情况下,一WordPress的博客显示您员额时尚杂志或扭转日期秩序。 You could choose to instead sort your posts by author or title like this.您可以选择不是你的职位分类,按作者或标题这样。 You could use either the author or title lines.你可以使用作者或标题行。

 <?php query_posts('orderby=author'); query_posts('orderby=title'); ?> 

Using “orderby” there are many different parameters that you can use like these…用“ orderby ”有许多不同的参数,您可以使用喜欢这些...

* orderby=author * orderby =作者
* orderby=date * orderby =日期
* orderby=category * orderby =类
* orderby=title * orderby =冠军
* orderby=modified * orderby =修改
* orderby=menu_order * orderby = menu_order
* orderby=parent * orderby =母公司
* orderby=ID * orderby =编号
* orderby=rand * orderby =兰特

Retrieve a Post by Time Period 检索后的时间段

There are many different ways to consruct a query to retrieve certain posts based by date only.有许多不同的方式来consruct查询检索基于某些职位只按日期。 Here’sa way to get them for a day of the month…下面的方式让他们一天的一个月...

 <?php query_posts('day=15'); //all posts on the 15th ?> 

You could also get them for the current month and year with a query like this…您也可以让他们对当前的月份和年份的查询这样的...

 <?php $current_month = date('m'); ?> <?php $current_year = date('Y'); ?> <?php query_posts("cat=22&year=$current_year&monthnum=$current_month&order=ASC"); ?> 

Retrieve Posts based on Tags 检索员额的基础上标签
You can retrieve posts for a specific tag or tags like this (use one line of code at a time only).您可以检索员额为特定标记或标记这样的(使用一个代码行的时间只) 。 The first line retrieves posts with a particular tag, the second line is the format for getting posts for multiple tags, but the third line is for getting posts that were tagged in multiple categories.第一行检索职位特别标记,第二行是形式越来越员额多个标签,但第三条生产线是越来越员额,标记在多个类别。 In other words the second line will retrieve all posts tagged as bread all posts tagged as baking.换言之,第二条生产线将检索所有帖子标记为面包的所有帖子标记为烘焙。 But the third line will get only posts tagged in bread and baking and recipe.但是,第三条生产线不仅将获得员额标记在面包和面包和配方。

 <?php query_posts('tag=cooking'); query_posts('tag=bread,baking'); query_posts('tag=bread+baking+recipe'); ?> 

I hope this helps you do a little Wordpress “theme hacking” and customize your blog.我希望这可以帮助您做一些WordPress的“主题黑客” ,并自订您的博客。 If you have any questions about query_posts please comment now, and we’ll see your tomorrow for the next hack!如果您有任何疑问,请评论query_posts现在,我们会看到您明天的下一个攻击!


Tags:标签: , ,

5 Responses to “Wordpress Hack #1 - query_posts”第5回应“ WordPress的哈克# 1 -q uery_posts”

  1. Mark Wilson 马克威尔逊 Has the following to say...有下列说...

    I had actually been looking for the one about authors.实际上我一直在寻找的作者之一。
    thanks a lot, it been a big help.多谢,这是一个很大的帮助。

    Mark Wilson’s last blog post.. 马克威尔逊最后的博客帖子.. I hate the word “niche†我讨厌的词作为仙,包括“ nicheÃ仙,香港特别行政区最大的

  2. Make Money Blogging 博客赚钱 Has the following to say...有下列说...

    Loving the idea even though this one is sort of irrelevant for me, cannot wait for the rest.爱的想法,即使这个人是那种对我来说无关紧要,不能等待了。

  3. Erika 埃里卡 Has the following to say...有下列说...

    Okay… as a wordpress newbie (but someone who chose to dive head-first into WP development… silly me) I can’t tell you how much I value this series that you’re doin!好吧...作为一个WordPress的新手(但有人选择谁潜水头首次进入可湿性粉剂发展...愚蠢的我)我不能告诉你我是多么珍惜这个系列你doin ! Once I finally get my blog up and running, I’m going to have to toss out a thank-you for helping me so much!一旦我终于得到我的博客和运行,我将不得不抛了感谢你帮助我这么多!

  4. admin 管理员 Has the following to say...有下列说...

    @Erika - glad to help! @埃里卡-高兴地帮助! I love comments like this, because it means I made the right decision in starting this series…我喜欢这样的评论,因为这意味着我做出了正确的决定开始这一系列... =

  5. The Genesis: Welcome to Internet Espionage, Awesome WP Links, and More | Internet Espionage 起源:欢迎互联网间谍,棒极了可湿性粉剂连结,及更多|互联网间谍 Has the following to say...有下列说...

    [...] — Hacking query_posts in WordPress — In his attempt to present readers with 30 wordpress hacks in 30 days, the first in his [...] [...] -黑客q uery_posts在W ordPress的-在他的企图本读者30 Wo rdPress的黑客在3 0天内,第一次是在他[ . ..]

Question or Comment??问题或意见? ? Spill it Now...溢油现在...

跳跃的喜悦以上的评论!

We Reward Comments!我们奖励的意见!


We dofollow links, and get your latest blog post as a byline under every new comment from the "CommentLuv" plugin!我们dofollow联系,并获得最新的博客帖子的署名下的每一个新的评论从“ CommentLuv ”插件! Top commenters for every month are listed on every page of this site in a sidebar widget linked back to your URL!顶端commenters每个月列出的每个网页上本网站的联系栏部件回到您的网址! We would like to reward you for becoming part of our community!我们要奖励你成为我们的社会! Your comment is valuable not only to us, but also all the other readers of this blog!您的评论是有价值的,不仅给我们,而且所有的其他读者的博客!


Click to add smilies to your post! 按一下以新增的笑脸到您的帖子! == [ ]^ = (= ( (= ( |= )研究= | 8= 0= ) 〜= 00= (= ; ;= ) ]= ; ; ;