【问题标题】:Link to first image attachment in a wordpress post链接到 wordpress 帖子中的第一个图像附件
【发布时间】:2010-03-21 20:33:57
【问题描述】:

如何在 wordpress 帖子中创建指向第一个图像附件页面的文本链接,而不试图弄清楚发布后的 slug 是什么。我意识到我可以将图像链接到他们的附件页面,但我无法创建文本链接。这可能吗?

【问题讨论】:

    标签: wordpress wordpress-theming


    【解决方案1】:

    想通了:

    <?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) . '">LINK TEXT HERE</a>';
            break;
        }
    }
    }
    
    ?>
    

    【讨论】:

    • 如果您将get_posts 调用限制为一个帖子,您将得到完全相同的结果,例如。 'numberposts' =&gt; 1, ...(这会比选择每个关联的附件只显示一个要好一些)。
    • 这很有道理,不知道为什么我有“-1”。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-17
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 2012-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多