- This topic has 3 replies, 2 voices, and was last updated 3 years, 4 months ago by
Tom.
-
AuthorPosts
-
September 20, 2019 at 6:46 am #1014410
An Nguyen
Hi All,
I used the below code for displaying the most recent post of specific category (96) on my archive page (3486), but I got an issue that the feature-image display incorrectly. Its real image is different from this. (You can view link update and move to blog to see the real image.)
I need you help on this code.
add_shortcode( 'most_recent_hero_post_archives', function() { $latest_posts = get_posts( 'numberposts=1&category=96' ); $latest_id = $latest_posts[0]->ID; $date_format = get_option( 'date_format' ); setup_postdata( $latest_posts[0] ); add_filter( 'excerpt_more', 'tu_no_read_more', 1000 ); add_filter( 'excerpt_length', 'tu_change_excerpt_length', 1000 ); $return = '<h2>' . get_the_title( $latest_id ) . '</h2>'; $return .= '<div class="hero-date">' . get_the_date( $date_format, $latest_id ) . '</div>'; $return .= '<div class="hero-excerpt">' . get_the_excerpt( $latest_id ) . ' ...</div>'; $return .= '<a class="button hero-button" href="' . get_permalink( $latest_id ) . '">Read more</a>'; remove_filter( 'excerpt_more', 'tu_no_read_more', 1000 ); remove_filter( 'excerpt_length', 'tu_change_excerpt_length', 1000 ); wp_reset_postdata(); return $return; } ); function tu_no_read_more() { return ''; } function tu_change_excerpt_length() { return 35; } add_filter( 'pre_get_posts', function( $query ) { if ( $query->is_category( 96 ) && $query->is_main_query() ) { $query->set( 'offset', '1' ); } } ); add_filter( 'generate_page_hero_background_image_url', function( $image_url, $options ) { if ( 3486 === $options['element_id'] ) { $latest_posts = get_posts( 'numberposts=1' ); $latest_id = $latest_posts[0]->ID; $image_url = get_the_post_thumbnail_url( $latest_id ); } return $image_url; }, 3486, 2 );
September 20, 2019 at 7:06 pm #1014935Tom
Lead DeveloperLead DeveloperHi there,
In your background image url filter, you have this:
$latest_posts = get_posts( 'numberposts=1' );
Try this:
$latest_posts = get_posts( 'numberposts=1&category=96' );
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/
Ongoing Development: https://generatepress.com/ongoing-developmentSeptember 20, 2019 at 7:22 pm #1014942An Nguyen
Hi Tom,
The issue is solved.
Thanks Tom.
September 21, 2019 at 9:12 am #1015331Tom
Lead DeveloperLead DeveloperYou’re welcome 🙂
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/
Ongoing Development: https://generatepress.com/ongoing-development -
AuthorPosts
- You must be logged in to reply to this topic.