Help with re-coding a lost widget I had
-
Somehow through some recent troubles with hackers, I have lost a custom widget I had made and do not know enough programming to remember how I eventually did it.
On my main blog I used to have a widget in the side bar that displayed a “Haiku summary” of each post. I know I used the “PHP Code Widget” in the sidebar to accomplish this. For every post I also make a separate post with the category “Haiku.” Then afterward when I post the main post, I have a custom field called “Haiku” where I can enter the post ID of the accompanying Haiku post. This way every individual archive post also displays its “Haiku Summary” in the PHP code widget in the sidebar. And on the main index it would display the latest Haiku post in the widget. I somehow slogged my way through coding that a million years ago and have no idea how.
Or perhaps there’s some better way to do it. Basically I want the LATEST Haiku category post displayed in the sidebar on the index, and then for each individual entry to display its specific companion Haiku post. Any Help is GREATLY appreciated.
-
Okay I think I have MOSTLY reverse engineered it again. Using the PHP Code widget I have the following but have not yet figured out the last step as to how to insert the post ID from the custom “haiku” field I put on every entry.
<?php
$query = new WP_Query(array(
'category_name' => 'Haiku', // Replace with your category slug
'p' => 'your-post-id' // Replace with your specific post ID
));
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
the_content(); // Display the post content
}
}
wp_reset_postdata();
?>Hi @nos402 ,
Yes, the missing part is specifically retrieving the custom field value and replacing
your-post-idwith it.WordPress provides
get_post_meta()to read custom field values, andWP_Querysupportspfor loading a specific post by ID.You may check:
Finally got it after much poking around in the dark!
<?php
$query = new WP_Query(array(
'p' => ($value = get_post_meta(get_the_id(), 'haiku', true)) // Replace with your specific post ID
));
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
the_content(); // Display the post content
}
}
wp_reset_postdata();
?>
You must be logged in to reply to this topic.