1

In WordPress how can I add a menu link to an archive for Posts? I can see that I can add category archive links but what if I want to have an archive of all the posts (therefore including all categories?)

3
  • Are you referring to Custom Posts here? because you can see all posts on blog page, or may be I didn't get your question at all.
    – Kumar
    Oct 17, 2013 at 9:56
  • 1
    No I basically want to know the easiest way to create a page that will be the archive for all my posts, and then include it in the menu. Or maybe I don't have to create a page at all, I don't know.
    – urok93
    Oct 17, 2013 at 9:59
  • You have idea about setting blog page, right?
    – Kumar
    Oct 17, 2013 at 10:03

1 Answer 1

1

If your blog is setted to show last posts on home page, the link you need is just the home page link, that can be inserted using custom link.

If your blog is setted to display a static page as front page, also create a page and set it as "Posts page", then the link to that page, will be the link for posts.

If you need to change something on how posts are shown, e.g. the number of posts showed, use pre_get_posts action hook with is_home() condition (even if site home page is a static page).

add_action('pre_get_posts','show_all_posts');

function show_all_posts( $query ) {
  if ( ! is_admin() && $query->is_main_query() && is_home() ) {
     $query->set('posts_per_page', -1);
  }
}

Edit

As pointed out in comments, when home page is setted to display last posts, often it is styled differently from normal archives, in that case the solution is create page template.

E.g. create a file named pages-posts.php in the theme root (if the theme is developed by third party better create a child theme). In this file put simply

<?php
/*
Template Name: Posts Page
*/

get_template_part('index'); // or 'archive' or whatever template you want to use

After that in functions.php add:

add_action('pre_get_posts','show_all_posts');

function show_all_posts( $query ) {
  if ( ! is_admin() && $query->is_main_query() && is_page_template('pages-posts.php') ) {
    $query->set('page', false);
    $query->set('pagename', false);
    $query->is_singular = false;
    $query->is_page = false;
    $query->is_home = true;
    $query->set('post_type', 'post');
    $query->set('posts_per_page', 10);
  }
}

Now you have to create a page in dashboards, assign the page template and use this page to show posts.

It can seems a bad, hard work, but once WordPress as not a specific archive for standard posts only alternative to this is put the query in the pgae template, but in that case you will rune 2 query one to retrieve the page, one for posts, using my code you will rune one query, so for performance is better.

Edit 2

Just yesterday I released a new plugin Clever Rules using that plugin what you ask can be done with a line of code and without adding any additional page.

After you have installed and activated the plugin, create a new file called 'myrules.php' in it put:

<?php
/**
 * Plugin Name: My Rules
*/
add_action('plugins_loaded', 'register_my_rules');   
function register_my_rules() {
    if ( function_exists('register_clever_rule') )
        register_clever_rule('/posts')->query('post_type=post')->template('index.php');
}

After that save this file in your plugins folder. Go to dashboard and activate this "My Rules" plugin. You are done, now when you visit the url http://yoursite.com/posts/ you will see all your posts showed using index.php template. Change it if you want.

Please note plugin is in Beta so not fully tested.

3
  • Thanks, but what I'd like to know is how to create a page that has all blog posts at once. Do I have to use code as you showed, or is there some easier way?
    – urok93
    Oct 17, 2013 at 10:00
  • But I don't want to send them to the home page, I want a separate page. My home page template includes some other stuff that I don't need.
    – urok93
    Oct 17, 2013 at 11:55
  • @drtanz see anser edit
    – gmazzap
    Oct 17, 2013 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.