- This topic has 5 replies, 2 voices, and was last updated 3 years, 7 months ago by Tom.
-
AuthorPosts
-
September 10, 2020 at 6:44 pm #1438738Espen
Hi,
I’m building a custom element in GP 3.0 Alpha 4 to display the meta information and featured image full width below the header and above the content and sidebar.
I’m using the generate_after_header hook in the GPP elements section with the following code:
<?php if ( is_single() ) : ?> <div class="page-header-content generate-page-header generate-content-header"> <div class="grid-container"> <div class="heading_wrapper"> <div class="nn-cat nn-single-cat"> <?php echo generate_do_post_meta_item( 'categories' ); ?> </div> <div class="above-header-title entry-title"> <h1><?php the_title(); ?></h1> <span> <?php generate_do_post_meta_item( 'author' ); generate_do_post_meta_item( 'date' );?></span> </div> </div> <?php if ( has_post_thumbnail() ) { ?> <div class="featured-image-in-header"> <?php the_post_thumbnail(); ?> </div> <?php } ?> </div> </div> <?php endif; ?>
It all works except the generate_do_post_meta_item( ‘author’ ) which does print out the surrounding classes but does not include the post author name itself.
Not sure if this is a bug or just me not doing it right but hoping you can help.
-Espen
September 10, 2020 at 7:53 pm #1438769TomLead DeveloperLead DeveloperHi there,
You won’t be able to get the author name outside the loop without some custom code.
Instead of using that function, try this:
global $post; $author_id = $post->post_author; printf( '<span class="author vcard" itemtype="http://schema.org/Person" itemscope="itemscope" itemprop="author"><a class="url fn n" href="%1$s" title="%2$s" rel="author" itemprop="url"><span class="author-name" itemprop="name">%3$s</span></a></span>', esc_url( get_author_posts_url( $author_id ) ), /* translators: author name */ esc_attr( sprintf( __( 'View all posts by %s', 'gp-premium' ), get_the_author_meta( 'display_name', $author_id ) ) ), esc_html( get_the_author_meta( 'display_name', $author_id ) ) );
September 11, 2020 at 3:35 am #1439069EspenAha, I see. Sorry about that. I considered whether it might be a loop thing but since it worked for comments, categories, and date I thought it would work for author as well.
Added your code and it works well. Thanks! 🙂
Just wondered if this is the recommended way to achieve the layout I am going for (see link in private information)?
The plan is to add the generate_after_header hook, to display meta information and header image.
Then use the new generate_before_do_template_part on is_single to filter the content and output only the_content.
add_filter( 'generate_do_template_part', function( $do ) { if ( is_single() ) { return false; } return $do; } ); add_action( 'generate_before_do_template_part', function() { if ( is_single() ) : ?> <article <?php post_class(); ?>> <?php the_content(); ?> </article> <?php endif; } );
Does this sound like the best course of action or is there a different way you would recommend?
Thanks again!
-Espen
September 11, 2020 at 9:43 am #1439634TomLead DeveloperLead DeveloperThe author functions are a little different for some reason – it’s tripped me up a few times before as well.
You can do that for sure.
Another way would be to just disable the default title and post meta – that way you’re outputting the content only without having to filter it.
September 11, 2020 at 10:24 am #1439677EspenThanks Tom,
Would there perhaps be a speed benefit to disabling the default title and post meta instead of filtering the loop?
I’m not quite sure how to disable the post title? I might be a little blind, but there seem to be options in the customizer for toggling off everything else in a blog post but not the title?
Thanks again!
-Espen
September 11, 2020 at 11:52 am #1439770TomLead DeveloperLead DeveloperNo speed benefit, just an alternative way to do it.
You can use a Layout Element with the Disable Elements module: https://docs.generatepress.com/article/layout-element-overview/
-
AuthorPosts
- You must be logged in to reply to this topic.