Ashish Singh
Ashish Singh

Reputation: 135

How to extract and display a post by ID in WordPress?

So, I have this special page template in WordPress. I want to use it to display a handful of posts by ID (which I will specify).

Something like

<?php displayPost(10); ?>

and it will have the posts thumbnail, title and some values from the post meta which I will use.

After searching and sweating for hours, this is what I have

<?php
class episodeDetails
{   
    public $id;
    function episodeTitle(){
        $title = get_the_title($this->id);
        $mykey_values = get_post_custom_values( 'episode', $this->id);
        foreach ( $mykey_values as $key => $value ) {
        return $title." : ".value;
        } 
    }
   function episodeImage(){
        return get_the_post_thumbnail_url($this->id);
    }

   function episodeLink(){
        return get_the_permalink($this->id);
    }
    function episodeMp3(){
        $mykey_values = get_post_custom_values( 'enclosure', $this->id);
        foreach ( $mykey_values as $key => $value ) {
       return strtok($value, "\n");
        }  
    }
}

$episode = new episodeDetails;
$episode->id="480";
?>

<div class="list-videos">
<div class="list-videos-image">
<a href="<?php echo $episode->episodeLink(); ?>" title="<?php echo $episode->episodeTitle(); ?>"><img width="404" height="404" src="<?php echo $episode->episodeImage(); ?>" class="attachment-portfolio-thumbnail wp-post-image" alt="<?php echo $episode->episodeTitle(); ?>"></a>
</div>
<div class="list-videos-text">
    <a href="<?php echo $episode->episodeLink(); ?>" rel="bookmark" title="<?php echo $episode->episodeTitle(); ?>!"><?php echo $episode->episodeTitle(); ?></a><br><a class="button" href="<?php echo $episode->episodeLink(); ?>" rel="bookmark"><i class="fas fa-play"></i> Play Now</a> <a class="button" href="<?php echo $episode->episodeMp3(); ?>" download=""><i class="fas fa-download"></i> Download MP3</a>
</div>
</div>

Now, it does display what I want but I feel that it can be done in a better and easier way. My way would require me to copy and paste the whole block of HTML again and again for every additional post. I want to simplify it and I am looking whether there is a concise way instead of my long and complicated HTML part which I feel is recursive.

Thank you

Upvotes: 1

Views: 2025

Answers (1)

Zoli Szab&#243;
Zoli Szab&#243;

Reputation: 4544

To strictly answer your question: just use a foreach loop.

// Your class declaration to be put here...

// Then declare your IDs
$episodeIds = array(480, 481, 482, ...);

// And finally loop over them
foreach($episodeIds as $id) {

    $episode = new episodeDetails;
    $episode->id=$id;

    ?><div class="list-videos">
        <div class="list-videos-image">
            <a href="<?php echo $episode->episodeLink(); ?>" title="<?php echo $episode->episodeTitle(); ?>"><img width="404" height="404" src="<?php echo $episode->episodeImage(); ?>" class="attachment-portfolio-thumbnail wp-post-image" alt="<?php echo $episode->episodeTitle(); ?>"></a>
        </div>
        <div class="list-videos-text">
            <a href="<?php echo $episode->episodeLink(); ?>" rel="bookmark" title="<?php echo $episode->episodeTitle(); ?>!"><?php echo $episode->episodeTitle(); ?></a><br><a class="button" href="<?php echo $episode->episodeLink(); ?>" rel="bookmark"><i class="fas fa-play"></i> Play Now</a> <a class="button" href="<?php echo $episode->episodeMp3(); ?>" download=""><i class="fas fa-download"></i> Download MP3</a>
        </div>
    </div><?php
}

Or you can wrap the template part of the code in a function (like your example displayPost(...)) and then call that function within the loop.

// Your class declaration to be put here...

// The display method gets the $episode object as parameter
function displayPost($episode) {
    ?><div class="list-videos">
        <div class="list-videos-image">
            <a href="<?php echo $episode->episodeLink(); ?>" title="<?php echo $episode->episodeTitle(); ?>"><img width="404" height="404" src="<?php echo $episode->episodeImage(); ?>" class="attachment-portfolio-thumbnail wp-post-image" alt="<?php echo $episode->episodeTitle(); ?>"></a>
        </div>
        <div class="list-videos-text">
            <a href="<?php echo $episode->episodeLink(); ?>" rel="bookmark" title="<?php echo $episode->episodeTitle(); ?>!"><?php echo $episode->episodeTitle(); ?></a><br><a class="button" href="<?php echo $episode->episodeLink(); ?>" rel="bookmark"><i class="fas fa-play"></i> Play Now</a> <a class="button" href="<?php echo $episode->episodeMp3(); ?>" download=""><i class="fas fa-download"></i> Download MP3</a>
        </div>
    </div><?php
}

// Then declare your IDs
$episodeIds = array(480, 481, 482, ...);

// And finally loop over them
foreach($episodeIds as $id) {

    $episode = new episodeDetails;
    $episode->id=$id;
    displayPost($episode);
}

Upvotes: 1

Related Questions