【问题标题】:How can i link wordpress post's title to pdf file download?如何将 wordpress 帖子的标题链接到 pdf 文件下载?
【发布时间】:2023-03-24 01:30:01
【问题描述】:

嗨,对不起,我的英语不好。

在我正在构建的网站中,我想提供一个部分,用户可以在其中直接从帖子标题链接下载 pdf 文档。我试图更好地解释我。

我希望我的客户可以撰写仅附有标题和 pdf 文件的帖子(分为许多类别)。

在网站的前端,我提供了一个页面,该页面从特定类别的每个帖子中加载标题,如果用户单击这些标题,则下载附加的相关 pdf 文件。

有没有办法做到这一点,以便我可以跳过帖子中“点击此处查看”的额外步骤?换句话说,我想添加一个帖子,当点击该帖子时,我希望它下载 PDF,而不是实际链接到该帖子。

提前致谢。

【问题讨论】:

    标签: wordpress pdf post hyperlink


    【解决方案1】:

    我找到了答案。

    正确的代码是:

    <?php 
    $categoria = get_cat_ID('mycategory');
    $posts = get_posts('category='.$categoria);
    foreach($posts as $post) { 
    $content = $post->post_content; 
    ?>
    <a href="<?php echo($content); ?>" target="_parent"><?php the_title(); ?></a></br>
    <?php } ?>
    

    或许这是另一个更好的解决方案:

    //seleziono la categoria che voglio mostrare
    $categoria = get_cat_ID('casi di studio');
    //prendo i post solo di quella categoria
    $posts = get_posts('category='.$categoria); 
    //per ogni post ricavato vado a cercare se esiste un allegato
    foreach($posts as $post) {
        $args = array(
            'post_type' => 'attachment',
            'post_parent' => $post->ID
            ); 
    
        $attachments = get_posts($args);
        //se l'allegato c'è entro nell'if
        if ($attachments) {
            foreach ($attachments as $attachment) {
                // Get the parent post ID
                $parent_id = $attachments->post_parent;
                // Get the parent post Title
                $parent_title = get_the_title( $parent_id );
                echo wp_get_attachment_link( $attachment->ID, '' , false, false, $parent_title ); 
                ?>
                </br>
            <?
            }
        }
    }
    

    【讨论】:

    • 或者,您也可以为此使用“页面链接到”插件。
    • 对不起,我不想使用插件。
    • 我只会为 PDF 链接使用自定义字段(例如,将其上传到 Wordpress 并使用 URL 作为字段值),然后在我的模板代码中只需将标题锚中的 &lt;?php the_permalink(); ?&gt; 替换为&lt;?php echo get_post_meta($post-&gt;ID, 'pdf-link', true); ?&gt;.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-05
    • 1970-01-01
    • 1970-01-01
    • 2013-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多