PHP Help!

crazyflx

Elite Member
Joined
Nov 9, 2009
Messages
1,623
Reaction score
4,870
I'm using a theme on my wordpress blog called "scarlett"

You can view a working example of it here (it isn't mine): http://idonteatthat.com/

You'll see on the right hand side, directly above the video (Hitler wants subway??) there are four posts, each has a thumbnail picture next to the post title.

That thing rotates between all the posts a blog has on it, and automatically takes a picture from a custom field called "screen" (which the user fills in when making their post) and makes a thumbnail out of it.

Well, on my blog, I don't always use the custom field, so I have blog posts appearing in that sidebar (which is referred to as a "feature list" and uses the PHP file featlist.php) that don't have images.

I would like to make that sidebar only show posts IF the custom field "screen" was used while making the blog post, so that only posts with pictures appear there.

Here is the code for "featlist.php" :

Code:
<div id="postlist">

<ul class="spy">
<?php $my_query = new WP_Query('orderby=rand'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post();?>
<li>

<?php $screen = get_post_meta($post->ID, 'screen', $single = true); 

if ($screen !="")
{?>

<img src="<?php bloginfo('stylesheet_directory'); ?>/timthumb.php?src=<?php echo $screen; ?>&h=60&w=100&zc=1" alt=""/>
<?php
}
?>


<h2><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
<div class="fcats"><?php the_category(', '); ?> </div> 
<div class="auth"> Posted by <?php the_author(); ?> </div> 

</li>
<?php endwhile; ?>

</ul>
</div>
<div class="clear"></div>
 
Code:
<div id="postlist">

<ul class="spy">
<?php $my_query = new WP_Query('orderby=rand'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post();?>

<?php $screen = get_post_meta($post->ID, 'screen', $single = true); 

if(empty($screen)){
?>

<li>
<img src="<?php bloginfo('stylesheet_directory'); ?>/timthumb.php?src=<?php echo $screen; ?>&h=60&w=100&zc=1" alt=""/>
<h2><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
<div class="fcats"><?php the_category(', '); ?> </div> 
<div class="auth"> Posted by <?php the_author(); ?> </div> 
</li>

<?php
} //end if screen is empty
?>

<?php endwhile; ?>

</ul>
</div>
<div class="clear"></div>

Should work.
 
Tried it, but nothing changed. I appreciate you giving it a shot though man, thanks!
 
I'd be willing to pay $15 via PayPal for somebody to figure this out...
 
According to Wordpress' source code, the return value of get_post_meta() could be boolean false, an empty string, an empty array, or some kind of actual data. *sigh*

Right before your if ($screen !="") line, add this:

Code:
print_r($screen);

Then refresh until you see a blog post in the sidebar without an image. Copy and paste the text that is outputted directly above it and reply back with it. That will shed some light on the problem, I think.
 
Once you get what $screen returns from voyevoda input, change

if ($screen != "") to if ($screen != "" && $screen != "what screen returned") and that should omit the return from showing the what you don't want it to.

If $screen returns something different for the bad data each time, then figure out what is constant about it and do

$constInString = strpos($screen,"constant output");
if ($screen != "" && $consInString == false)

Credit definitely goes to voyevoda on this one. Other wise i would have moved on and not replied at all.
 
According to Wordpress' source code, the return value of get_post_meta() could be boolean false, an empty string, an empty array, or some kind of actual data. *sigh*

Right before your if ($screen !="") line, add this:

Code:
print_r($screen);
Then refresh until you see a blog post in the sidebar without an image. Copy and paste the text that is outputted directly above it and reply back with it. That will shed some light on the problem, I think.


Alright, what happens when I used that little piece of code is this:

When there IS a picture, the URL of the picture is displayed above everything else in the sidebar.

When there IS NOT a picture there is no text at all.
 
Once you get what $screen returns from voyevoda input, change

if ($screen != "") to if ($screen != "" && $screen != "what screen returned") and that should omit the return from showing the what you don't want it to.

If $screen returns something different for the bad data each time, then figure out what is constant about it and do

$constInString = strpos($screen,"constant output");
if ($screen != "" && $consInString == false)

Credit definitely goes to voyevoda on this one. Other wise i would have moved on and not replied at all.

I'm not sure what to put
Code:
&&$screen !="HERE"
because nothing appears.
 
Been a while since I've done a lot of wp work but shouldnt this work? Then again i might be way off, its been a while.

Code:
<div id="postlist">
<ul class="spy">

<?php
 $querystr = "
    SELECT wposts.* 
    FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
    WHERE wpostmeta.screen > '' 
    ORDER BY Rand
 ";

 $pageposts = $wpdb->get_results($querystr, OBJECT);

?>
 <?php if ($pageposts): ?>
  <?php foreach ($pageposts as $post): ?>
    <?php setup_postdata($post); ?>

    <li>
    <img src="<?php bloginfo('stylesheet_directory'); ?>/timthumb.php?src=<?php echo $screen; ?>&h=60&w=100&zc=1" alt=""/>
    <h2><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
    <div class="fcats"><?php the_category(', '); ?> </div> 
    <div class="auth"> Posted by <?php the_author(); ?> </div> 
    </li>
    </ul>
    </div>
    <div class="clear">
    </div>  

    <?php endforeach; ?>
    <?php endif; ?>
 
Been a while since I've done a lot of wp work but shouldnt this work? Then again i might be way off, its been a while.

I appreciate the attempt, but that didn't work. That made the sidebar disappear completely, moved everything else up and made the site just look generally all screwed up.
 
Alright, what happens when I used that little piece of code is this:

When there IS a picture, the URL of the picture is displayed above everything else in the sidebar.

When there IS NOT a picture there is no text at all.

In PHP, a string composed entirely of whitespace isn't considered "empty". It's possible that $screen is a single space character or something. Try this in the same spot I had you put the print_r($screen) above:

Code:
echo "<div class=\"bhw_test\">";
print_r($screen);
echo "</div>";

Then refresh until you get a blank image and look at the page source and search for "bhw_test". See if there is ANYTHING (including whitespace) between the div tags.

If so:

Code:
if(isset($screen) && $screen != false && !empty(trim($screen)))

Should work.
 
Last edited:
Try $screen != null because from what voyevoda said, "get_post_meta() could be boolean false, an empty string, an empty array", then null would be win, if that is the case.
 
Now the sidebar disappears and in it's place is the following message:

Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/thisweek/public_html/wp-content/themes/scarlett/featlist.php on line 9
 
you are missing a ; probably to finish the line.

Paste your code?

Thanks for your help guys, my knowledge of PHP is near nothing (obviously).

Code:
<div id="postlist">

<ul class="spy">
<?php $my_query = new WP_Query('orderby=rand'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post();?>
<li>

<?php $screen = get_post_meta($post->ID, 'screen', $single = true); 
echo "<div class="bhw_test">";
print_r($screen);
echo "</div>";
if ($screen !="")
{?>

<img src="<?php bloginfo('stylesheet_directory'); ?>/timthumb.php?src=<?php echo $screen; ?>&h=60&w=100&zc=1" alt=""/>
<?php
}
?>


<h2><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
<div class="fcats"><?php the_category(', '); ?> </div> 
<div class="auth"> Posted by <?php the_author(); ?> </div> 

</li>
<?php endwhile; ?>

</ul>
</div>
<div class="clear"></div>
 
echo "<div class="bhw_test">";

needs to be

echo "<div class=\"bhw_test\">";

Need to escape the " so it doesn't end the string prematurely.

You can also do ?> <div class="bhw_test"> <?php to bounce from php to html
 
Last edited:
This should work :)

Code:
<div id="postlist">

<ul class="spy">
<?php $my_query = new WP_Query('orderby=rand'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post();?>
<?php $screen = get_post_meta($post->ID, 'screen', $single = true); 

if ($screen !="")
{?>
<li>
<img src="<?php bloginfo('stylesheet_directory'); ?>/timthumb.php?src=<?php echo $screen; ?>&h=60&w=100&zc=1" alt=""/>

<h2><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
<div class="fcats"><?php the_category(', '); ?> </div> 
<div class="auth"> Posted by <?php the_author(); ?> </div> 

</li>
<?php
}
?>

<?php endwhile; ?>

</ul>
</div>
<div class="clear"></div>
If it did, PM me for paypal email. :D I could use the extra bucks.
 
if problem is not solved I can try to resolve this issue.. Please let me know..thanks
 
echo "<div class="bhw_test">";

needs to be

echo "<div class=\"bhw_test\">";

Need to escape the " so it doesn't end the string prematurely.

You can also do ?> <div class="bhw_test"> <?php to bounce from php to html

That worked perfectly.

However, what's strange, is this is what the source code shows now:


Code:
 <ul class="spy">
<li>

<div class="bhw_test"></div>

<h2>
 
Last edited:
change
if ($screen != "")

to

if (isset($screen) && $screen != false && !empty(trim($screen)) && $screen != NULL && $screen != "")
 
Back
Top