22nd of Aug, 2007ce* in: ideas
Wordpress, however easy it is on the mind for non coders like me, is always going to come up short on the goods when turning a clients idea into reality. This article isn’t for those proficient in PHP to be honest. If you know PHP, you are well equipped to make wordpress do whatever you pretty much damn well like. If you like your full head of hair (and don’t fancy trading your design skills for learning PHP) the following may well help you keep it:
With the new version of wordpress 2.2 most of us who use it for client CMS would have welcomed an expanded Authors function that could work in the loop. More specifically the ability to make a page that doesn’t just list all the authors but a page that can pull together the individual profiles of authors and list them on a page.
Thankfully, someone has made this a reality. Hats off to Kaf Oseo, for this unlisted page on his website. http://guff.szub.net/source/page-authors-2.0.php The basic premise is to make a new “page” (write page) and select this as a template from the right hand side menu. I’m sure you know about pages but if you didn’t, making pages is a small matter of including a line of PHP at the top of your template file. Like this:
< ?php
/*
Template Name: All Authors Template (WP2)
*/
?>But more can be found on the wordpress codex as I’m sure you are aware.
There comes a nightmare in everyones wordpress building, of changing a front page to show different posts in different styles. In essence, for those with the logical PHP brains and magic underpants this is easy. After some hair pulling and a bit of research into wildly complex ways to make posts alternate background colours, a solution came it’s way. An avid streamliner of code, I got this down into a single loop to display 3 different styled posts, with any other posts using the default style you may have made. Actually the catalyst for this is that my client wanted a “new” post, above two other posts which needed their own wizardry to sit on the page, the two posts needed to be styled differently and the whole job lot had to come from one single feed: the loop.
Here’s the code, explanation below:
< ?php if(have_posts()) : ?>
< ?php while(have_posts()) : the_post(); ?>
< ?php if ( 1 == $i ) {
?>“style1″< ?php
} else if ( 2 == $i ) {
?>“style2″< ?php
} else if ( 3 == $i ) {
?>“style3″< ?php
} else {
?>“styleother”< ?php
} ?>
< ?php $i++; ?>< ?php endwhile; else: ?>
< ?php endif; ?>So why on earth would I not make a variable, say $thestyle, and call that in the div class? Well, the first post needed to have author info inside, and several other divs to make it fit into the design. As Wordpress treats all built-in functions as you-want-this-right-now they cannot be included into strings of HTML spliced with PHP called by PHP variables. The above will only give you a div. No content.
So what does the code really look like?
Here is a sample of style1, inserted to the above code would give me everything I’m looking for even though it’s not incredibly elegant. Bare in mind stringing together wordpress functions and HTML string with PHP separator dots (how you join functions and text) would print the wordpress functions first, then dump the HTML after it. Not what we want at all.
< ?php if ( 1 == $i ) { ?>
<div class="frontpageitem1">
<h1>< ?php the_title(); ?></h1>
<h2><b>< ?php the_time('y年m月d日'); ?></b>< ?php the_author(); ?></h2>
<br clear="all" /><img src=”wp-content/themes/topsecret/images/< ?php the_author(); ?>.jpg” class=\”face\” alt=”< ?php the_author(); ?>“/>
<div class=”item1holder”>
<div class=”item1quote”>
< ?php the_excerpt();?>
<p><b><a href=”< ?php the_permalink(); ?>“>つづぎを読む »</a></b></p>
</div>
<div class=”item1cap”>
</div>
< ?php } else if ( 2 == $i ) { ?>and so on.
I hope this helps.
To limit the amount posts you can see on any one page (this being home.php), a lean meat idea is to use the following PHP while describing your loop:
< ?php while(have_posts() and $i <= 3) : the_post(); ?>
Many people search for this functionality in wordpress, as by default wordpress relies on you to specify this in the admin, but it’s a wide brush to paint your multi template blog with. There are many plugins out there to do the same thing, however this is such a super simple elegant hands-on-in-control way.
Lastly, remember to put this before your loop:
< ?php $i = 1; ?>
and this before the end of your loop:
< ?php $i++; ?>
or none of it will work! (these are for counting the posts!)
I hope I’ve saved some hair pulling for you non programmer-programmers like me out there.
Many thanks to mr.Varin for opening my eyes.
Free cut-out and keep ghetto blaster for your desktop. Now you can relive the 80's dream, be the envy of your co-workers with this DIY Ghetto Blaster!
A set of vector landmarks, free for use on commercial or personal projects.
Doodled this a while back as a t-shirt idea, only recently found again.
yes that’s also possible if you want to style all of the posts differently but wouldn’t work if you only want to style the first three differently.
nice move though, appreciated!
Could you not use the method posted above but like this
?>”style s?”
that way all the post would use the “style” class but then also use “s1″ “s2″ etc, then in the stylesheet, only have s1, s2, s3 thus changing only the top 3?
well that is what I’m trying to say above, if you increment the number, you need to have as many styles as you have posts, if it’s done the way in my post, you have have the first 3 styled individually then style the rest with one base style. Thanks for the comment.
Hi, Thanks for the tips.
What should I do if I want different style for each alternate post
Example :
Post 1 - Style A
Post 2 - Style B
Post 3 - Style A
Post 4 - Style B
and so on.
Please advise. Thanks in advance
aNieto2k, Wednesday, August 22, 2007, 18:00
I think you can do it much better.
< ?php $i=1; if(have_posts()) : ?> <!– $i = 1 (the first number) –>
< ?php while(have_posts()) : the_post(); ?>
?>”style?< ?php echo i++; ? >”
< ?php endwhile; else: ?>
< ?php endif; ?>