On-demand wordpress top posts: full-text output or list output

Source: Internet
Author: User
Setting wordpress top posts has options when publishing articles. this is very simple and you don't know how to google it. the most difficult way is to output top posts in a certain format in a template, this is what we will discuss in this article. The key function of wordpress pin-to-top article about setting wordpress pin-to-top articles has options when publishing articles. this is very simple and you don't know how to google it yourself, the most difficult way is to output the top article in a certain format in the template. this is what we will discuss in this article. Wordpress pin-to-top article key functions wordpress has two common functions: is_sticky (): Returns true if the article is set to the top. if not, returns falseget_option ('sticky _ posts '): get the top article ID, and return an array containing the top article ID. how do you use these two functions? here are two examples. Example 1: When the homepage displays the article, introduction to the full-text output method for top-down articles: In loop, if is_sticky () is used to determine whether it is a top-down article, set the global variable $ more = 1; then call the_content (); that is, the full text is output; otherwise, set the global variable $ more = 0 if it is not the top article; then call the_content ('more... '); is the output of the <-- more --> tag [php]

The sub-chapter list is as follows:

  • [Pin to top] "title =" "Rel =" bookmark ">

  • "Title =" "Rel =" bookmark ">

No corresponding article found Example 2: Find all the top posts at a time, and then use the list method to present the method introduction: Use the get_option ('sticky _ posts') function to find all the top article IDs, use the query_posts () function to output the [php]
    $ Sticky, 'Caller _ get_posts '=> 1); if (have_posts (): while (have_posts (): the_post ();?>
  • ">" Alt =" "/>" Title =" "Rel =" bookmark ">
Note: the red part is the image in the blog post called. if there is no image, a default image is displayed. Or [php] 6, // Obtain a maximum of six top posts 'post _ in' => $ sticky); $ postQuery = get_posts ($ args ); // Set the circular output to the top article foreach ($ postQuery as $ post): setup_postdata ($ post);?>

"Title =" "Rel =" bookmark ">

In the foreach loop, you can use all the post-related template tags to obtain information, such as the title -- the_title () fixed link -- the_permalink () special image -- the_post_thumbnail () if you need to display the top articles of the current article category on the article page during topic development, refer to the following code: [php]
Top recommendation
Cat_ID); // query the specified category article if (have_posts ():?>

"Title =" "Rel =" bookmark ">

No articles...
Place the above code in your page template single. php. Return to the first top post [php] $ sticky = get_option ('sticky _ posts'); query_posts ('P = '. $ sticky [0]); or [php] $ args = array ('posts _ per_page '=> 1, 'Post _ in' => get_option ('sticky _ posts'), 'Caller _ get_posts' => 1); query_posts ($ args); note: the second method can only return the latest post. If no Post is published, the latest post is returned. Returns the first sticky Post. If no, no content is returned. [php] $ sticky = get_option ('sticky _ posts '); $ args = array ('posts _ per_page '=> 1, 'post _ in' => $ sticky, 'Caller _ get_posts' => 1 ); query_posts ($ args); if ($ sticky [0]) {// insert here your stuff ...}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.