• Resolved liquidRock

    (@liquidrock)


    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)
  • 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.www.ads-software.com/Function_Reference/has_category

    Thanks

    Thread Starter liquidRock

    (@liquidrock)

    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;
    		}
    	}
    ?>

    Hello liquidRock,

    Please try using like:

    if ( has_category(‘audioz’, $recent[“ID”] ) )

    Thanks

    Thread Starter liquidRock

    (@liquidrock)

    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 ??

    Welcome ,Can you please make to resolved.

    Thread Starter liquidRock

    (@liquidrock)

    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.