Last active
August 6, 2019 21:55
-
-
Save neilgee/41339231372f24447edcc45a48a6cead to your computer and use it in GitHub Desktop.
Add Customizer Control for Adding Featured Image automatically to Posts/Pages - http://wpbeaches.com/add-featured-image-top-page-genesis/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php //<= dont add me in | |
add_action( 'customize_register', 'themeprefix_customizer_featured_image' ); | |
function themeprefix_customizer_featured_image() { | |
global $wp_customize; | |
// Add featured image section to the Customizer | |
$wp_customize->add_section( | |
'themeprefix_single_image_section', | |
array( | |
'title' => __( 'Post and Page Images', 'themeprefix' ), | |
'description' => __( 'Choose if you would like to display the featured image above the content on single posts and pages.', 'themeprefix' ), | |
'priority' => 200, // puts the customizer section lower down | |
) | |
); | |
// Add featured image setting to the Customizer | |
$wp_customize->add_setting( | |
'themeprefix_single_image_setting', | |
array( | |
'default' => true, // set the option as enabled by default | |
'capability' => 'edit_theme_options', | |
) | |
); | |
$wp_customize->add_control( | |
'themeprefix_single_image_setting', | |
array( | |
'section' => 'themeprefix_single_image_section', | |
'settings' => 'themeprefix_single_image_setting', | |
'label' => __( 'Show featured image on posts and pages?', 'themeprefix' ), | |
'type' => 'checkbox' | |
) | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php //<= dont add me in | |
// Add featured image on single post | |
add_action( 'genesis_entry_header', 'themeprefix_featured_image', 1 ); | |
function themeprefix_featured_image() { | |
$image = genesis_get_image( array( // more options here -> genesis/lib/functions/image.php | |
'format' => 'html', | |
'size' => 'large',// add in your image size large, medium or thumbnail - for custom see the post | |
'context' => '', | |
'attr' => array ( 'class' => 'aligncenter' ), // set a default WP image class | |
) ); | |
if ( is_singular()) { | |
if ( $image ) { | |
printf( '<div class="featured-image-class">%s</div>', $image ); // wraps the featured image in a div with css class you can control | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php //<= dont add me in | |
// Add featured image on single post | |
add_action( 'genesis_entry_content', 'themeprefix_featured_image', 1 ); | |
function themeprefix_featured_image() { | |
$add_single_image = get_theme_mod( 'themeprefix_single_image_setting', true ); //sets the customizer setting to a variable | |
$image = genesis_get_image( array( // more options here -> genesis/lib/functions/image.php | |
'format' => 'html', | |
'size' => 'large',// add in your image size large, medium or thumbnail - for custom see the post | |
'context' => '', | |
'attr' => array ( 'class' => 'aligncenter' ), // set a default WP image class | |
) ); | |
if ( is_singular() && ( true === $add_single_image ) ) { | |
if ( $image ) { | |
printf( '<div class="featured-image-class">%s</div>', $image ); // wraps the featured image in a div with css class you can control | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment