0
votes

I'm on a post page and in addition I want to display a thumbnail, title, and link from all posts in the category titled "sponsor". I was able to display the thumbnail and title:

 <a <?php echo ( !wp_is_mobile() )? 'target="_blank"' : '' ?> href="<?php _s( get_the_ID()) ?>">
 <?php 
    query_posts( array( 'category_name' => 'sponsor' ) );
    if ( have_posts() ) while ( have_posts() ) : the_post();
    echo '<li>';
    the_post_thumbnail( 'big-thumb', array( 'alt' => get_the_title(), 'class' => 'img-responsive post-cover' ) );
    the_title();
    echo '</li>';
    endwhile;
    wp_reset_query();
?>
 </a>

BUT am unable to successfully get the right links for the posts within the category "sponsor". I tried using get_the_ID, but it is using the link from the post page that I'm on. From my research I think it's because I need to get the page ID outside the loop.

I'm a newbie when it comes to PHP so any help would be appreciated.

1
What are you trying to get, the ID or link of posts from Sponsor category? - Dee Jay
I'm trying to get the link. I should have mentioned this, but I created a custom field called "external-link" which is what I want as the the link. - kesernio
Could you edit the question so it would show where you put the get_the_ID() function, I just want to see how you tried to get the link? - Dee Jay
Sure. Just added it up top. - kesernio

1 Answers

0
votes

Just try it as to make enable link for post by their id

 <?php 
    query_posts( array( 'category_name' => 'sponsor' ) );
    if ( have_posts() ) while ( have_posts() ) : the_post(); ?>

    <a <?php echo ( !wp_is_mobile() )? 'target="_blank"' : '' ?> href="<?php _s( get_the_ID()) ?>">
    <?php
    echo '<li>';
    the_post_thumbnail( 'big-thumb', array( 'alt' => get_the_title(), 'class' => 'img-responsive post-cover' ) );
    the_title();
    echo '</li>';
    echo '</a>';
    endwhile;
    wp_reset_query();
?>