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

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();
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换