• Hi there,

    I have a PHP template that I am using for my homepage. I am planning on having a featured section which I would like to be controlled/managed by a specific post in admin.

    What would be the best way to do this?

    Can I just include a single post and define it by ID in my template file? I would only be wanting the title, image, category and short description.

    Any advice would be great, thanks!

Viewing 1 replies (of 1 total)
  • Yes, you can definitely include a single post in your PHP template and define it by ID. Here are the steps to achieve this:
    First, find the ID of the post you want to display. You can do this by going to the Posts section in your WordPress dashboard and hovering over the title of the post. The ID will appear in the URL at the bottom of your browser.In your PHP template file, use the get_post() function to retrieve the post by its ID. Here’s an example:

    <?php
    $featured_post_id = post_Id; // replace with the ID of your featured post
    $featured_post = get_post( $featured_post_id );
    ?>
    <div class="featured-section">
        <h2><?php echo $featured_post->post_title; ?></h2>
        <?php echo get_the_post_thumbnail( $featured_post->ID ); ?>
        <p><?php echo $featured_post->post_excerpt; ?></p>
        <p>Category: <?php echo get_the_category_list( ', ', '', $featured_post->ID ); ?></p>
    </div>
    
    • This reply was modified 1 year, 8 months ago by Aqsa Qamar.
    • This reply was modified 1 year, 8 months ago by bcworkz. Reason: code format fixed
Viewing 1 replies (of 1 total)
  • The topic ‘Best way to display a single post in a template file’ is closed to new replies.