Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Flexible featured image name #2799

Merged
merged 6 commits into from
Sep 2, 2022
Merged

Conversation

Agos95
Copy link
Contributor

@Agos95 Agos95 commented Aug 24, 2022

Purpose

This allows more flexibility in the naming of the featured image for posts, and fixes #2777.

With this change, featured image is searched following this order of operations:

  1. Search for a file *featured* in the post directory
    • this is for backward compatibility
  2. Search for a file .Params.image.filename in the post directory
    • this is for who prefers to have all the files related to a specific post inside the post directory
  3. Search for a file .Params.image.filename in the assets/media/ directory
    • this allows to use an image in assets/media/ as a featured one. In this way, multiple posts can have the same featured figure without the need to upload the same image multiple times in multiple folders
    • furthermore, now posts can just be single markdown pages and they are not required to be page boundles (since the featured image is searched in assets/media/ too)
    • in addition, using Hugo cascade, it is possible to specify a default featured image in config.yaml, which can be used as fallback in the case nothing is specified in the post front matter. The lines to add are:
cascade:
  # Configure blog posts (except post/_index.md)
  - _target:
      path: /post/[!_index]**
    image:
      # in assets/media/
      filename: default_featured_image.jpg

This PR also #fixes the code in the different views in order to use the same logic to select the featured image.
Lastly, the change is also applied in the other files where the featured image was searched by using the hard coded *featured* name.

Further CMS enhancement

With this PR, now the CMS can accept images with any name (thus fixing #2777). However, from the CMS you are not able to select an image which is already present in your assets/media/, neither you are able to upload an image to that folder, since the "image" widget is configured to look inside the post directory.

Maybe it can be useful to open a new issue to address also this problem.

@netlify
Copy link

netlify bot commented Aug 24, 2022

Deploy Preview for hugo-portfolio-theme canceled.

Name Link
🔨 Latest commit 0fcbb84
🔍 Latest deploy log https://app.netlify.com/sites/hugo-portfolio-theme/deploys/63086ea423258b000bc541f5

@netlify
Copy link

netlify bot commented Aug 24, 2022

Deploy Preview for markdown-slides canceled.

Name Link
🔨 Latest commit 0fcbb84
🔍 Latest deploy log https://app.netlify.com/sites/markdown-slides/deploys/63086ea42f680400092506c1

Copy link
Collaborator

@gcushen gcushen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for proposing an improvement!

  1. Please move all the repetitive code into a single function so that there is one place to maintain this code going forward rather than pasting the same block of code into 8 files.
  2. What's the purpose of adding <!--unformattedContent--> to the files?

@gcushen
Copy link
Collaborator

gcushen commented Aug 24, 2022

@Agos95
Copy link
Contributor Author

Agos95 commented Aug 25, 2022

I should have applied the requested fixes:

  • moved the code to find the featured image in partials/functions/get_featured_image.html
  • use the partial funcion code to get the featured image in the different template files
  • removed the tags <!--unformattedContent--> (they were just for my text editor in order to avoid to autoformat those lines on save)

Please, let me know if something more is required!

Copy link
Collaborator

@gcushen gcushen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the line breaks back to the end of files, otherwise it shows as a modification in GitHub and is inconsistent with the rest of the code base.

@Agos95
Copy link
Contributor Author

Agos95 commented Aug 26, 2022

Ok, I added a blank line at the end of files.

@gcushen gcushen merged commit 1fe36f5 into HugoBlox:main Sep 2, 2022
@gcushen
Copy link
Collaborator

gcushen commented Sep 2, 2022

@Agos95 thanks for contributing an improvement 🎉

mitchellolsthoorn added a commit to mitchellolsthoorn/wowchemy-hugo-themes that referenced this pull request Sep 6, 2022
The partial name misspelled the word "functions" as "funcctions". This caused an error when running the latest build. The error was introduced in HugoBlox#2799.
@Agos95 Agos95 deleted the flexible-featured-image branch April 28, 2023 07:21
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Featured Image naming flexibility in CMS
2 participants