dynamic description of the last article with one image

Home » Snippets » dynamic description of the last article with one image
0

Created with:

Visibility: 

public

Creator: Jan

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php

<?php

namespace WPTurbo;

/**
 * Displays the latest article with its title, content, and featured image.
 *
 * This function checks for the latest published post and renders it on the page.
 *
 * @return void
 */
function wpturbo_display_latest_article(): void {
    // Retrieve the latest post data
    $latest_post = wpturbo_get_latest_post();

    // Check if the latest post is available
    if ( null !== $latest_post ) {
        // Render the post if it exists
        wpturbo_render_post( $latest_post );
    } else {
        // Render a message if no posts are found
        wpturbo_render_no_posts_message();
    }
}

/**
 * Retrieves the latest post.
 *
 * This function queries the database for the most recent published post
 * and returns its data.
 *
 * @return array|null The latest post data or null if no post found.
 */
function wpturbo_get_latest_post(): ?array {
    // Set the arguments for the query to fetch the latest post
    $args = [
        'numberposts' => 1,       // Limit to 1 post
        'post_type'   => 'post',  // Only fetch posts
        'post_status' => 'publish' // Only fetch published posts
    ];

    // Execute the query and store the results
    $latest_posts = wp_get_recent_posts( $args );

    // Return the latest post if available, otherwise null
    return ! empty( $latest_posts ) ? $latest_posts[0] : null;
}

/**
 * Renders the latest post.
 *
 * This function displays the title, content, and featured image of the given post.
 *
 * @param array $post The post data, which should include title, content, and ID.
 * @return void
 */
function wpturbo_render_post( array $post ): void {
    // Sanitize the post title for safe output
    $post_title = esc_html( $post['post_title'] );

    // Sanitize and format the post content for safe output
    $post_content = wp_kses_post( apply_filters( 'the_content', $post['post_content'] ) );

    // Get the featured image for the post
    $post_thumbnail = get_the_post_thumbnail( $post['ID'], 'large' );

    // Output the post title, featured image, and content
    echo "<h2>{$post_title}</h2>";
    echo $post_thumbnail;
    echo "<div>{$post_content}</div>";
}

/**
 * Renders a message when no posts are found.
 *
 * This function displays a user-friendly message indicating that
 * no posts are available.
 *
 * @return void
 */
function wpturbo_render_no_posts_message(): void {
    // Output a message indicating that no posts were found
    echo "<p>No posts found.</p>";
}

// Call the function to display the latest article on the page
wpturbo_display_latest_article();
				

This documentation will help other developers, or even yourself in the future, understand the code's intent and functionality more easily.

Register an account to save your snippets or go Pro to get more features.