Site logo

Archived topic

Featured image on Archive page

5 replies · Started by Laurent on June 6, 2022

Viewing posts 1–6 of 6

Hi,

I'm trying to add the "Featured Image" function for categories.

I have followed the explanations here:
https://generatepress.com/forums/topic/how-do-i-get-the-header-on-the-category-pages/#post-1440100
https://generatepress.com/forums/topic/advance-custom-field-image-for-my-category-archive-header-element-background/#post-1492195

Here are some screenshots of what I did:
[url=https://postimg.cc/GBFCB51V][img]https://i.postimg.cc/GBFCB51V/screenshot-dev-chantdescailles-be-2022-06-06-22-22-59.png[/img][/url] [url=https://postimg.cc/gLJFTc6b][img]https://i.postimg.cc/gLJFTc6b/screenshot-dev-chantdescailles-be-2022-06-06-22-23-51.png[/img][/url] [url=https://postimg.cc/1nyZDrmt][img]https://i.postimg.cc/1nyZDrmt/screenshot-dev-chantdescailles-be-2022-06-06-22-24-16.png[/img][/url] [url=https://postimg.cc/5YpWgQym][img]https://i.postimg.cc/5YpWgQym/screenshot-dev-chantdescailles-be-2022-06-06-22-25-07.png[/img][/url]

Do you understand what I have to do to make the image appear near the category title?

Thank you in advance for your answer.
See you soon,

Hi there,

Not sure if I fully understand the question. Featured images (WP core function) can only be added to static pages and posts and not to archives/blogs.

Would this video be helpful?
https://youtu.be/L1KtrKJIFzk

Let me know :)

Hi Laurent,

Are you using a Header Element or a Block Element - Page Hero?

Perhaps you can provide a link to the site in question?

If you’re using a Page Hero, you may create a custom portable hook with a shortcode through adding this PHP:

function your_shortcode($atts, $content = null) {
      ob_start();
      do_action('hook_name');
      return ob_get_clean();
}
add_shortcode('portable_hook', 'your_shortcode');

Adding PHP reference: https://docs.generatepress.com/article/adding-php/#code-snippets

Then, you may add shortcode [portable_hook] hook to a Shortcode Block.

Then, you may add this code through a Hook Element hooked to Custom Hook - hook_name:

<?php
$term = get_queried_object();

$image = get_field('myimage', get_queried_object() );

if( $image ) {
	echo '<img src="'. $image . '">';
}
?>

Kindly replace myimage with your field name.

Example: https://share.getcloudapp.com/NQulvR41

See: https://share.getcloudapp.com/kpu8m1OR

Also see image appearing: https://share.getcloudapp.com/DOudvnkd

This should make the image appear wherever you want. The return format of the image should be image URL int the custom field settings.

Kindly let us know.

Hi Fernando,

Thank you very much for your reply but I can't answer your question.

Here is the address of the category where I am trying to post the image:
https://ouvaton.link/hRAv8F

Do you know how to find the answer to your question only by checking the page?

Thank you very much in advance.
Good evening,

I see. Thank you for adding the link to your site.

If that’s the case, can you try creating a Hook Element.

1. Add this code in the hook element:

<?php
$term = get_queried_object();

$image = get_field('myimage', $term );

if( $image ) {
	echo '<img src="'. $image . '">';
}
?>

Replace myimage with your field name for the image. The return format of the image field should be image URL.

2. Set the hook to generate_after_archive_title
3. check “Execute PHP”
4. Set the display rule location to Post Category Archive then choose the appropriate archive.

Example: https://share.getcloudapp.com/qGuODBoj

Also see: https://share.getcloudapp.com/mXuxmzqj

Kindly let us know how it goes!

This archived topic is closed to new replies.