1

I am using get_post_type_archive_link('post') on my index.php page to get the url of all posts from all categories.

I'm using it in a filter. I have listed all the categories separately using

$filter_cat=array();
$categories = get_categories();
foreach ($categories as $category_one) {
    $cat_ID = $category_one->cat_ID;
    $cat_name = $category_one->name;
    $filter_cat[] ='<a href="'.get_category_link( $cat_ID ).'" data-cat="'.$cat_ID.'">'.$cat_name.'</a>';
}
$filter_cat_out = '<li class="filter_button_category">' . implode('</li><li class="filter_button_category">', $filter_cat) . '</li>';
$filters_out = '<li class="filter_button_category filter_button_all">
                    <i class="icon-tag"></i><a href="'.get_post_type_archive_link('post').'" class="all_posts">'.esc_attr__('All', 'mytheme').'</a>
                </li>
                '.$filter_cat_out;

Now everything works fine, if I use blog as my main page, but if I select some page as a static page, and I get to my index page manually (I need to go to one of the categories), the 'All' link will always point to current category instead of archive page with all the categories listed.

Is such thing even possible? Getting a link to a page where all the categories will be listed in my archive page?

2

get_post_type_archive_link() doesn't return a link for the post type post, because technically it has no archive registered on registration of the post type. You can check this, if you inspect the output of get_post_type_object( 'post' ). So actually get_post_type_archive_link( 'post' ) will return false - see source.

You can get the link for the page for the posts another way:

$permalink = get_permalink( get_option( 'page_for_posts' ) );
  • Your solution returns the first post link :\ I suspected that something like what you've said could happen, wasn't 100% sure. – dingo_d Dec 2 '15 at 12:28
  • Have you set the blog page via »Dasboard > Settings > Reading > Front page displays > A static page«? @dingo_d Or to say it another way, you have to input a valid ID. – Nicolai Dec 2 '15 at 12:32
  • When I use static page, as I've mentioned in my question, I can't get the link, when I set wp to display my blog posts, it works. But I don't want to display my front page as a blog page. If I understand you correctly, I need to set some page, to be my posts page for your link to work, no? – dingo_d Dec 2 '15 at 12:53
  • Firstly, setting the »Posts page« at above mentioned settings page, doesn't make that page the front page. Those settings are well documented, read up on them to know more. Secondly, you don't need to use - or probably better, set - that, you could work with a page (template), which is likely what you are doing. Last but not least, to get the (correct) permalink you need to input an ID or post object - valid of course - if you do that, then you'll get the link. Otherwise the current post will be returned, which is what happened for you. @dingo_d – Nicolai Dec 2 '15 at 13:02
  • Yep, as I thought, for your solution to work I need to have a page assigned as a blog page. I'll accept it, even tho it's not completely what I wanted. Thanks! – dingo_d Dec 2 '15 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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