Thank You. great trick…
After a short break of writing on WordPress and on the request of my subscribers i decided to make a list of useful WordPress tricks and you are probably already using some of them, if not going to when making a WordPress theme. As you already know WordPress is one of the most popular blog platforms these days and it has grown so powerful that you can use it also use as Content Management System (CMS).
If you are new to WordPress and blogging itself you should read "Before you start blogging" article and go with some free themes for now, which you can find here in free themes directory.
<?php $category = get_the_category(); echo $category[0]->cat_name; ?>
So this grabs a single category name (it's not printing it just keeps it for now) and I've been using it a lot while developing themes
<?php /* Template Name: Free Themes */ ?> <?php get_header(); ?> //the loop <?php get_footer(); ?>
You should be familiar with this it makes a custom page template which you have to assign to some page after creating it. I've written a bit of it on how to make a archive page article and you can read more about it here
With query_posts()
<?php query_posts('cat=3&showposts=1'); ?> <?php while (have_posts()) : the_post(); ?> <?php the_excerpt('Read the rest of this entry »'); ?> <?php endwhile; ?>
query_posts() is very useful and you can use it where ever you want, display latest posts (<?query_posts('showposts=10');?>) for example or client testimonials, exclude a specific category (<?php query_posts('cat=-3'); ?>) etc...
With new WP_Query()
<?php $recent = new WP_Query("page_id=2&showposts=1"); while($recent->have_posts()) : $recent->the_post();?> <?php the_excerpt('Read the rest of this entry »'); ?> <?php endwhile; ?>
You'll notice these two are very similar so what ever you feel like using, i personally use the first one links and the second one for content. I've used it in my "Tabbed Featured Post" tutorial, click here to see how it looks like.
The php solution
<?php $i=1; ?> <?php while (have_posts()) : the_post(); ?> <?php if ($i == 1) { ?> <div id="post-<?php the_ID(); ?>" class="featured"> featured content </div> <?php } else { ?> <div id="post-<?php the_ID(); ?>"> rest of content </div> <!-- post --> <?php } ?> <?php $i++; ?> <?php endwhile; ?>
I think it's pretty much clear, it changed first post into a different div style (you can use here what ever you want of course). I've used this in my Solemnity free WordPress theme and if you would like to see how it looks click here for demo page. The only thing i don't like about this way is that your main loop in this case has to be in seperate div, you know you can't have anything between featured post and rest of posts (for example sidebar etc...) like here on Stylized Web. So becarefull how you will code it into html first
<?php include (TEMPLATEPATH . '/sidebar2.php'); ?>
If you need to include some other file than <?php get_sidebar(); ?> for example different sidebar or footer file etc...
<?php if ( is_front_page() ) { include (TEMPLATEPATH . '/home1.php'); } else { include (TEMPLATEPATH . '/rest.php'); } ?>
Don't think it needs some explanation, this also could be used for featured content/post and here are some other Conditional Tags:
is_home(), is_category(), is_archive(), is_search(), is_single(), is_date(), is_404(), etc...
In your index.php file, look for this bit of code: <?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?> Right before that line, add this code: <?php query_posts($query_string . “&order=ASC”) ?>
If you are the old-fashion guy and like the old ones on the top
Bad code used in title tags or search templates: <?php echo $s; ?> as it allows malicious code injection. You should use this one <?php echo wp_specialchars($s, 1); ?>
<?php $comments = array_reverse($comments, true); ?> <?php foreach ($comments as $comment) : ?> content here <?php endforeach; ?>
<?php $posts=query_posts($query_string . 'posts_per_page=10&offset=2'); while (have_posts()) : the_post(); ?> this will exclude first two posts <?php endwhile; ?>
Thank You. great trick…
Really nice set of tricks for WP. Thx!
I would like to know, would htmlentities() instead of wp_specialchars() work fine too?
Thanks for sharing these solutions, and congrats for your blog design.
Great article?
You mentioned you using query_posts() for grabing links..
How do you grab a specific link category and display those links in the footer?
Nice Article
Thanks you very much
Cezar,
please give us a more detailed explanation,tell us what part of this article are you referring to and past your code here
great trick,
i have one question though. why cant i get the tags to show.
i added in the loop but they dont show. are they supposed not to work? or am i doing something wrong?
thanks,
C
you are welcome ![]()
Dejan Cancarevic “Max,
Try this http://stylizedweb.com/2008/03/01/syntax-highlighter/“
Thanks for the link!
you can find it here
http://stylizedweb.com/2008/06/20/grab-data-from-wordpress-database/
You, my friend, are a champion! I know this might be pushing the envelope, but WP_Query() is restricted to posts, or can I get a list of recent comments too in a similar way?
Harley,
Sure it is, use WP_Query() as i says in text, so try this
<?php $recent = new WP_Query(”showposts=5″); while($recent->have_posts()) : $recent->the_post();?>
<li><a href=”<?php the_permalink() ? rel=”nofollow”>” rel=”bookmark” title=”Permanent Link to <?php the_title(); ?>”><?php the_title(); ?></a></li>
<?php endwhile; ?>
Hey, I’m just having a bit of trouble with the query_posts():
I want to know if it’s possible to have such content needed (I need a list of post titles) BEFORE the main loop? I have a section in my header that I wanted to include recent posts in (without using a plugin), however when I use the exampled ‘query_posts() code, it shows what I want up top in the header, but then the content in the main loop is whatever was specified in the query_posts(), which for example was just the 1 post from a certain category.
Is it possible to ‘end’ the use of the query_posts() for the header only? When I pasted the code AFTER my main loop, it worked! I’m confused as to why this is so, maybe you could shed some light on it for me!
Thanks a lot, and fantastic post by the way. Learnt some cool stuff!
~Harley
Max,
Try this http://stylizedweb.com/2008/03/01/syntax-highlighter/
Thanks for the post! Helped me a lot.
Just a side-question, how did you get the code displayed? What plugin? Thanks
Max
really cool tips thanks! ![]()
Karen,
go ahead and post your question to
http://stylizedweb.com/knowhow
Dejan, nice job! I’m new to Wordpress (& am not a template designer) . .. am wondering how I can change the link text color from white to black? The white just doesn’t show up very well . . . I’m using the Sweet Sunset Wordpress Theme. Thanks for any help you can give!
Very useful tips, thanks, Dejan!
Thanks for sharing these solutions, and congrats for your blog design.
All,
Thank you for the good words, i really appreciate it! ![]()
Thanks Dejan ::)
Woot! Congrats!
i come from chinese, and i like your design. i have use it in my second blog, thanks, it look great.
Awesome site!
This are all some pretty useful tips. I am going to add a link to this post and your site on my blog.
Cool. I’m gonna use these!
thanks for this good article..
Really nice set of tricks for WP. Thx!
Thx, Dejan
These tips are really great.
Bingu,
I’ve updated the post with it
How to reverse comment order?
I would like to know, would htmlentities() instead of wp_specialchars() work fine too?
just wanna touch the include specific files tips for sidebar
if you have additional sidebar like for single or archive you can do it like the following (WP2.5.x)
get_sidebar($name); // arg $name is sidebar-”name”.php
the function will look for sidebar-single.php (inside theme dir) and load it if exists otherwise it will load the default sidebar.php.
you can include section base sidebar base on wordpress hierarchical order. below is just a simple example (need to tweak it base on which section came first thought)
if (is_single()){
get_sidebar(’single’); // include /theme/sidebar-single.php
} elseif (is_archive()){
get_sidebar(’archive’); // include /theme/sidebar-archive.php
} else {
get_sidebar();
}
Hey, WP cut off my code!
Here’s the code: http://paste.ubuntu.com/20100/
heh yea sorry i hate when WordPress does this.. ![]()
Thanks Dejan.
Only a correction:
<a class="continuelink" href="" rel="nofollow" title="Continue reading ">… continue reading
johnbillion,
thanks i didn’t know about that one
Teli Adlam ,
I’ve been using is_home for a while now so thanks for the heads up!
Thanks for the tricks. Stumbled. Will come handy anytime.
great infos about wp!
Nice write up. Picked up a few ideas for use in future themes, so thanks for taking the time to compile it.
You may want to add a note about is_home as it’s been slightly tweaked for version 2.1.x and up. (Basically, is_page(’home’) seems to be the preferred conditional tag when working with newer versions of WordPress.)
Dejan,
Instead of wp_specialchars() I use attribute_escape(). There is no difference to the output, but attribute_escape() is preferred as it passes the output through the attribute_escape filter first.
just add manually
<a class=”continuelink” href=”<?php the_permalink(); ? rel=”nofollow”>” title=”Continue reading <?php the_title(); ?>”>… continue reading</a>
Pardon: inside the parenthesis in the_excerpt() there is ‘Read the rest of this entry’
Here’s my code in my sidebar: http://paste.ubuntu.com/20006/
Hvala!
Steven,
Thanks buddy
Aldo,
Go ahead and past your code here and we’ll see what is it
Nice resource for all WP theme designers.
I’m ashamed to admit I’ve never heard the last one before. It’s especially troubling because it’s the one where my ignorance could have negative consequences.
Nice tricks!
I’d like to know why — using ‘The featured post’ — the line ‘Read the rest of this entry’ is not displayed in my sidebar.
Hi,
really good article
have a nice day
Chris
Andrew,
heh i’m glad you like it
If you have some PW issues or questions go ahead and post them b/c that’s how i’ve written this article - people asked questions, so i’ll be happy to help..
Ivan,
I think so too ![]()
Been using some of them for long time now, good to see them in one place ![]()
Excellent! I can’t tell you how many PHP 101 sites I have had to sift through to get this information (I’m much more a designer than programmer). -If only I would have found this post earlier!
heh i’m not using any of them, shame on me ![]()
really useful info, thanks!
on August 23, 2008
Thank you.