categories - Link to index.php from home.php?

I have home.php that displays recent posts from two prominent categories.At the bottom of home.php, I have a link that s

I have home.php that displays recent posts from two prominent categories.

At the bottom of home.php, I have a link that says "All Posts" which I would like to have point to a listing of all posts. When I rename home.php and view index.php instead, this is what comes up.

How do I create a link in my home.php that effectively points to index.php?

Thanks in advance.

I have home.php that displays recent posts from two prominent categories.

At the bottom of home.php, I have a link that says "All Posts" which I would like to have point to a listing of all posts. When I rename home.php and view index.php instead, this is what comes up.

How do I create a link in my home.php that effectively points to index.php?

Thanks in advance.

Share Improve this question asked Jan 31, 2020 at 7:17 icannotflyicannotfly 31 bronze badge 2
  • home.php is the template that's used for whichever page is displaying your latest posts. In Settings > Reading what do you have set for your front page? And regarding home.php, how have you implemented the 2 prominent categories? Are you using custom queries, or filtering which categories the posts come from? – Jacob Peattie Commented Jan 31, 2020 at 8:00
  • I'm using custom queries for that. I have "your latest posts" set as the homepage. – icannotfly Commented Jan 31, 2020 at 8:06
Add a comment  | 

1 Answer 1

Reset to default 0

You can't link between templates. Not directly. The template files are just the files that WordPress will use to render the posts it's found, based on the URL. You can see how WordPress decides which template to load under which circumstances in the Template Hierarchy. So to solve this issue we need to determine when a page will display "All Posts", and how to link to that.

The first thing we need to do is set the correct settings in Settings > Reading. Your comment mentions that you have this set to display your latest posts on the homepage. This is incorrect. We know this because you want to display your latest posts when you click the All Posts link, not the homepage, which is only displaying some posts, based on custom queries.

So, what we need go do is create a page, under Pages > Add New, and set that page as the Front page. Then we need to create another page, and set that as the Posts page.

The next thing to do is determine the templates that we need. If you look at the template hierarchy, linked above, we can see that the Site Front Page will use the front-page.php template. This is the template that you should use to display "recent posts from two prominent categories". So copy your current home.php template into front-page.php.

Then, for the "All Posts" page, we can see in the template hierarchy that the Blog Index Page will use home.php, or — if that doesn't exist — index.php. So if index.php is displaying what you want for the "All Posts" page, you can just delete home.php.

So that should leave you with a homepage using front-page.php that displays the posts from the categories that you want, and an 'All Posts' page that displays all your latest posts using index.php.

The last step is to get the All Posts link to point to your All Posts page. You can get the link to your latest posts page like this:

<?php
$url = get_the_permalink( get_option( 'page_for_posts' ) );

echo '<a href="'. esc_url( $url ) . '">All Posts</a>';
?>

get_option( 'page_for_posts' ) gets the ID of the page that you've set as your Posts page in Settings > Reading, and get_the_permalink() gets the link to that page. This will let you change the title or slug of that page without breaking your template.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744787391a4593709.html

相关推荐

  • categories - Link to index.php from home.php?

    I have home.php that displays recent posts from two prominent categories.At the bottom of home.php, I have a link that s

    11小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信