How to get current page information (title, thumbnail and excerpt) outside of the loop?

Question

I’m new in developing with WordPress and I’m having trouble getting my website done for my blog and archive page. Since those pages have a header (featured-image, title and excerpt), I can’t seem to find a way to get the current page ID to show current page information outside of the loop.

Here is a small map of what I have :

  • Header
    — Current page title
    — Current page excerpt
    — Current page thumbnail
  • Loop
    — Blog post thumbnail
    — Blog post title
    — Blog post excerpt
    — Blog post date

I tried a lot of things. The only thing I was able to “fix” is the title of the current page using “single_post_title()” and for my archive page “post_type_archive_title()“. However, it brings a new problem for this. I saw that post_type_archive_title() and wp_title() for the page title in the head is taking the plural name of the archive instead of the singular one.

My main problem is that title, excerpt and thumbnail are showing the latest (most recent) post from the loop instead of the page we are currently on.

For the rest, here is my code :

page-featured-image.php

<?php

//Variables
$page_image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );
?>      
<!-- PAGE FEATURED IMAGE -->         
<section class="page-header"> 
    <div class="featured-image-container"> 
        <div class="featured-image" style="background-image:url(<?php echo $page_image[0]; ?>)"></div>                 
        <div class="img-overlay"></div>                 
        <div class="page-title">
            <?php if (is_page()||is_home()): ?>
            <h1><?php single_post_title(); ?></h1>
            <?php endif;
            if (is_archive()): ?>
            <h1><?php post_type_archive_title(); ?></h1>
            <?php endif;?>
            <?php if ( has_excerpt() ) : ?>
                <h2><?php echo get_the_excerpt(); ?></h2>
            <?php endif; ?>
        </div>                 
    </div>             
</section> 

home.php

<?php 
//Blog page template

get_header(); 

get_template_part( 'template-parts/header/navigation' );

get_template_part( 'template-parts/page/page-featured-image' );

?>

<section class="container"> 
    <div class="section-flex"> 
        <div class="title-section"> 
            <h2><?php _e( 'Aperçu', 'aurora' ); ?></h2> 
            <h3><?php _e( 'Articles récents', 'aurora' ); ?></h3> 
        </div>                 
        <div class="section-right"> 
            <?php get_template_part( 'template-parts/page/filter' );?>                     
        </div>                 
    </div>
       
    <?php 
    //Rewind index
    rewind_posts();

    $args = array(  
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => 15, 
        'orderby' => 'date',
        'order'   => 'DESC',
    );

    $loop = new WP_Query( $args ); 

    if ( $loop->have_posts() ){

        //Blog container
        echo '<div class="flex-container">';

        // Start loop
        while ( $loop->have_posts() ) {
            
            $loop->the_post();
            
            PG_Helper::rememberShownPost();

            get_template_part( 'template-parts/page/blog-post' );

        } 
        wp_reset_postdata();
        
        echo '</div>';
        
    }else{
        _e( 'Sorry, no posts matched your criteria.', 'aurora' );
    }
    
    ?>

</section>         
              

<?php get_footer(); ?>

Thank you in advance for your help

PS: If you want to know, yes I use the function to add excerpt to page in my functions.php file

0
nic 4 weeks 2021-05-16T14:10:55-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse