Post Title Not showing up

时间:2013-06-29 作者:user32447

嗨,我用这个代码从某个类别呼叫post

<?php
$queryObject = new  Wp_Query( array(
        \'posts_per_page\' => 3,
        \'post_type\' => array(\'post\'),
        \'category_name\' => \'photos-bet-awards-2013-2\',
            \'orderby\' => \'date\',
        ));
// The Loop!
if ($queryObject->have_posts()) {
?>
<?php
while ($queryObject->have_posts()){
    $queryObject->the_post();
    ?>

<a href="<?php the_permalink(); ?>" title="<?php printf(__( \'Read %s\', \'wpbx\' ), wp_specialchars(get_the_title(), 1)) ?>">
                    <?php the_post_thumbnail(\'betphumbs\'); ?>
                    </a>

             <?php  
$args = array(
\'post_type\' => \'attachment\',
\'numberposts\' => -1,
\'offset\' => 0,
\'orderby\' => \'menu_order\',
\'order\' => \'asc\',
\'post_status\' => null,
\'post_parent\' => $post->ID,
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
    if(wp_attachment_is_image( $attachment->ID )) {
    echo \'<a href="\'. get_attachment_link($attachment->ID) . \'"><?php the_title(); ?>    </a>\';
    break;
 }
}
}

?>


<span class="entry-date"><font color="#999999">
<?php
$attachments = get_children(array(\'post_parent\'=>$post->ID));
$nbImg = count($attachments);
echo \'\'.$nbImg.\' photos.\';
?><br><b> <?php the_time(\'m/d/y \\a\\t g:ia\') ?></b></font></span>

<?php
}
?>
<?php wp_reset_postdata();
}
?>
现在我的问题是<?php the_title(); ?> 没有抓住帖子的标题how can i fix this?

1 个回复
SO网友:rfrq

添加:

$first_attachment = reset($attachments); 
$first_attachment_id = $first_attachment->ID;
$first_attachment_title = $first_attachment->post_title;
以下内容:

$nbImg = count($attachments);
现在,您可以通过以下方式返回链接:

echo wp_get_attachment_link( $first_attachment_id, \'\' , true, false, $first_attachment_title ); 
如果需要一种自定义链接标记,可以查看相关函数:http://codex.wordpress.org/Function_Reference/wp_get_attachment_link

Edit:

没有必要获取两次附件,据我所知,您只需要知道您有多少个附件以及第一个附件的URL。因此,您可以替换:

<?php  
$args = array(
\'post_type\' => \'attachment\',
\'numberposts\' => -1,
\'offset\' => 0,
\'orderby\' => \'menu_order\',

...

break;
 }
}
}

?>
使用:

<?php
$attachments = get_children(array(\'post_parent\'=>$post->ID));
$nbImg = count($attachments);
$first_attachment = reset($attachments); 
$first_attachment_id = $first_attachment->ID;
$first_attachment_title = $first_attachment->post_title;
?>
之后,您可以放置链接:

<?php echo wp_get_attachment_link( $first_attachment_id, \'\' , true, false, $first_attachment_title ); ?>

结束