I am not able to get featured images to my page. I try to use it in list-item.js file, please see the pictures below.
These warnings and errors came to Developer’s console:
Cross-Origin Read Blocking (CORB) blocked cross-origin response with MIME type text/html. See for more details.
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
I’ve implemented featured images on the list for mars-theme and will be available in the next release, but here is the implementation. I created a component called FeaturedMedia as the following:
// featured-media.js
import React from "react";
import { connect, styled } from "frontity";
const FeaturedMedia = ({ state, id }) => {
const media = state.source.attachment[id];
if (!media) return null;
const srcset = Object.values(media.media_details.sizes)
// Get the url and width of each size.
.map(item => [item.source_url, item.width])
// Recude them to a string with the format required by `srcset`.
.reduce(
(final, current, index, array) =>
final.concat(
`${current.join(" ")}w${index !== array.length - 1 ? ", " : ""}`
),
""
);
return (
<Image alt={media.title.rendered} src={media.source_url} srcSet={srcset} />
);
};
export default connect(FeaturedMedia);
const Image = styled.img`
margin-top: 16px;
height: 300px;
width: 100%;
object-fit: cover;
`;
Then, it’s imported in list-item.js, and passed an id prop as shown below:
As you can see, I’m using the field source_url of the attachment to get the link, because the field link is not always pointing to the image itself, but to the WP page associated to the attachment. That might be the problem you encountered in your implementation.
Also, to facilitate development, we exposed the store as a global variable so you can explore it easily from the console, as shown in the following video:
Thank you so much @orballo for quick and thorough answer!
It works now perfectly and yes of course it should be own component, I tried to just test it inside of list-item.
I am learning many things at the same time, so it feels sometimes a little overwhelmed.
Yes you are able to fetch featured images with below combination in list-item.js file (I tested it), but I think it’s better to use separate component ‘FeaturedMedia’ like @orballo shared above .
I have created a custom theme, but this solution doesn’t work for my post excerpt on the frontpage, it only renders the featured image inside the post.
Is there a solution for putting it also with the excerpt on the frontpage?