如何将wordpress帖子的标题链接到pdf文件下载?

时间:2013-11-13 08:57:21

标签: wordpress pdf post hyperlink

嗨,我很抱歉我的英语。

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

我希望我的客户可以只附加标题和pdf文件来撰写帖子(分为多个类别)。

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

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

提前致谢。

1 个答案:

答案 0 :(得分:0)

我找到了答案。

正确的代码是:

<?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>
        <?
        }
    }
}
相关问题