One of the great features of Genesis is the ability to get the featured image of any given Post or Page.
The Genesis function to use is
The function has many built-in features, including the ability to get the source URL instead of the markup for the image, to customize the output, to set a default fallback image if no featured image is available, to fetch one of the images attached to the post rather than the featured image and the list goes on.
You will want to use a hook to control where to output the image. The example below uses
genesis_entry_content, which will output the image directly after the body of each Post or Page.
genesis_get_image accepts the following arguments:
post_id – The ID of the Post or Page whose featured image you want to get
Default: the current post in the Loop
format – Whether to output the markup for the image (
html) or its source URL (
url). Any other value will return the root-relative path to the image (i.e. the source URL minus your domain name).
size – The size of the image. See the WordPress codex for how to format this argument.
attr – Any custom HTML attributes you’d like to add to the image. See the WordPress codex for how to format this argument. Note that this argument is only applicable if you specify
html as the format.
fallback – What to display if the Post or Page has no featured image. You can either specify:
first-attachedfor the first attached image to the Post or Page
- an array with keys
urlpointing to the markup and source URL of an image
- the attachment ID of an image in your Media Library.
Note that if you supply an array with
format argument will be used to determine which one to output, but any other arguments you specify (
attr etc) will have no effect.
num – You can use this to get the second, third or fourth image attached to a Post or Page instead of the featured image, regardless of whether a featured image exists. Specify 1 to get the second, 2 to get the third and so on. Note that the
fallback argument must be set to
first-attached for this to work.
Default: checks for a featured image
context – This argument currently does not affect the output. However, if you are using any of the filters on the function (see below), you can use it for discrimination purposes.
- the markup for the image if the
formatargument is set to
- the source URL to the image if the
formatargument is set to
- the root-relative path to the image if the
formatargument is set to anything else
falseif there’s no suitable image
falseif there’s no featured image and you supply an array without a
urlkey as the
There are 3 filters on this function:
genesis_get_image_default_args – use this to change the default values of the arguments that are used when not specified in the function call.
genesis_pre_get_image – use this to short-circuit the function.
genesis_get_image – use this to modify the final output of the function.
genesis_image is a shortcut function to echo the output of
genesis_get_image. The arguments, return values and filters are all the same.
genesis_get_image has been included since version 0.1.0.
genesis_get_image is located in lib/functions/image.php.
- How to Set a Default Featured Image with Genesis
- How to Insert Content Before or After the Entry Content with Genesis
Relevant tutorials from the Genesis community.