permalinks - Link for the page archive for posts, like using custom post types with get_post_type_archive_link

admin2025-01-07  3

I am trying to get the page to list all my posts (post type = post) in archive.

I can do this with custom post types, with get_post_type_archive_link(), but using get_post_type_archive_link('post') to get the page url of all posts doesn't work the same way.

Also using get_permalink() or get_permalink( get_option( 'page_for_posts' ) ) returns only the link of one post.

Example:

I would like to get a link like www.example/posts/ or other, in a similar way as the custom post type, when using get_post_type_archive_link('news'), retrieves www.example/news/.

Can I get a list of posts other than in the home page? Is this possible?

Thanks for the help!

I am trying to get the page to list all my posts (post type = post) in archive.

I can do this with custom post types, with get_post_type_archive_link(), but using get_post_type_archive_link('post') to get the page url of all posts doesn't work the same way.

Also using get_permalink() or get_permalink( get_option( 'page_for_posts' ) ) returns only the link of one post.

Example:

I would like to get a link like www.example.com/posts/ or other, in a similar way as the custom post type, when using get_post_type_archive_link('news'), retrieves www.example.com/news/.

Can I get a list of posts other than in the home page? Is this possible?

Thanks for the help!

Share Improve this question edited May 5, 2017 at 14:18 user38561 asked May 5, 2017 at 10:33 user38561user38561 396 bronze badges 9
  • get_post_type_archive_link works with post as of several versions ago. – Milo Commented May 5, 2017 at 15:55
  • Possible duplicate of Get the blog page URL set in Options – Milo Commented May 5, 2017 at 15:56
  • I had already check that question, but .Well, for some reason the get_post_type_archive_link – user38561 Commented May 5, 2017 at 17:19
  • You can also look at get_post_type_archive_link source code to see how it gets that link in the case of post, it incorporates your other example. – Milo Commented May 5, 2017 at 17:24
  • @Milo thanks for the reply and link, but for some reason the get_post_type_archive_link is returning the URL of the root of website (www.example.com) and not something like (www.example.com/news/) . Has that suppose to return that? I can to distinguis from the front_page or home page! At the moment I have made a workaround using add_query_arg but probably is a good practice to use WP core functions dedicated for this. I have the WordPress version 4.7.3. – user38561 Commented May 5, 2017 at 17:35
 |  Show 4 more comments

1 Answer 1

Reset to default 0

I use a shortcode:

function shortcode_article_list() {
  $posts_array = get_posts( array('posts_per_page' => -1, 'orderby' => 'date', 'order' => 'DESC', 'post_type' => 'post', 'post_status' => 'publish') );
  $output = '<ul>';
  foreach ($posts_array as $post) {
    $a = explode(' ', $post->post_date);
    $output .= '<li><a href="' . home_url('/') . $post->post_name . '.html">' . $post->post_title . '</a> (' . $a[0] . ')</li>';
  }
  $output .= '</ul>';
  return $output;
}
add_shortcode('article_list', 'shortcode_article_list');

Perhaps there's something you can use from it.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736257667a455.html

最新回复(0)