- This topic has 22 replies, 3 voices, and was last updated 1 year, 2 months ago by
David.
-
AuthorPosts
-
July 29, 2022 at 6:28 am #2297266
Niek
Hi iam trying to get a certain layout for my pages with posts.
As seen in private information iam trying to get post title on the right with under that date and time.
The left is a image from the category that shows from what website the article is.Can somebody help me with how to do this?
July 29, 2022 at 6:32 am #2297269Niek
The idea iam trying to get is shorturl.at/eopu6
Right now i have it like this: shorturl.at/acDPXJuly 29, 2022 at 6:52 am #2297296David
StaffCustomer SupportHi there,
the Image – where is this saved ? Is it the Featured Image of the post – if not then how is it added to the Category ?
July 29, 2022 at 6:58 am #2297299Niek
Hi David, thanks for your reply.
In the original example i saved it as an image to the FTP as “categoryname.webp”.
So if there is a way i can enclose the categoryname and add put html around it, that would work for me.July 30, 2022 at 3:47 am #2297936David
StaffCustomer SupportHmmm… you could try creating a shortcode that will get the category name of the first term. And outputting the Image HTML:
function display_category_image() { $id = get_the_ID(); $category = get_the_category( $id ); $cat_name = $category[0]->cat_name; $html = '<img class="cat-image" src="/cats/'.$cat_name.'.webp" />'; return $html; } add_shortcode('cat_image', 'display_category_image' );
Note the:
src="/cats/'.$cat_name.'.webp"
needs to match the URL path for your image.Then you can add the
[cat_image]
shortcode in your Query Loop.July 30, 2022 at 4:00 am #2297947Niek
Hi David,
Thanks for your help.
It does not display the category title though. Might there be a typo in the code?July 30, 2022 at 10:37 am #2298341Niek
When i troubleshoot the code and echo the $category it always give back “29”.
Not sure why it doesnt grab the right id.July 31, 2022 at 4:47 am #2298700David
StaffCustomer SupportYeah my bad the ID is coming from the Page not the current object in the Query Loop.
So thats not going to work. Hmmm…..Are you rebuilding the existing site ? Or is this a new site ? Just wondering if theres an alternative method.
July 31, 2022 at 4:50 am #2298701Niek
It’s a new website. I’m trying to get the same effect.
July 31, 2022 at 6:59 am #2298798David
StaffCustomer SupportOk, i think i am having a brain freeze lol – i have asked Tom to take a quick look at my shortcode as we should be able to make that work. Hope you don’t mind waiting a little while…
July 31, 2022 at 7:26 am #2298836Niek
Haha no problem buddy! I’ll wait a little longer.
Thanks in advanceJuly 31, 2022 at 7:41 am #2298847David
StaffCustomer SupportThanks for your patience, its bound to be something real obvious ๐
July 31, 2022 at 7:16 pm #2299276Tom
Lead DeveloperLead DeveloperHi there,
This is a tricky one as
get_the_ID()
doesn’t work in the Query Loop. You can see the same issue here with the core Query Loop block: https://github.com/WordPress/gutenberg/issues/35676We also don’t have access to the
$block
variable in a shortcode, so we can’t use the available context.I wonder if the plugin mentioned in the last comment in that issue will help?
Let us know ๐
July 31, 2022 at 11:59 pm #2299347Niek
Hi Tom,
Thank you so much for your answer.
I have installed the plugin and trying to figure out what i should fill in for the “Field name”.I expect i should fill in the HTML code in the prefix and suffix.
August 1, 2022 at 4:04 am #2299526David
StaffCustomer SupportOK, that explains it…. lets try a different approach.
Remove that plugin and the shortcode snippet i provided.And add this PHP Snippet:
function db_category_image( $block_content, $block ) { $id = get_the_ID(); $category = get_the_category( $id ); $cat_name = $category[0]->cat_name; if ( ! empty( $block['attrs']['className'] ) && 'cat-image' === $block['attrs']['className'] ) { $block_content = ' <figure class="gb-block-image has-cat-image"> <img class="cat-image" src="/cats/'.$cat_name.'.webp" /> </figure> '; } return $block_content; } add_filter( 'render_block', 'db_category_image', 10, 2 );
then in you Query Loop template, add an image block, select a placeholder image, and in Advanced > Additional CSS Class(es) add:
cat-image
-
AuthorPosts
- You must be logged in to reply to this topic.