Display blog posts on any page (with navigation). How to display all WordPress posts on one page Wordpress display posts on a static page


By default, the latest posts in WordPress are displayed on the homepage, while older posts are accessible through the post navigation on / page / 2 /, / page / 3 /, and so on. In this article, we'll show you how to display your posts on any static page using a custom loop WP_Query which works great with post navigation.

For example, if you display static page as home page (as specified in Reading settings ), you may want to show the list of posts separately, for example on a separate page " blog". I came to this when I was doing my blog. xy.css while using a static (no blog posts) home page. I like this approach, since navigation through records in this form works intuitively, for example, like this:

This is extremely useful for specific CMS settings, such as when the home page is used as a forum, e-store, or navigation page. Here's a guide for how to display blog posts on your page (with navigation!)

Step 1. Page template

Create a blank page template, name it page-blog.php and add the following code there:

query ("showposts = 5". "& paged =". $ paged); while ($ wp_query-> have_posts ()): $ wp_query-> the_post (); ?>

"title =" (! LANG: Read more">!}

1) { ?>

Actually, that's all. Now just plug it in and stuff your own parameters for WP_Query - and you're done. For example, instead of displaying 5 records, you can set showposts = 10 or as much as you like.

Note that post navigation contains conditions that on the first page of posts (i.e., on your page / blog /) empty markup / styles for the link were not displayed " Next posts". You can read more about optimizing WordPress post navigation here.

Note also that this example uses slightly outdated markup, just to keep things simple. You will most likely have to make a few changes to keep it in sync with your theme.

Step 2. Adding a new page

Once you have finished and uploaded the file to the site page-blog.php, go to the admin console and go to the page for adding a new page. There, create a new page called " Blog"(or whatever you like), set its template to" Blog"from the panel" Page attributes".

Ready! Now go to the blog page and you will see how your loop WP_Query works: the latest blog posts will be displayed on the page, as well as navigation through the previous posts, if they exist, of course.

Conclusion

In WordPress, you can easily display your blog posts anywhere. In this article, we talked about how to display posts on any page using our WP_Query loop that supports navigation. This can be very helpful when setting up WordPress as a standalone custom CMS.

Retrieves records (posts, pages, attachments) from the database according to the specified criteria. You can select any posts and sort them as you like.

Array of WP_Post objects (posts). Each object in the array looks like this:

Array (=> object (WP_Post) # 4692 (24) (["ID"] => int (822) ["post_author"] => string (1) "1" ["post_date"] => string (19) "2016-07-07 10:28:57" ["post_date_gmt"] => string (19) "2016-07-07 07:28:57" ["post_content"] => string (6225) "Article content" ["post_title"] => string (37) "Dead Sea (14 photos)" ["post_excerpt"] => string (15) "Quote about the article" ["post_status"] => string (7) "publish" [ "comment_status"] => string (4) "open" ["ping_status"] => string (4) "open" ["post_password"] => string (0) "" ["post_name"] => string (95 ) "mertvoe-more-14-foto" ["to_ping"] => string (0) "" ["pinged"] => string (0) "" ["post_modified"] => string (19) "2016- 07-07 10:28:57 "[" post_modified_gmt "] => string (19)" 2016-07-07 07:28:57 "[" post_content_filtered "] => string (0)" "[" post_parent "] => int (0) ["guid"] => string (0) "" ["menu_order"] => int (0) ["post_type"] => string (4) "post" ["post_mime_type"] = > string (0) "" ["comment_count"] => string (1) "0" ["filter"] => string (3) "raw") => objec t (WP_Post) (...) => object (WP_Post) (...))

Usage

get_posts ($ args);

Usage pattern

// default parameters $ posts = get_posts (array ("numberposts" => 5, "category" => 0, "orderby" => "date", "order" => "DESC", "include" => array (), "exclude" => array (), "meta_key" => "", "meta_value" => "", "post_type" => "post", "suppress_filters" => true, // suppression of change filters SQL query)); foreach ($ posts as $ post) (setup_postdata ($ post); // output format the_title () ...) wp_reset_postdata (); // reset $ args (string / array) The list of arguments according to which the result will be obtained.
Default: preset

$ Args parameter arguments

Since WordPress 2.6, in addition to the parameters described below, get_posts () can take all the same parameters as WP_Query.

Get_posts () has suppress_filters enabled by default, which is disabled in query_posts () and WP_Query - i.e. filters work there. Enabling suppress_filters cancels all SQL query change filters of the following type: posts_ * or comment_feed_ *.

suppress_filters does not affect the work of the pre_get_posts filter - it will work regardless of what is specified in suppress_filters.

This disabling of filters by default can be confusing if there are plugins that affect the display of records through SQL query filters, such as WPML. In such cases suppress_filters should be disabled.

In the "category" parameter, you need to pass the ID, not the name of the category. Also "category" can be passed a string: ID separated by commas.

numberposts (number) The number of posts to display. Set to 0 to limit the output to the maximum number of posts per page (set in the VI settings) or set -1 to remove the output restrictions (LIMIT).
Default: 5 offset (number) Indent from the first post (record). category (number / string / array)

From which categories to display entries. Indicate the ID of the category from which you want to get posts or specify -3 instead of 3 if you want to get all posts, except for posts from category 3 (exclude category). You can specify several IDs separated by commas ("3,5,12" or "-3, -5, -12").

See WP_Query's cat parameter description.

In the WP_Query and query_posts () functions, this parameter is disabled by default (equal to false).
Default: true

Examples of

#one. Indented posts

If you have one, the last post on the main page, and you need to display 5 more previous ones from category 1, then you can use the following code:

    5, "offset" => 1, "category" => 1); $ myposts = get_posts ($ args); foreach ($ myposts as $ post) (setup_postdata ($ post);?>
  • ">

# 2. Indented posts without breaking the main loop.

If the get_posts () function was used and after it you need to use the standard WordPress loop, then you need to save the $ post global variable, it does it like this:

    5, "offset" => 1, "category" => 1); $ myposts = get_posts ($ args); foreach ($ myposts as $ post) (setup_postdata ($ post);?>
  • ">

# 3. Ability to use special functions of the VI Loop

By default, in a loop based on get_posts () it is impossible to use, for example, the_content () or the_date () functions. This problem is solved by the setup_postdata () function, which needs to be passed the $ post variable:

3); $ lastposts = get_posts ($ args); foreach ($ lastposts as $ post) (setup_postdata ($ post); // setting data?>

">

Data can also be obtained by accessing an object property (object-> object_property). For example, for this example, $ post-> ID will be the post ID, $ post-> post_content will contain the post content. An object property is a column of the posts database table. You can see the column names.

Do not forget that the data is displayed on the screen via the php echo statement:

ID; ?>

#4. Latest posts sorted by title

Let's get the latest posts sorted by title in alphabetical order. The following example will display the date, title, and quote of a post:

10, "order" => "ASC", "orderby" => "title")); foreach ($ postslist as $ post) (setup_postdata ($ post);?>


#5. Random posts

We will get 5 random posts, implemented using the "orderby" => "rand" parameter:

    5, "orderby" => "rand"); $ rand_posts = get_posts ($ args); foreach ($ rand_posts as $ post):?>
  • ">

# 6. Get all attachments

Used outside of the WordPress Loop. The following code will display the title, link and quote of the attached file:

"attachment", "posts_per_page" => -1, "post_status" => null, "post_parent" => null); $ attachments = get_posts ($ args); if ($ attachments) (foreach ($ attachments as $ post) (setup_postdata ($ post); the_title (); the_attachment_link ($ post-> ID, false); the_excerpt ();)) wp_reset_postdata (); ?>

# 7. Attached files of a specific post

The code needs to be used inside a WordPress Loop, where the $ post-> ID variable is:

"attachment", "posts_per_page" => -1, "post_status" => null, "post_parent" => $ post-> ID); $ attachments = get_posts ($ args); if ($ attachments) (foreach ($ attachments as $ attachment) (echo apply_filters ("the_title", $ attachment-> post_title); the_attachment_link ($ attachment-> ID, false);)) wp_reset_postdata (); ?>

#eight. Latest posts from the same heading

Let's display a list of the last posts of the current category in which the post is located. In this case, we will exclude the current record:

term_id; $ real_id = get_the_ID (); $ args = array ("cat" => $ cat_add_id); $ posts = get_posts ($ args); foreach ($ posts as $ post) (setup_postdata ($ post); if ($ post-> ID<>$ real_id) (?> ">

Notes

    Since version 2.6, a number of passed values ​​for the orderby parameter have been changed - the post_ prefix has been removed, for example, it was post_title, it has become just title.

  • Since version 3.0, ID arrays can also be passed to the include and exclude parameters.

Do you want to buy cheap Instagram views for video or TV broadcast, but don't know where? Try to visit the Doctor SMM website, where you will be offered one of the lowest prices for views on the Russian Internet. Hurry up, as the offer is valid for a limited time! In addition, here you can very quickly purchase a resource with the optimal speed mode, specifically for your page. Develop your account quickly and easily!

Notes

  • See: WP_Query :: parse_query ()

List of changes

Since version 1.2.0 Introduced.

The code get posts: wp-includes / post.php WP 5.2.3

5, "category" => 0, "orderby" => "date", "order" => "DESC", "include" => array (), "exclude" => array (), "meta_key" => "", "meta_value" => "", "post_type" => "post", "suppress_filters" => true,); $ r = wp_parse_args ($ args, $ defaults); if (empty ($ r ["post_status"])) ($ r ["post_status"] = ("attachment" == $ r ["post_type"])? "inherit": "publish";) if (! empty ($ r ["numberposts"]) && empty ($ r ["posts_per_page"])) ($ r ["posts_per_page"] = $ r ["numberposts"];) if (! empty ($ r ["category" ])) ($ r ["cat"] = $ r ["category"];) if (! empty ($ r ["include"])) ($ incposts = wp_parse_id_list ($ r ["include"]); $ r ["posts_per_page"] = count ($ incposts); // only the number of posts included $ r ["post__in"] = $ incposts;) elseif (! empty ($ r ["exclude"])) ($ r ["post__not_in"] = wp_parse_id_list ($ r ["exclude"]);) $ r ["ignore_sticky_posts"] = true; $ r ["no_found_rows"] = true; $ get_posts = new WP_Query; return $ get_posts-> query ($ r); )

For displaying posts on WordPress, we have a separate template file - single.php. However, this is the output of one record inside the loop, which is determined by the functions of the free engine and does not create any complexity. But how to display wordpress posts on another page or on the home page?


get_posts ()

In this case, we need the get_posts () function. With its help, we can get access to all posts according to the criteria we need. This method has a lot of advantages, one of them is the ability to sort posts by date, name, ID, etc. The list of all the arguments is quite large, you can view it in the WordPress code, but we will consider by direct example only the most necessary set for execution the task at hand.

So, the principle of the function:

$ posts = get_posts ($ args);

As you can imagine, in the $ args variable we set all the necessary parameters for the output of our posts. In the posts variable, we get an array of data that is ready to be looped out. After the end of the loop, do not forget to use the wp_reset_postdata () function, which will allow us to avoid bugs on the page with additional loops associated with displaying posts. Our code will look like this:

9, "category" => 1, "orderby" => "date"); $ myposts = get_posts ($ args); foreach ($ myposts as $ post) (setup_postdata ($ post);?>

">

This is a completely ready-made code to insert in the desired place in your theme template. Let me explain a little about the given arguments. In the numberposts parameter we set the number of displayed posts, in the category we specify the category ID, and orderby is responsible for sorting by date.

The loop already uses a regular template with the display of a thumbnail, link to the post, title and publication date. As you can see, everything is quite simple, now you can display wordpress posts anywhere in your theme.

For the convenience of visitors, and to speed up the indexing of articles by search engines, site maps (content) are created, both in XML and HTML format. In XML format, a sitemap is only suitable for search engines, and in HTML, you can display all records, and it is targeting visitors, but this is also a plus for search engines.

A small digression from the topic.
I decided to do a small blog redesign. Editing the sidebar styles the whole evening. As you can see, now it is of a different color + updated.
The blog template itself is now "non-rubber" and has a fixed width, and under each post has added social buttons from Addthis. That's it

Let's go back to the main one - a list of all blog articles on one page.

I wanted to make an HTML blog map using the plugin, but I didn’t like it because if a post belongs to two categories, then the names of the posts are repeated, that is, the effect is as if the blog has several times more posts than it actually is. ... The blog map is the following structure (if the posts are displayed by category):



Post date / Post title from category 1
Post date / Post title from heading 1 and heading 2 // repeat the post name!

Alternatively, you can display posts by tags, but the situation will be similar when displaying posts by headings, and you can optionally display a list of blog pages.

In the end, I decided to make a simple numbered list of all blog posts in descending order (by date of publication).

So let's get started.

Create a copy of the page.php theme file and rename it to something else, such as soderzhanie.php. Then open NotePad ++ in the editor and change the code:

Before editing, the code looks like this:

1 2 3 4 5 6 7 8 9 10 11 12 13 14

"")); comments_template (); endwhile;) get_footer ();?>

"")); comments_template (); endwhile;) get_footer ();?>

We leave only the output functions header-a and footer-a, and we don't need content

Where it says Vasya was here "Here will be the code for displaying all blog entries!" Paste in the following code:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 "post", "post_status" => "publish", "posts_per_page" => - 1, "caller_get_posts" => 1 if ($ moi_zapros -> have_posts ()): print "All posts: example" . "

All blog posts:

    " ; while ($ moi_zapros -> have_posts ()): $ moi_zapros -> the_post (); ?>
  1. "target =" _blank ">
  2. ?>

"post", / * Select only posts. * / "post_status" => "publish", / * And published only. * / "posts_per_page" => -1, / * Remove the limitation on the number of displayed posts per page. * / "caller_get_posts" => 1 / * Ignore the peculiarities of sticky posts. * /); $ moi_zapros = null; $ moi_zapros = new WP_Query ($ parametri); / * Form a new "non-standard" request. * / if ($ moi_zapros-> have_posts ()): print "All posts: example". "

All blog posts:

    "; while ($ moi_zapros-> have_posts ()): $ moi_zapros-> the_post ();?>
  1. "target =" _blank ">
  2. "; endif; wp_reset_query (); / * Resetting our selection. * /?>

    Final code for soderzhanie.php file:

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 / ** * Template name: All posts // page template name * /$ parametri = array ("post_type" => "post", / * Select only records. * /"post_status" => "publish", / * And published only. * /"posts_per_page" => - 1, / * Remove the limitation on the number of displayed records per page. * /"caller_get_posts" => 1 / * Ignore the peculiarities of sticky entries. * /); $ moi_zapros = null; $ moi_zapros = new WP_Query ($ parametri); / * Form a new "non-standard" request. * / if ($ moi_zapros -> have_posts ()): print "All posts: example" . "

    All blog posts:

      " ; while ($ moi_zapros -> have_posts ()): $ moi_zapros -> the_post (); ?>
    1. "title =" (! LANG: Permanent link for: " target="_blank">!}
    2. "; endif; wp_reset_query (); / * Dumping our selection. * / ?>

    "post", / * Select only posts. * / "post_status" => "publish", / * And published only. * / "posts_per_page" => -1, / * Remove the limitation on the number of displayed posts per page. * / "caller_get_posts" => 1 / * Ignore the peculiarities of sticky posts. * /); $ moi_zapros = null; $ moi_zapros = new WP_Query ($ parametri); / * Form a new "non-standard" request. * / if ($ moi_zapros-> have_posts ()): print "All posts: example". "

    All blog posts:

      "; while ($ moi_zapros-> have_posts ()): $ moi_zapros-> the_post ();?>
    1. "title =" (! LANG: Permanent link for:" target="_blank">!}
    2. "; endif; wp_reset_query (); / * Resetting our selection. * /?>

      Would you like to display all your WordPress posts on one page? Recently one of our readers was interested in how to create an archive page and display all entries on it. In today's article, we are going to show you how to display all your WordPress articles on one page without pagination.

      Why and when do you need to display all records on one page?

      WordPress has a built-in archive page for each category, tag, author, and date.

      Many site owners still prefer to create their own archive pages on their sites. These pages usually highlight popular entries, display an archive by date in the form of an accordion, display a list of headings or a tag cloud, etc.

      Some blogs prefer to simply list the titles for all WordPress posts on one page.

      Displaying all WordPress posts on one page

      There are many different ways to display all your posts on one page. You can display articles on the page with a shortcode, you can display them using a plugin, and, finally, you can show all the posts on the page using an arbitrary template and loop.

      We'll cover all three ways, and we'll start with the easiest one.

      Method 1: Using the Display Posts Shortcode Plugin

      First thing you need to do is install and activate the Display Posts Shortcode plugin.

      The plugin works out of the box and does not need any additional configuration.

      Go ahead and create a new page, call it "Archives" or whatever you like. After that, insert the following shortcode into it:

      This shortcode will display a simple chronological list of all your post titles. The shortcode parameters specify the limit for 1000 records per page.

      If you have more than a thousand posts, then you can change this value. You can also change the order of records to ASC, and the records will be displayed in reverse chronological order (old records first).

      While you can use the above shortcode to display quotes, thumbnails, and other related information, we do not recommend doing so. Since you display all your posts on one page, this page will be very long and you need to make it as simple and beautiful as possible. Here it will be appropriate to display only the titles of the posts.

      If you need to display entries on the page depending on the category or by other parameters, then you can do this by reading the plugin documentation.

      Method 2: Using the Simple Yearly Archive Plugin

      If you display all your WordPress posts on one page, then it will take a long time to scroll. This can be avoided by displaying a list of records grouped by year. Users will be able to click on the desired year and expand it, and then see the publications for the selected year.

      First you need to install and activate the Simple Yearly Archive plugin.

      After activation, go to the page Settings »Simple Yearly Archive to configure the plugin.

      This plugin will allow you to display a list of posts in different variations. You can display them all under the links to the annual archive, or display them in a "collapsed" list of each year.

      If you want to display them under the name of the year, you will need to add

      and
      next to the 'Before / After (Year headline)' option.

      The rest of the plugin settings are self-explanatory, so everyone can decide for themselves which options are worth checking.

      Don't forget to click on the save changes button.

      Now, in order to display all posts on one page, you need to add a shortcode to this very page.

      The plugin has many parameters that can be used in the shortcode. For a complete list of parameters, see the documentation page.

      Method 3: Display All WordPress Posts on One Page Using Template Code

      Using a plugin to display all posts on one page is the easiest solution, but some of you may want to implement this with code in your page template.

      First, we create a custom page template and copy the design from our page.php file into it.

      After that, we use the loop below to display all records on one page.

      "post", "post_status" => "publish", "posts_per_page" => - 1)); ?>have_posts ()):?>

        have_posts ()): $ wpb_all_query-> the_post (); ?>
      • ">

      If the above code doesn't make sense to you, then we recommend using Method 1.

Editor's Choice
The Nizhny Novgorod region and Nizhny Novgorod are historically the second center of the Russian Old Believers after Moscow. Currently in ...

Maslenitsa is one of the oldest Russian holidays. Pagan in origin, Maslenitsa peacefully "got on" with religious traditions ...

"Mom, draw!" Every mom sooner or later hears from her child the cherished "Mom, draw for me ...". And the options for ending this phrase ...

Archpriest Avvakum (1620-1682) is an outstanding historical figure. On Russian soil, the authority of this man in the 17th century was ...
There were brother and sister - Vasya and Katya; and they had a cat. In the spring, the cat disappeared. The children looked for her everywhere, but could not find it. Once they played ...
Aspiring artists often have situations when they have no experience in depicting something. In order not to get confused, to understand where to start ...
Holy water - ordinary in composition and original origin water (well, spring, lake, river, tap), wonderful ...
For a long time, you can buy chicken hearts in the store, but until recently, hearts were sold exclusively together with the liver ...
With apples and dried apricots (it is better to take turkey breast fillets for this dish), baked under foil - the dish is not quite ordinary, thanks to ...