I have created a custom theme, and have post excerpt on the front page from two categories.
I would like to have featured images for each excerpt, but it is not working with the solutions I have found here. It says either internal 500 error, or the featured_media is undefined.
The featured image turns up inside the post, but not on the front page. I am importing from
// import FeaturedMedia from āā¦/featured-mediaā;
There are two main ways to get the featured image from a category.
Option 1. Fetch it when you load homepage.
So you will need to do a actions.source.fetch('/your-category/');
Option 2. Modify your homepage rest api response in PHP
if ( ! function_exists( 'page_add_rest_data' ) ) {
function page_add_rest_data( $data, $post, $request ) {
if (page.template === "homepage.php") {
// Go get your categories and the data necessary and set it
}
return $data;
}
}
add_filter( 'rest_prepare_page', 'page_add_rest_data', 12, 3 );
I prefer option 2 since it will save you potentially 2 network calls and not have to do any promises etc. It also will be faster as 1 payload especially if you have object caching in your wordpress server.
I have tried putting this in the functions.php file now, but why does it say homepage.php? I am adding the image in my posts as featured image, and would like the image to also load on the frontpage for each post.
It is working in the post page, so that is working.
How about the front page in wp? What are you using to call it? Is it a page or the blog index?
Hmm nope. I like to generally have all the payload information in 1 API call (avoids multiple calls to the server and since object caching is available in my webhost, which is wpengine, it actually has snappy responses).
Hi again,
I would like to avoid implementing it in the wp-theme/wp-frontpage, since this is adviceable when you update the wp-theme the code will need to be updated and implemented again unless you use a child theme.
I would like the solution to be in my frontity theme, not the wp-theme.