Need a line or two of custom PHP for my design portfolio website. Could you be the King of Kings?
March 17, 2011 4:18 PM   Subscribe

Need a line or two of (I think simple?) custom PHP for my design portfolio website. Could you be the King of Kings?

I am working on a portfolio website for my music-related design stuff built on WordPress but hosted on my own site. Basically using WP as a CMS here.

I noticed that most of the pages are using php to build the page based on what category an image is in (Record Cover, Poster, etc)

I figure if its using that info, then I should be able to use the php to see that "Yes, this is a 'Poster' so lets insert this custom image that says POSTER in this div here" basically creating a slug dependant on the category that the piece falls in to.

Problem: no idea how I would write that. Any guesses?

Here is the site in question (its my first site that Im building pretty much on my own and I freely admit I dont know what Im doing so please be gentle) and its pretty embryonic right now, but it will give you the gen idea...

(a sample page where I would like to have a div above the poster displaying a custom POSTER image)

http://decabet.com/chk-chk-chk/

Thanks!
posted by Senor Cardgage to Computers & Internet (13 answers total) 1 user marked this as a favorite
 
So I'm no PHP person, but what you need is an if statement

It's basically like

if (condition = true) {

then do this thing that I'm doing
}

else if (this thing = true) {
then do this other thing
}

else {
do this thing instead
}

So the page on php.net is here, and you should be able to glean the syntax from that.

You also need to get the category out of Wordpress. In fact, this page gives you the exact code that you need. I think I googled for "Wordpress if category"

There are basically two ways of getting HTML out of it. You can either
  1. Enclose everything from "if" until { within a PHP block (that angle bracket, question mark and php thing), then put your html there, and then enclose the } to the next { in php and so on.
  2. Put the whole thing in one PHP block, and use echo 'some html goes in here'; to render each line.
Make sure you end each line with a semicolon - it's how PHP knows you've finished saying this thing and that you're moving onto the next one. You do the same thing in CSS.

I hope that helps! Someone who actually knows their stuff will probably show up soon and tell me I am an idiot (protip - I am!) but I'm pretty sure that's the right way of going about it.
posted by Magnakai at 4:55 PM on March 17, 2011


You might be able to just use Wordpress' body class for this, and a little css.

You probably have a body tag that looks like this:

<body>

Change it to:

<body <?php body_class(); ?> >

It may be dependent on your theme, but hopefully that gives you a decent list of classes in the body tag (you can view the source and you should see something like:

<body class="archive category category-poster">

You can then target items using css using something like (this would insert the poster icon left of your title text):

.category-poster .content_container_page h4.single_title { background: url('/wp-content/themes/CleanDessign/images/poster_icon.png') no-repeat 0 0; padding-left: 100px; }

-------------

Another useful function is Wordpress' post_class which works in a similar manner to the above. You have a line like:

<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

which will add classes similar to what I mentioned above, and can be targeted in a similar manner using css.

I think a combination of both functions might be what you're after.
posted by backwards guitar at 5:52 PM on March 17, 2011


Best answer: <?php if(in_category( 'posters' )){ echo '<div class="poster-image"></div>'; } ?>

Is that what you're looking for? The in_category function test to see whether the current post is in a certain category. Place the above line of code wherever you want the special div to appear.
posted by milquetoast at 7:22 PM on March 17, 2011


Er, tests to see. And the function reference is here.
posted by milquetoast at 7:23 PM on March 17, 2011


I just used that if(in_category( 'posters' )) tonight to add a category-specific div and it worked like a champ.
posted by shopefowler at 8:38 PM on March 17, 2011


Response by poster: That works like a charm!
Do I just stack a bunch of those in the code? One for each section?
posted by Senor Cardgage at 8:40 PM on March 17, 2011


Response by poster: YES! It works. Thanks!
posted by Senor Cardgage at 8:46 PM on March 17, 2011


You could also echo a div with a class for each category the thing is in.
posted by toomuchpete at 9:01 PM on March 17, 2011


Response by poster: No idea what that means lol
posted by Senor Cardgage at 9:09 PM on March 17, 2011


// Declare an array with a list of your categories that need headers, 
$special_cats = array("posters","brochures","billboards");
$sc_count = count($special_cats);

// step through these to test if they're true; note that if a post is in more than
// one category if will get more than one header. You can test for this if needs be.

// cycle through categories list
for ($z=0; $z<$sc_count; $z++) { 

    // does it match?
    if ( in_category($special_cats[$z]) ) { 

        // if so, print out a div with the classname of the match + "-image"
        echo('<div class="' . $special_cats[$z] . '-image"></div>');

    }
}

// if you need to test for multiple categories and only use the first one

// set a flag variable to 0; if we match we'll set it to 1
$cats_flag = 0;

for ($z=0; $z<$sc_count; $z++) {

    // here we're checking for the category AND to make sure we haven't already matched one
    if ( in_category($special_cats[$z]) && $cats_flag == 0 ) {

        echo('<div class="' . $special_cats[$z] . '-image"></div>');

        // we got a match, setting our flag to 1 will prevent any further divs
        $cats_flag = 1;
    }
}

posted by maxwelton at 10:16 PM on March 17, 2011


milquetoast - how did you manage to post up that code? The metafilter posting system seemed to strip out most of that stuff for me.
posted by Magnakai at 2:08 AM on March 18, 2011


He used the HTML PRE tag.
posted by kirkaracha at 7:15 AM on March 18, 2011


I used <code>, but <pre> works just as well. I reckon you have to use the escape character for the left angle bracket no matter what: &lt; in place of <.
posted by milquetoast at 9:10 AM on March 18, 2011 [1 favorite]


« Older Started a new relationship, now old flame wants to...   |   Mmmm... delicious ginger nicotine. Newer »
This thread is closed to new comments.