【问题标题】:Echo single image attachment title in wordpress在wordpress中回显单个图像附件标题
【发布时间】:2013-10-20 21:23:57
【问题描述】:

我想知道能够在附件图像页面上获取标题的确切代码是什么。这是我现在在 image.php 文件中用于标题部分的代码。

<h2><a href="<?php echo get_permalink($post->post_parent); ?>" rev="attachment"><?php echo get_the_title($post->post_parent); ?> </a><?php echo ' &raquo; $attachment->post-title'; ?></h2>

所以我希望它显示主帖子标题,然后是双右箭头,然后是单个图像标题,但 $attachment->post-title 不是正确的命令。

我正在我的测试站点上尝试这个。在这里,您可以看到我所指的确切内容。 http://pandafeed.net/gave-her-a-bath-and-tucked-her-in-she-passed-out-right-away/thumbnail-for-1407/

提前致谢

【问题讨论】:

    标签: php image title


    【解决方案1】:

    这应该获取首页上每个帖子的第一个附加图片(或列表中的任何帖子)的标题:

    <?php
      if (! is_singular()) {
        $image_id = get_the_ID();
        // Not necessary, but it's an option:
        //$permalink = get_permalink($image_id);
        $args = array(
          'post_type'   => 'attachment',
          'numberposts' => 1,
          'post_parent' => $image_id
        );
        $attachments = get_posts($args);
        if ($attachments) {
          $title = get_the_title($attachments[0]->ID);
          print $title;
        }
      }
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-08
      • 2019-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-26
      相关资源
      最近更新 更多