Page 1 of 1

My first question

Posted: Wed May 20, 2009 8:16 pm
by scriptacular
Hello it's my first question so be gentle. It is wordpress related so here it is...

This code is a way to show a list popular posts without a plugin...

Code: Select all

<li><h3>Popular Posts</h3>
<ul class="bullets">
<?php
$popular_posts = $wpdb->get_results("SELECT id,post_title FROM {$wpdb->prefix}posts ORDER BY comment_count DESC LIMIT 0,10");
foreach($popular_posts as $post) {
    print "<li><a href='". get_permalink($post->id) ."'>".$post->post_title."</a></li>\n";
}
?>
</ul>
</li>
I'm trying to properly insert this code

Code: Select all

<div style="float:left; margin-right: 5px"><img src="<?php $key="fav"; echo get_post_meta($post->ID, $key, true); ?>" /></div>
between the <li> <a href tags but i just can't seem to escape it correctly. ie..

Code: Select all

<li><h3>Popular Posts</h3>
<ul class="bullets">
<?php
$popular_posts = $wpdb->get_results("SELECT id,post_title FROM {$wpdb->prefix}posts ORDER BY comment_count DESC LIMIT 0,10");
foreach($popular_posts as $post) {
    print "<li><div style=\"float:left; margin-right: 5px\"><img src=\"<?php $key=\"fav\"; echo get_post_meta($post->ID, $key, true); ?>\" /></div><a href='". get_permalink($post->id) ."'>".$post->post_title."</a></li>\n";
}
?>
</ul>
</li>
When done properly it should show a small image from a custom field right in front of the title.
I've tried and tried to no avail :banghead: any help would be greatly appreciated

Re: My first question

Posted: Wed May 20, 2009 9:21 pm
by JAB Creations
A good firm warning: WordPress's PHP code is utter crap. I'm hardly the most experienced here at the forums but I've used WordPress for a little over a year so your frustrations are understandable though do not let the horrid implementation of PHP by WordPress give you a bad impression of PHP itself which is a most awesome programming language! :)

You should post the error message you get, check your access logs for HTTP 404 codes, and do other debugging first. If you get an error take the time to look the core error message up online and it should give you hints as to what is going on.

Also use [syntax=php]<?php /* php code here */[/syntax] for your code to make it easier to read on the forums please. :wink:

Re: My first question

Posted: Wed May 20, 2009 10:47 pm
by scriptacular
Thanks for the response JAB and sorry for using the wrong tags :? and yes php seems to be a great programming language! As for my question i guess i'm not explaining it correctly. If I do it like this..where i give the img source an absolute path like below

Code: Select all

<?php $popular_posts = $wpdb->get_results("SELECT id,post_title FROM {$wpdb->prefix}posts ORDER BY comment_count DESC LIMIT 0,10");
foreach($popular_posts as $post) {
    print "<li><img src=\"http://localhost/wordpress/wp-content/u ... ultfav.gif\"><a href='". get_permalink($post->id) ."'>".$post->post_title."</a></li>\n";
} ?>
I get this Image with the camera next to the title just like i would if i had styled the <li> tags using css. When I do i do it this way

Code: Select all

<?php $popular_posts = $wpdb->get_results("SELECT id,post_title FROM {$wpdb->prefix}posts ORDER BY comment_count DESC LIMIT 0,10");
foreach($popular_posts as $post) {
    print "<li><div style=\"float:left; margin-right: 5px\"><img src=\"<?php $key=\"fav\"; echo get_post_meta($post->ID, $key, true); ?>\" /></div><a href='". get_permalink($post->id) ."'>".$post->post_title."</a></li>\n";
} ?>
i get this Image no image. The errors only show up when i try different ways of escaping it, more of a trial and error method which i know are probably incorrect, more like a shot in the dark. When i view source sometimes it looks like <li><img src="" sometimes its <li><img src"fav" sometimes it's <li><img src=" />" i can even get the correct path to show up, but not the image. I can usually get it right after a few tries but this time i'm stumped :(

p.s. did i do the tags right this time?

Re: My first question

Posted: Thu May 21, 2009 12:56 am
by scriptacular
Got a bit of help and figured it out. I figured it out a while ago except I was echoing the wrong variable. The correct code is

Code: Select all

<?php $key="fav"; 
$varImage = get_post_meta($post->ID, $key, true);   
$popular_posts = $wpdb->get_results("SELECT id,post_title FROM {$wpdb->prefix}posts ORDER BY comment_count DESC LIMIT 0,10");
foreach($popular_posts as $post) {
print "<li><img src=\"". $varImage ."\" />&nbsp;&nbsp;&nbsp;<a href='". get_permalink($post->id) ."'>".$post->post_title."</a></li>\n";
    } ?>