Jump to content

Problems with conditionals in PHP


jameskyle

Recommended Posts

I'm new to PHP and I'm having trouble doing what I'd like to do with it. I'm working with WordPress and the Thematic framework, creating a child theme to exhibit the artwork of a friend.

 

The background to this project, my working with PHP can be found here. I'm basically trying to create a function that applies CSS to a particular part of the site (in this case the div id "hentry",) changing the background image of the div to the featured image of that particular post.

 

The function should also detect whether the page is showing a single page or post, so it should style these pages differently by making the background image = none.

Link to comment
https://forums.phpfreaks.com/topic/221213-problems-with-conditionals-in-php/
Share on other sites

Though I am not 100% sure what they are off the top of my head I know WP has built in functions for just that. To include the image on the roll if there is one, as well as in the post pretty much the way your talking about. I'd say look into the WP FAQ, and chances are you will find exactly what you want or similar in nature that can be altered further.

 

I only say this cause I'm new to working with Wordpress and customizing it, and I have a large project coming up soon thats going to throw me neck deep into it, and I have been looking into similar functions with WP.

As I see it I have two problems. The first is writing a conditional statement that not only reports no errors (that's easy!) but which does what I'd hoped. What I have:

if (is_single() || is_page()) {
echo "single post or page";
} else {
echo "post background is featured image";
}

This doesn't work. It always runs the else section, never tripping any of the conditions to return a single post or page echo, even on pages that display a single post or page. I presume either the if condition is incomplete and should read something like is_page = true or something similar. On testing that didn't seem to work, but my PHP knowledge is minimal so I may not have tested adequately. I may have to reference a specific part of the page being displayed, though I'm not sure.

 

This PHP lark is a little tougher than I thought it'd be, for sure!

I'll post again with the progress I've made.

 

Using the echo command as I would a trace in Actionscript I've made my conditional work.

function set_post_background() {
//then define the action to take:
if (is_page() || is_single()) {
    	echo "single post page";
} else {
	echo "background to posts should be featured image";
}
}
//now we can add our new action to the appropriate place like so:
add_action('wp_head', 'set_post_background');

I've changed is_post() to is_single(), as is_post no longer works in WordPress 2.9+.What a pain, but I'm sure it's for good reason. I've also added the add_action line, which seems to be necessary. I don't know much about PHP but I assume that this is placing my echo code in the header of the page and not in the head tags of the HTML.

 

Here's the resources I used to get this working. It's not like it just popped into my head or anything!

When I was beginning to dount that || meant AND... - http://www.w3schools.com/php/php_operators.asp

Looking for anything that could help in the Thematic guide Wiki - http://themeshaper.com/thematic/guide/?page_id=10

The ever useful WP Codex - http://codex.wordpress.org/Conditional_Tags#A_Single_Post_Page

 

Now I just need to add the code to make the background image of each hentry div the featured image of the post it represents. I don't think the code on the Thematic Guide Wiki will cut it, since it refers to a static stylesheet, while I'm trying to make a more dynamic beastie.

 

Thematic Guide Wiki code:

 

//lets give our new action a name

function childtheme_css() {
//then define the action to take:
if (is_page('82')) {?>
    <link rel="stylesheet" type="text/css" href="<?php echo
bloginfo('stylesheet_directory') ?>/homepage.css" /> <?php }
//end action:
}
//now we can add our new action to the appropriate place like so:
add_action('wp_head', 'childtheme_css');

 

I suppose I could just add the featured image to all posts then hide it on those that it's not meant to be displayed on. That's a less elegant solution though, and I'd rather avoid hiding images since I assume that despite being hidden it'll still try to download them, whereas in my preferred solution they won't be a problem.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.