• Resolved Web Assembler

    (@webassembler)


    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="https://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>
Viewing 1 replies (of 1 total)
  • Thread Starter Web Assembler

    (@webassembler)

    I have worked it out ??

    <?php if ( $post->ID == 17) { ?>
    
    	<li>
    		<a href="https://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(); ?>
Viewing 1 replies (of 1 total)
  • The topic ‘Link to external site if post id’ is closed to new replies.