Title: using if else with wp_get_recent_posts
Last modified: September 1, 2016

---

# using if else with wp_get_recent_posts

 *  Resolved [liquidRock](https://wordpress.org/support/users/liquidrock/)
 * (@liquidrock)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/using-if-else-with-wp_get_recent_posts/)
 * Hi
 * I’m trying to generate a hyperlink on my homepage for the most recent post using
   wp_get_recent_posts() and is_category(). The site is not setup like a blog—posts
   are being loaded into custom page templates using categories with dynamic #id
   based on slug. So, I’m trying to get the category of the most recent post and
   then generate the link for it with the proper URL.
 * I tested the structure of the link generation outside of the if else and it almost
   works (it grabbed the correct slug, but with the audio category instead of zazzle),
   but it’s not working in the if else statements.
 * I’m not a programmer so it may be a stupid mistake. One thing I did notice is
   that if I print the array $recent, there seems to be no category name or category
   number in it, even if I specify the categories in the $args array like ‘category’
   => ‘2,3,4,5’ .
 * The homepage template is hard-coded as it’s not loading any posts into it, so
   maybe I need to use this stuff in a loop to make it work right??
 *     ```
       <?php
       	$args = array( 'numberposts' => '1', 'offset' => '0', 'orderby' => 'post_date', 'order' => 'DESC', 'post_status' => 'publish' );
       	$recent_posts = wp_get_recent_posts( $args );
       	$home = home_url( '/' );
       	$homeURL = esc_url( $home );
       	$before = '<a href="';
       	$after = '">dive in</a>';
   
       	foreach( $recent_posts as $recent ){
   
       		$slug = basename( get_permalink($recent["ID"]) );
   
       		// if post is audio
       		if ( is_category('audioz') ) {
       			echo $before . $homeURL . "audio/" . "#" . $slug . $after;
       		}
       		// else if post is video
       		else if ( is_category('videoz') ) {
       			echo $before . $homeURL . "videos/" . "#" . $slug . $after;
       		}
       		// else if post is article
       		else if ( is_category('articlez') ) {
       			echo $before . $homeURL . "article/" . "#" . $slug . $after;
       		}
       		// else if post is zazzle link
       		else if ( is_category('zazzle') ) {
       			echo $before . $homeURL . "store/" . "#" . $slug . $after;
       		}
       	}
       ?>
       ```
   
 * I also tried using the category number in is_category() but that didn’t work,
   and I also tried using in_category().
 * Advice welcome. I’ve been at this for hours and can’t find anything about this
   on the forums or through web searches. I can’t link to the page as I only have
   the ability to work locally at the moment in MAMP.

Viewing 6 replies - 1 through 6 (of 6 total)

 *  [Clarion Technologies](https://wordpress.org/support/users/clarionwpdeveloper/)
 * (@clarionwpdeveloper)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/using-if-else-with-wp_get_recent_posts/#post-7459579)
 * Hello liquidRock,
 * If you want the current post in loop has category then please use “has_category(
   $category, $post );”.Please follow the below link you get idea how to use this:
 * [https://codex.wordpress.org/Function_Reference/has_category](https://codex.wordpress.org/Function_Reference/has_category)
 * Thanks
 *  Thread Starter [liquidRock](https://wordpress.org/support/users/liquidrock/)
 * (@liquidrock)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/using-if-else-with-wp_get_recent_posts/#post-7459580)
 * Hi there
 * I tried replacing is_category with has_category but it’s still not working. I
   also tried using in_category with the same syntax but to no avail.
 * Here’s the modified code. Is this correct?
 *     ```
       <?php
       	$args = array( 'numberposts' => '1', 'offset' => '0', 'orderby' => 'post_date', 'order' => 'DESC', 'post_status' => 'publish' );
       	$recent_posts = wp_get_recent_posts( $args );
       	$home = home_url( '/' );
       	$homeURL = esc_url( $home );
       	$before = '<a href="';
       	$after = '">dive in</a>';
   
       	foreach( $recent_posts as $recent ){
   
       		$slug = basename( get_permalink($recent["ID"]) );
   
       		// if post is audio
       		if ( has_category('audioz'), $recent ) {
       			echo $before . $homeURL . "audio/" . "#" . $slug . $after;
       		}
       		// else if post is video
       		else if ( has_category('videoz'), $recent ) {
       			echo $before . $homeURL . "videos/" . "#" . $slug . $after;
       		}
       		// else if post is article
       		else if ( has_category('articlez'), $recent ) {
       			echo $before . $homeURL . "article/" . "#" . $slug . $after;
       		}
       		// else if post is zazzle link
       		else if ( has_category('zazzle'), $recent ) {
       			echo $before . $homeURL . "store/" . "#" . $slug . $after;
       		}
       	}
       ?>
       ```
   
 *  [Clarion Technologies](https://wordpress.org/support/users/clarionwpdeveloper/)
 * (@clarionwpdeveloper)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/using-if-else-with-wp_get_recent_posts/#post-7459594)
 * Hello liquidRock,
 * Please try using like:
 * if ( has_category(‘audioz’, $recent[“ID”] ) )
 * Thanks
 *  Thread Starter [liquidRock](https://wordpress.org/support/users/liquidrock/)
 * (@liquidrock)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/using-if-else-with-wp_get_recent_posts/#post-7459618)
 * Success!!
 * Can’t believe I didn’t try that since I used a similar thing with my $slug variable.
 * At any rate, thanks so much. Working perfectly now 🙂
 *  [Clarion Technologies](https://wordpress.org/support/users/clarionwpdeveloper/)
 * (@clarionwpdeveloper)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/using-if-else-with-wp_get_recent_posts/#post-7459621)
 * Welcome ,Can you please make to resolved.
 *  Thread Starter [liquidRock](https://wordpress.org/support/users/liquidrock/)
 * (@liquidrock)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/using-if-else-with-wp_get_recent_posts/#post-7459623)
 * resolved 😉

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘using if else with wp_get_recent_posts’ is closed to new replies.

## Tags

 * [hyperlink](https://wordpress.org/support/topic-tag/hyperlink/)
 * [if else](https://wordpress.org/support/topic-tag/if-else/)
 * [wp_get_recent_posts](https://wordpress.org/support/topic-tag/wp_get_recent_posts/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 6 replies
 * 2 participants
 * Last reply from: [liquidRock](https://wordpress.org/support/users/liquidrock/)
 * Last activity: [10 years, 2 months ago](https://wordpress.org/support/topic/using-if-else-with-wp_get_recent_posts/#post-7459623)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
