WordPress.org

Forums

[resolved] Link to external site if post id (2 posts)

  1. WebAssembler
    Member
    Posted 1 year ago #

    Hi there

    I am currently displaying a list of posts within a category id using get_posts. Each post is displayed in an <li> and links to the_permalink. This works fine but I want one of the posts to link to an external site based on the post id which is 17. I've had a go at this but can't seem to work it out and am new to this. Any help much appreciated!

    <?php while (have_posts()) : the_post(); ?>
    
      <?php the_content(); ?>
    
    <?php endwhile; ?>  
    
    <ul>
    	<?php
    
    		$args = array(
    			'posts_per_page' => 5,
    			'category' => 2
    		);
    
    		$myposts = get_posts( $args );
    
    		foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
    
    		<!-- if the post id = 17 create a url link to google
    		otherwise make the link go to the permalink -->
    		<?php $my_id = get_the_ID(17); ?>
    		<?php if( $my_id ) { ?>
    		<li>
    			<a href="http://www.google.com"><?php the_title(); ?></a>
    		</li>
    		<?php } else { ?>
    		<li>
    			<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    		</li>
    		<?php } ?>
    
    		<?php endforeach; wp_reset_postdata(); ?>
    
    </ul>
  2. WebAssembler
    Member
    Posted 1 year ago #

    I have worked it out :-)

    <?php if ( $post->ID == 17) { ?>
    
    	<li>
    		<a href="http://www.google.com"><?php the_title(); ?></a>
    	</li>
    
    	<?php } else { ?>
    	<li>
    		<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    	</li>
    <?php } ?>
    <?php endforeach; wp_reset_postdata(); ?>

Topic Closed

This topic has been closed to new replies.

About this Topic