duanhuizhe6767
2014-10-27 03:16
浏览 72
已采纳

Wordpress在循环外获取ID

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条回答 默认 最新

  • dp9599 2014-10-27 04:15
    已采纳

    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();
    ?>
    
    打赏 评论

相关推荐 更多相似问题