Tips & Tricks
Calling custom fields for next/previous posts
Custom fields are definitely very useful and are used on many WordPress installs. Today I’m going to show you how to easily get custom fields values outside the loop.
With that said, I’d like to say I am using this snippet for a new theme I am building.
I want to pull a custom field from the “next” & “previous” post. So I used the following to do so:
<?php $previous_post = get_previous_post(); $next_post = get_next_post(); $image_prev = get_post_meta( $previous_post->ID, 'image', $single = true); $image_next = get_post_meta( $next_post->ID, 'image', $single = true); ?>
Simple. This is how I put it to use:
<?php if ( $image_prev != '' ) : ?> <img src="<?php echo FROSTY_SCRIPTS ?>/thumb.php?src=<?php echo $image_prev; ?>&h=50&w=169&zc=1&q=80" alt="" /> <?php endif; ?>
Tips & Tricks
Display custom url if comment authors url is blank
Here is a nifty trick for your comments.php
template. If someone comes to your site and leaves a comment but doesn’t leave a url back to there site, the default link that is shown in place of the php code comment_author_url
is the current page link.
That might not look good. So, here is a little trick that I just implemented into my site. Besides installing the twittar plugin and pulling Twitter avatars I wanted to use the image itself for the authors url like. But if the author doesn’t have a Twitter avatar or a Gravatar it will display a default Gravatar that I set. If the default Gravatar is shown, usually the author will not leave a url link.
I would like to link the image to Gravatar.com so that they can get an image attached to their email address. So.. here is the code:
<?php if($comment->comment_author_url != "") { ?> <a href="<?php comment_author_url(); ?>"><?php gravatar(); ?></a> <?php } else { ?> <a rel="nofollow" href="http://gravatar.com" title="Get a Gravatar today!"><?php gravatar(); ?></a> <?php } ?>
Tips & Tricks
Echo custom fields in any category
Here is a neat trick. Say you want to show a custom field in you post or in a certain categories post. There is a simple code you need to write in order to accomplish this:
<?php $image = get_post_meta($post->ID, "image", $single = true); ?> <?php if($image != '') : if(in_category(7)) { echo ''; } else { ?> <img src="<?php echo $image ?>" alt="<?php the_title(); ?>" /></a>
In the example above I am calling the variable $image
and telling the server that it equals the value of “image” inside get_post_meta
or “custom field” of the current post.
Then we are asking if that variable $image
doesn’t equal nothing or !=
and if it is in the category id of 7, echo what ever is in the single quotes (which is nothing in this example), otherwise show the <img>
and the value inside “image” which should be the location of the image.
Update: Thanks to Austin from PressedWords for pointing out his trick, from the comment below.
Tips & Tricks
Disable caching of your site or post
I talked about Disabling search engine on search pages in a previous post using the meta tag
. Today lets go over the web bots Cached copy of your site. If you are working on builder your blog, or have a temporary site up, use the following code:
<meta name="robots" content="noarchive">
This will tell any bot to follow your site, index it, but prevents a cached copy of this page from being available in the search results.
-
Tips & Tricks2 months ago
WordPress Security Hacks
-
Pages2 months ago
Write For Us – Guest Post
-
Tips & Tricks1 month ago
How to: show/hide a widget in WordPress with jQuery
-
Plugins1 month ago
Top Membership plugins
-
News5 months ago
How to: Show/Hide any div box with jQuery in WordPress
-
Tips & Tricks5 months ago
Limit the characters that display on the_title
-
Tips & Tricks3 months ago
Remove spaces when echoing the_title
-
Tips & Tricks4 months ago
Remove the title attribute using jQuery
eylültoprak
July 7, 2009 at 5:13 pm
Well, depending on how you can file attachments.
.-= eylültoprak´s last blog ..al senin olsun… =-.