开发者

What is the best way of retrieving a specific post in Wordpress?

I need to retrieve a specific post to display on my websites front page. To avoid hard coding the pos开发者_运维百科t ID, I've added a custom property where I add the post ID.

The following code displays the wanted post:

(The code is within the LOOP)

// Get content from specific post (in this case, Åpningstider post))
$openingHoursID = get_post_meta($post->ID, "apningstider", true);  

if (!empty($openingHoursID))
{
  $openingHoursPost = get_post($openingHoursID);
  $openingHours = $openingHoursPost->post_content;
}  
else  
  $openingHours = "Åpningstid ikke angitt";

<div class="openinghours"><?php echo $openingHours; ?></div>
  1. Is there a better / easier way?
  2. The output is striped for HTML. How can I maintain HTML?


Well, if you can write the content as a page instead, you can do: Admin -> Settings -> Reading -> Front page displays -> A static page.


You should consider putting this content in a Page instead of a Post, they're meant for this sort of thing.

But for what you're currently doing, you should just call query_posts() before the loop with that particular post ID, like query_posts('p='.$openingHoursID) and then use the_content() as normal to output the post with formatting intact.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜