Celebrating 7 Years! Get up to $30 off GeneratePress Premium!

[Resolved] Dynamic Post Header Based on Featured Image

Home Forums Support [Resolved] Dynamic Post Header Based on Featured Image

Home Forums Support Dynamic Post Header Based on Featured Image

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #514138
    Dev

    I am using “Page Header” and assigning “Global Locations” for different Page Headers. I understand that I can override the default page header for a given post type on the post edit page.

    What I want to do, however, is to dynamically change the Post Header for a Published Post based on if the “Featured Image” has been set.

    I may do this by writing a WP Cron function to look at all the published posts in the last 24 hours and change the “Page Header” if a featured image has been set.

    How may I approach doing this? I know how I can write a function to check for a featured image, I can write a wp cron job. What I don’t know however is how I can change the “Page Header”.

    Effectively the same option that is provided in the UI in “generate-meta-box-content” on post edit page, but do that dynamically.

    #514272
    Leo
    Staff
    Customer Support

    Hi there,

    If I understand this correctly, all you have to do is to create a page header and turn on Add Background Image without actually uploading an image in that specific page header.

    This will make it so that the page header will automatically grab the featured image you assigned for that post/page and use it as a background image.

    Let me know if this helps.

    #514310
    Dev

    Leo/Tom,
    Thanks for suggesting that alternate option. I have been aware of it, however, like mentioned in my previous email I wish to invoke the assignment of a GP “Page Header” to a post programmatically. This is because my page header for a post with a featured image will be different from the post without it.

    Essentially, all I am asking for is how do I invoke the function that can be used to change the page header associated with the post. The functionality found in the UI for “Edit a post” in the meta boxes below the post.

    Under UI, this is found under generate_layout_options_meta_box -> #generate-layout-page-header -> Select box name “_generate-select-page-header”

    #514361
    Tom
    Lead Developer
    Lead Developer

    In GPP 1.6 (available for testing: https://generatepress.com/gp-premium-1-6/), we introduced a new filter: generate_page_header_id

    This filter allows you to change the Page Header ID based on your select conditions.

    So you can write your conditionals to select a certain post or set of posts, and change the ID to a specific Page Header.

    Let me know if you need more info πŸ™‚

    #514605
    Dev

    Thanks Tom. I will give that a try.

    #514945
    Tom
    Lead Developer
    Lead Developer
    #529983
    Dev

    Is this filter available now in 1.6?

    Would you mind sharing an example of how to use it?

    #530084
    Dev

    Never mind. I figured it out.

    #530167
    Tom
    Lead Developer
    Lead Developer

    Awesome πŸ™‚

    For others who are curious, this should do it:

    add_filter( 'generate_page_header_id', function( $id ) {
    	if ( is_page( 'about' ) ) {
    	 	return 17686; // ID of the Page Header to use.
    	}
      
      	return $id;
    } );
Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.