My first question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
scriptacular
Forum Newbie
Posts: 6
Joined: Wed May 20, 2009 6:46 pm

My first question

Post 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
Last edited by Benjamin on Wed May 20, 2009 8:17 pm, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: My first question

Post 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:
scriptacular
Forum Newbie
Posts: 6
Joined: Wed May 20, 2009 6:46 pm

Re: My first question

Post 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?
scriptacular
Forum Newbie
Posts: 6
Joined: Wed May 20, 2009 6:46 pm

Re: My first question

Post 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";
    } ?>
Post Reply