I'm wanting to display an image from a sprited image that will display on a single.php page, depending upon what category is checked when I go to add a new post when logged into the WP Admin.
See image here: http://www.cl.ly/3C0v0C2S2B0m2H1G212E
I'm talking about the vertical green flag on the right.
So, in essence, if I check the Category "Basketball" for the post titled "Balloon Round-Up", I'd like it to display the basketball image (shown in the screenshot linked above...) underneath the category heading (see screenshot...). Also, I'd also like to display a different image underneath the heading "Suggested Grades" depending upon what grade level I specify. The suggested grades options would be K-2, 3-5, 6-8, K-5, 3-8, 9-12, K-12.
In regards to the various categories, I have about 19 different ones, that would need to display 19 different images, depending upon what category I specify for the post. I can list those later I guess...
I know how to do sprited images, and position the images using CSS, but I need help with the Wordpress PHP part to where it will automatically display the correct image underneath the heading for the correct category. Not sure how I would display the Suggested Grades, since I probably would not create categories for those. So, if you have a better idea, I'd be up for a suggestion. Maybe if there was a custom post type option where underneath the post there would be a Suggested Grades heading with a text input field where I could simply type in "K-5", and then it would output the CSS class for that image. Possibly the same thing with the Category?
Let me know if this makes sense, or if this is too confusing on what I'd like to do. Regardless, whatever you do to accomplish this would need to output a CSS class for each image that is displayed.
Ivaylo Draganov answers:
Hello,
You can use categories for the skill and a custom taxonomy for the grades. Here's the code for creating the taxonomy(put it in <em>functions.php</em>:
<?php
//hook into the init action and call create_book_taxonomies when it fires
add_action( 'init', 'create_custom_taxonomies', 0 );
//create two taxonomies, genres and writers for the post type "book"
function create_custom_taxonomies()
{
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
'name' => _x( 'Grades', 'taxonomy general name' ),
'singular_name' => _x( 'Grade', 'taxonomy singular name' ),
);
register_taxonomy('grade',array('post'), array(
'hierarchical' => true,
'labels' => $labels,
));
}
Then use the template tag [[LINK href="http://codex.wordpress.org/Template_Tags/post_class"]]<?php post_class(); ?>[[/LINK]] in the <div> that wraps your post. That function will output HTML classes for the each post which you can then use for CSS styling.
There's no need to use conditional checks for each category. You could also use the function[[LINK href="http://codex.wordpress.org/Function_Reference/get_the_category"]] get_the_terms[[/LINK]] to create HTML elements for the skill and grade corresponding to the post. For example - create simple text <a> elements and then use CSS image replacement to mask those with the sprited images.
Let me know if you need more on this :--)
Spencer Barfuss comments:
Hey, thanks for the post. I like your option better. I'd love to use it, but my PHP skills are horrible. I could use some more help on this... ;)
Spencer Barfuss comments:
And when you provide the code, it needs to be placed within the "downflag" div that I have created.
See screenshot here: http://www.cl.ly/2q19380J1w362V0D1X1k
Ivaylo Draganov comments:
Here's what you can use to have the category and custom taxonomy* appear inside of div.downlag: [[LINK href="http://pastebin.com/QiR7vkDh"]]http://pastebin.com/QiR7vkDh[[/LINK]]
* assuming you have used the above code to register a custom taxonomy for posts. If correctly implemented it should create another meta box on the write/edit post screen where you can create the grades and attach them to posts
From the screenshot I can see that you're using one of Woo's themes so I believe they have <em>post_class</em> implemented already. Use Firebug(or similar dev-tool) to inspect the code of your pages to see whether post classes are applied.
Spencer Barfuss comments:
Ok, I pasted that code exactly as you have it within the "downflag" div in the single.php file, and it caused most of the layout to disappear. Any clue as to why?
Ivaylo Draganov comments:
Could be due to a PHP error. To enable debugging mode and see the error messages, add this line to your <em>wp-config.php</em>:
define( 'WP_DEBUG', true );
And try a slightly improved version of the code:
[[LINK href="http://pastebin.com/QiR7vkDh"]]http://pastebin.com/QiR7vkDh[[/LINK]]
You can see it in action on my sandbox site(look above the post title for the output):
[[LINK href="http://test.druuf.com/wordpress/?p=12"]]http://test.druuf.com/wordpress/?p=12[[/LINK]]
Spencer Barfuss comments:
Ok, that seemed to work, but that code works only for the category that I've posted, and is not displaying the Suggested Grades code. Is there a way to configure that as well?
Ivaylo Draganov comments:
Is there a menu item under posts in your admin area that reads "Grades"? If you've properly placed the code from my first answer, then that section should appear. Check out the screenshot: [[LINK href="http://awesomescreenshot.com/0b8eppy81"]]http://awesomescreenshot.com/0b8eppy81[[/LINK]]
Spencer Barfuss comments:
Hi, the grades custom post type and ability to check a grade level on the right hand side is not working, and is not showing like you have it in your screenshot. I copied and pasted your code within the single.php file. Here's what it looks like...
<?php
/**
* Single Post Template
*
* This template is the default page template. It is used to display content when someone is viewing a
* singular view of a post ('post' post_type).
* @link http://codex.wordpress.org/Post_Types#Post
*
* @package WooFramework
* @subpackage Template
*/
get_header();
?>
<!-- #content Starts -->
<?php woo_content_before(); ?>
<div id="content" class="col-full">
<div id="main-sidebar-container">
<!-- #main Starts -->
<?php woo_main_before(); ?>
<div id="main">
<div class="postdate"><span><?php the_time('n.j.y') ?></span></div>
<div class="downflag">
<?php
// set vars
$post_id = get_the_ID();
$html = '';
// get post taxonomies(skill & grade)
$skills = get_the_terms ( $post_id, 'category' );
$grades = get_the_terms ( $post_id, 'grade' );
// do for each category(skill)
if ( $skills && !is_wp_error( $skills ) ) {
foreach ( $skills as $term ) {
$html .= '<a href="' . get_term_link( $term->slug, 'category' ) . '" class="skill ' . $term->slug . '">' . $term->name . '</a>';
}
}
// and for each taxonomy(grade)
if ( $grades && !is_wp_error( $grades ) ) {
foreach ( $grades as $term ) {
$html .= '<a href="' . get_term_link( $term->slug, 'grade' ) . '" class="grade ' . $term->slug . '">' . $term->name . '</a>';
}
}
// print the results on screen
?>
<?php echo $html; ?>
</div>
<?php
woo_loop_before();
if (have_posts()) { $count = 0;
while (have_posts()) { the_post(); $count++;
woo_get_template_part( 'content', get_post_type() ); // Get the post content template file, contextually.
}
}
woo_loop_after();
?>
</div><!-- /#main -->
<?php woo_main_after(); ?>
<?php get_sidebar(); ?>
</div><!-- /#main-sidebar-container -->
<?php get_sidebar('alt'); ?>
</div><!-- /#content -->
<?php woo_content_after(); ?>
<?php get_footer(); ?>
Ivaylo Draganov comments:
The above seems correct. Make sure that you've placed this in your <em>functions.php</em>:
//hook into the init action and call create_book_taxonomies when it fires
add_action( 'init', 'create_custom_taxonomies', 0 );
//create two taxonomies, genres and writers for the post type "book"
function create_custom_taxonomies()
{
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
'name' => 'Grades',
'singular_name' => 'Grade',
);
register_taxonomy('grade',array('post'), array(
'hierarchical' => true,
'labels' => $labels,
));
}
Spencer Barfuss comments:
Ok, it works! However, I can't get it to hook with the CSS. For example, I just checked the K-2 category. I updated the post, reloaded the page, and it shows K-2 over the image, which is good! But... when I go into the CSS to remove the text by using text-indent:-9999em;
, it won't remove the text. When I highlight it in Firebug, it won't even show the class I've created for it. I even tried display:none;
, and still nothing.
I can remove the text though for the skills, like striking. So, I put into the CSS .striking {text-indent:-9999em;}
, and it works. It removes the text from the web page, only displaying the image I want. But for some reason, it's not working with the suggested grades.
Do you have any idea as to why it would be doing this?
Spencer Barfuss comments:
Ok, nevermind. There was a colon missing in the CSS. :) Thanks for your help! And congratulations!
Fahad Murtaza answers:
OK
You can use 19 different classes for each of your gallery. In each class, you can do the CSS for the sprited image
.class1{
.... code for sprite category 1
}
.class2{
... code for sprite category 2
}
and also for checking the current category
in_category('5')
Returns true if the current post is in the specified category id. read more
here, '5' is the id of the category you want to check
Note: Be sure to check your spelling when testing, "is" and "in" are a big difference.
OK so in single.php, use something like
<?php
if(in_category('2'))
{
// use CSS class class1 which shows the related part of sprited image
}
if(in_category('3'))
{
// use CSS class class2 which shows the related part of sprited image
}
Here 2 and 3 are the IDs for different categories.
You can read more documentation here
http://codex.wordpress.org/Function_Reference/in_category
Fahad Murtaza comments:
Please explain more about this part and I will update my answer
<blockquote>
So, if you have a better idea, I'd be up for a suggestion. Maybe if there was a custom post type option where underneath the post there would be a Suggested Grades heading with a text input field where I could simply type in "K-5", and then it would output the CSS class for that image. Possibly the same thing with the Category?
</blockquote>
Regards,
Fahd Murtaza
Spencer Barfuss comments:
So, I understand the logic behind what you posted, but what about the suggested grades? Should create categories for those? Or is there an easier way? If possible, I'd like the categories to be only for the skill (i.e. Basketball, baseball, etc.) and not for grade levels (K-2, K-5, etc.) which are displayed above the category heading.
Also, I understand the if(in_category('3'))
part, but if that's true, then what exactly would I post where you have "// use css class class2 which shows the related part of the spirited image"?
Fahad Murtaza comments:
Can you post the markup for the div that shows grades and the Category sprited image. Would be great if you can also share the sprite image so I can create an example for you.
// USe CSS class class2 means
in CSS, you will write something like this
.class2{
/*
Write the code for showing just a part of sprited image as you mentioned you already know the CSS part
*/
/*
You can use the CSS background property for this and background positioning etc
*/
}
For example, you want to use the CSS class above in a category with ID 2.
In single.php, the PHP for this CSS class will be something like
<?php
if(in_category('2'))
{
echo "<div class='class2'></div>";
}
?>
Fahad Murtaza comments:
The div I mentioned in the HTML above can be something like the following screenshot. If you are using a different HTML mark-up, you can provide that and I can code a quick example.
http://www.webpagescreenshot.info/img/989212-611201194127PM#
Fahad Murtaza comments:
About
<blockquote>So, I understand the logic behind what you posted, but what about the suggested grades? Should create categories for those? Or is there an easier way? If possible, I'd like the categories to be only for the skill (i.e. Basketball, baseball, etc.) and not for grade levels (K-2, K-5, etc.) which are displayed above the category heading.</blockquote>
You can use custom fields for the Grade levels where you enter the value for custom field i.e a custom field named grade for each post will hold the suggest grade
like
k-2,k-5 etc
and then in single.php
you can pull the custom field value and based on it, again use a custom CSS class to show a part of sprited image.
But one thing, I see you have sprited the image in a different manner. If I can have a look at the sprited image and the related markup in HTML, I can suggest the best solution possible.
Spencer Barfuss comments:
Ok, I can see now how you would do that using echo
, and I don't think I need to show you the sprited image. That's what I needed to see, and that makes enough sense for me.
But still not sure what the best way to show the image for the Suggested Grades...
Fahad Murtaza comments:
Its not necessary to use
echo
<?php
if(in_category('2'))
{
?>
<div class='class2"></div>
<?php
}
?>
About the grades, you need to decide if you want to use a custom fields for suggested grade, then it all becomes easy.
Some documentation can be found here.
http://codex.wordpress.org/Custom_Fields
Please ask if you need more help.
Regards,
Fahd Murtaza
Fahad Murtaza comments:
So you are using woothemes?
Spencer Barfuss comments:
Yes, I am...
Fahad Murtaza comments:
OK, I see you are going to use Ivaylo Draganov's solution; that seems a better choice.