Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Entry Header, First Paragraph, Featured Image Above Content (from second para) and Sidebar on single Posts in Genesis. https://sridharkatakam.com/entry-header-first-paragraph-featured-image-above-the-remaining-content-and-sidebar-on-single-posts-in-genesis/
// Register a custom image size for featured images on single Posts
add_image_size( 'post-single', 1200, 300, true );
// Create a shortcode that outputs the URL of author page for the entry author outside the loop
add_shortcode( 'post_author_posts_link_outside_loop', 'sk_post_author_posts_link_shortcode' );
/**
* Produces the author of the post (link to author archive).
*
* Supported shortcode attributes are:
* after (output after link, default is empty string),
* before (output before link, default is empty string).
*
* Output passes through 'genesis_post_author_posts_link_shortcode' filter before returning.
*
* @since 1.1.0
*
* @param array|string $atts Shortcode attributes. Empty string if no attributes.
* @return string Shortcode output
*/
function sk_post_author_posts_link_shortcode( $atts ) {
if ( ! is_singular() ) {
return;
}
$defaults = array(
'after' => '',
'before' => '',
);
$atts = shortcode_atts( $defaults, $atts, 'post_author_posts_link_outside_loop' );
global $post;
$author_id = $post->post_author;
$author = get_the_author_meta( 'display_name', $author_id );
$url = get_author_posts_url( $author_id );
if ( genesis_html5() ) {
$output = sprintf( '<span %s>', genesis_attr( 'entry-author' ) );
$output .= $atts['before'];
$output .= sprintf( '<a href="%s" %s>', $url, genesis_attr( 'entry-author-link' ) );
$output .= sprintf( '<span %s>', genesis_attr( 'entry-author-name' ) );
$output .= esc_html( $author );
$output .= '</span></a>' . $atts['after'] . '</span>';
} else {
$link = sprintf( '<a href="%s" rel="author">%s</a>', esc_url( $url ), esc_html( $author ) );
$output = sprintf( '<span class="author vcard">%2$s<span class="fn">%1$s</span>%3$s</span>', $link, $atts['before'], $atts['after'] );
}
return apply_filters( 'genesis_post_author_posts_link_shortcode', $output, $atts );
}
<?php
// Add opening div.single-post-top for entry header + first para
add_action( 'genesis_before_content', 'sk_single_post_top_opening' );
function sk_single_post_top_opening() {
echo '<div class="single-post-top">';
}
// Add entry header before content
add_action( 'genesis_before_content', 'genesis_entry_header_markup_open' );
add_action( 'genesis_before_content', 'genesis_do_post_title' );
add_action( 'genesis_before_content', 'genesis_post_info' );
add_action( 'genesis_before_content', 'genesis_entry_header_markup_close' );
// Show first para before content
add_action( 'genesis_before_content', 'get_first_paragraph' );
/**
* Echo first paragraph from the current post.
*
*/
function get_first_paragraph() {
global $post;
$content = wpautop( $post->post_content );
$str = substr( $content, 0, strpos( $content, '</p>' ) + 4 );
// To remove images from the first paragraph
// $str = preg_replace( '/<img[^>]+\>/i', '', $str );
echo '<div class="first-para">' . $str . '</div></div>';
}
// If featured image is present, show it before content
add_action( 'genesis_before_content', 'sk_featured_image' );
function sk_featured_image() {
if ( $image = genesis_get_image( 'format=url&size=post-single' ) ) {
printf( '<div class="featured-image-single"><img src="%s" alt="%s" /></div>', $image, the_title_attribute( 'echo=0' ) );
}
}
// Remove first para from the content
add_filter( 'the_content', sk_remove_first_para, 20 );
function sk_remove_first_para( $content ) {
$content = preg_replace( '/<p.*?<\/p>/s', '', $content, 1 );
return $content;
}
// Remove entry header from its default location
remove_action( 'genesis_entry_header', 'genesis_entry_header_markup_open', 5 );
remove_action( 'genesis_entry_header', 'genesis_entry_header_markup_close', 15 );
remove_action( 'genesis_entry_header', 'genesis_do_post_title' );
remove_action( 'genesis_entry_header', 'genesis_post_info', 12 );
// Customize entry meta in the entry header
add_filter( 'genesis_post_info', 'sp_post_info_filter' );
function sp_post_info_filter( $post_info ) {
$post_info = '[post_date] by [post_author_posts_link_outside_loop] [post_comments] [post_edit]';
return $post_info;
}
genesis();
.single-post-top {
padding: 50px 50px 22px 50px;
overflow: hidden;
}
.single-post .content-sidebar-wrap {
background-color: #fff;
overflow: hidden;
margin-bottom: 40px;
}
.featured-image-single img {
vertical-align: top;
}
@media only screen and (max-width: 800px) {
.single-post-top {
padding: 0;
}
.featured-image-single {
margin-bottom: 28px;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.