【问题标题】:How to open the_attachment_link ( WordPress ) in new window如何在新窗口中打开 the_attachment_link ( WordPress )
【发布时间】:2016-03-27 23:34:52
【问题描述】:

大家好,我有一个使用 wp-types 插件的 PDF 文件的帖子类型附件。实际上脚本工作正常并在列表中显示所有 PDF 文件,但我需要在新窗口中打开它们。知道如何解决这个问题并使帖子类型类别中的每个文件都在新窗口中打开吗?

我知道如何将其添加到普通 HTML 文档中或使用带有永久链接的 href。


<a href="#" title="My PDF File Title" target="_blank">PDF File</a>

例如,我有类似的 PDF 单个文件,代码如下,并且工作正常:

<a href="<?php echo types_render_field("main-pdf");?>" target="_blank" title="Download <?php the_title(); ?> - Specifications"><i class="fa fa-file-pdf-o"></i> Download PDF Specification for <?php the_title();?></a>

我无法使用下面的代码在新窗口中打开每个文件,例如按照这个 WordPress 指南“将附加的图像和标题显示为列表”访问link (WordPress Codex)

target="blank"

对下面的代码有什么建议或解决方案吗?

<?php 
$args = array(
'post_type' => 'attachment',
'post_status' => 'any',
'target' => '_blank', // a try this but not working
'tax_query' => array(
    array(
        'taxonomy' => 'media_category',
        'field' => 'link',
        'terms' => 137
    )
)

);

$the_query = new WP_Query( $args );

if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
    $the_query->the_post();


       echo '<li class="cataloguelist">';
       echo '<i class="fa fa-file-pdf-o"></i>';

       echo the_attachment_link( $attachment->ID, true);
       echo apply_filters( 'the_title', $attachment->post_title );

       echo '</li>';

}
} else {
// no attachments found
}

wp_reset_postdata();?>

【问题讨论】:

    标签: php wordpress custom-post-type


    【解决方案1】:

    删除'target' =&gt; '_blank', // a try this but not working 并在主题的functions.php 中添加以下代码以添加target="_blank"

    仅在使用the_attachment_linkwp_get_attachment_link 时有效

    function TargetAttachmentLink($markup) {
        return preg_replace('/^<a([^>]+)>(.*)$/', '<a\\1 target="_blank">\\2', $markup);
    }
    add_filter( 'wp_get_attachment_link', 'TargetAttachmentLink', 10, 6 );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-24
      • 1970-01-01
      • 1970-01-01
      • 2016-08-14
      • 1970-01-01
      • 2020-12-01
      • 2014-11-04
      相关资源
      最近更新 更多