Site logo

Archived topic

Single Post Meta under the Title in the Hook

13 replies · Started by Artem on December 14, 2019

Viewing posts 1–14 of 14

Hi,
I'm trying to get the following layout for a single post:

Post Title
Author | Date | Category
Featured image
Content

I show Featured Image via Header Element.
The Post Title I show via Hook Element using this PHP <?php the_title( '<h1>', '</h1>' ); ?>
Would you be so kind to explain what PHP code should I add to my Hook Element in order to show inline:
Author | Date | Category

I tried:
<?php the_category( ' ' ); ?> - It shows the category.
However
<?php the_date( 'Y-m-d', '<h2>', '</h2>' ); ?>
or
<?php the_author(); ?>
don't show anything.

Thanks a lot.

Thanks, Leo.
This filter works fine by putting all the meta in one line.
However:
1 - with this filter, I still have the category displayed (duplicated) at the end of a single post.
2 - another issue is that with this filter I can't have my featured image full width, with my content "contained". That's why I was thinking about the hook. In fact, I took this idea from the Merch site in your site library.
3 - how can I have separator like this | between different meta: Author | Date | Category?
4 - one more question. What is the preferable way of tweaking the GeneratePress style/layout: code snippets or hooks? I was thinking that it's preferable to use hooks (whenever possible) instead of using third-party plugins.

Thank you.

1. You would need a second filter:
https://docs.generatepress.com/article/generate_footer_entry_meta_items/

2. So you'd like to duplicate the single post layout from Merch?

If so this page explains it in details:
https://docs.generatepress.com/article/generate_footer_entry_meta_items/

3. Let's solve 1/2 first.

4. That depends on what you are trying to add. If it can be done with hooks then definitely use the hook element module. But if it's a filter or custom function then Code Snippets or a child theme is needed:
https://docs.generatepress.com/article/adding-php/

Thank you, Leo.
Definitely, I didn't explain clearly what I need. Sorry about that. I'll try once again.
1 - I needed to remove category at the end of a single post as I have it already at the beginning of the post just below the Title.
I found the filter on your forum and it wors fine:

add_filter( 'generate_footer_entry_meta_items', function( $items ) {
    return array_diff( $items, [ 'categories' ] );
} );

2 - Yes, I try to do the single post layout similar (but not identical) to Merch.
Something like this: http://prnt.sc/qb8gg3
I need my title and meta above the featured image (full width) and my single post content width - 700px. So, what's the best way to do this? How to adjust the width of the featured image independently of the content width?
Thanks.

So, what’s the best way to do this? How to adjust the width of the featured image independently of the content width?

This isn't very easy as the featured image is inside the content container with the same width.

The workaround is to set the single post container layout to be the wider/featured image width with a layout element:
https://docs.generatepress.com/article/layout-element-overview/#content-1

Then wrap your single post content in like this:

<div class="single-post-content-container">
    post content
</div>

Then target it with CSS;

.single-post-content-container {
    max-width: 700px;
    margin-left: auto;
    margin-right: auto;
}

I also linked you to the wrong page in the previous reply. David explained the single post layout for Merch as well:
https://gpsites.co/merch/the-single-post/

Let me know if this helps at all :)

Thanks for the reply, Leo.
Anyway, I still don't understand why these two functions: <?php the_title( ); ?> and <?php the_category( ); ?> work inside the Hook, and these two <?php the_date( ); ?> or <?php the_author(); ?> don't work.
I still need to show the post author and the post date inside the hook. Which function should I use?
Thank you.

Hi there,

You could try these functions:

<?php generate_do_post_meta_item( 'date' ); ?>
<?php generate_do_post_meta_item( 'author' ); ?>

Thank you, Tom.
The function for the date <?php generate_do_post_meta_item( 'date' ); ?> works fine. However, the function for the author <?php generate_do_post_meta_item( 'author' ); ?> gives me just "by". No author name displayed.

What hook are you adding it to?

Hook after_header
Execute PHP - on
Priority - 5
Display on all posts

Ah, since it's outside the loop, you need to specify the author ID.

For example, this is the code we use in the Header Element:

<?php
global $post;
$author_id = $post->post_author;

$author = sprintf( '<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 ) ),
    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 ) )
);

echo $author;
?>

Great. This works fine.
Many thanks.

You're welcome :)

This archived topic is closed to new replies.