Get post having a specific custom field and a specific value

December 26, 2008

How to get a list of post having a specific custom field and a specific value? Here’s how to do it. we just have to verify if the custom field is set and if its value is the one we expect. If yes, we display the post info. <?php if (have_posts()) : while (have_posts()) : the_post(); $customField = get_post_custom_values("img"); if ( (isset($customField[0])) && ($customField[0] == “myValue”) ) { //Custom field have myValue as a value, display info ... [Read the full story]

Retrieve images in wordpress post content

December 23, 2008

How to get or count the images contained within the content of a post? To achieve this, simply paste the following code on one of your theme files. It will search for all images contained in the post content, and display it.   <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <?php $szPostContent = $post->post_content; $szSearchPattern = '~<img [^\>]*\ />~’; // Run preg_match_all to grab all the images and save the results in $aPics preg_match_all( $szSearchPattern,... [Read the full story]

Embed Adsense anywhere on your posts using shortcode

December 23, 2008

Do you ever wanted to get a total control over the way your adsenses ads are displayed? In the following text, I’m going to teach you how to be able to use adsense only in the post you want and display the ads where you want. To achieve this aim, we just use shortcode of wordpress. First, you have to add the following code to your function.php file. Don’t forget to change the adsense code. function showads() { return 'your adsense code'; } add_shortcode('adsense', 'showads'); Once you saved the functions.php file,... [Read the full story]

Use two or more loops without duplicate wordpress posts

December 23, 2008

How to use multiples loops without having the duplicate wordpress posts? There’s probably many other ways to achieve the same effect. But here’s a pretty nice way to avoid duplicate posts in two or more loops. To avoid wordpress post duplication, we started by creating a php array. And adding the IDs of the posts we get in the first loop to that array: <h2>Loop NO1</h2> <?php $ids = array(); while (have_posts()) : the_post(); the_title(); ?> <br /> <?php $ids[]= $post->ID; endwhile; ?> Now,... [Read the full story]

Get wordpress posts having a specific custom field

December 23, 2008

How we can only get posts that have a certain custom field. As far as we know, this question has been asked many times on several blogs and WordPress forums. It is quite simple. What I’ve done is simply using a normal loop (That you can enhance with query_post() for exemple) and then, using a conditional instruction to check out if the post have the esired custom field. <?php if (have_posts()) : while (have_posts()) : the_post(); $customField = get_post_custom_values("img"); if (isset($customField[0]))... [Read the full story]

Page 8 of 14« First...«678910»...Last »