0

我有以下代码,需要从我的自定义帖子类型中显示三件事,称为情况说明书。

  1. 标题
  2. 摘要 (fact_sheet_summary)
  3. 文件上传 URL (fact_sheet_pdf_link)

我可以让前两个工作,但不知道如何做第三个。基本上我的输出应该是......

The Title The Summary paragraph Click here to download as PDF

有什么想法可以查询以列出所有这些帖子类型的结果吗?有没有比我下面的更好的方法来做到这一点?主要问题是,我无法获取上传文件的 URL。

<?php

$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'fact-sheet',
'order' => 'ASC',
));

if($posts)
{


foreach($posts as $post)
{
    echo '<span class="fact-sheet-title">' . get_the_title($post->ID) . '</span><br />';
    echo '<p><span class="fact-sheet-summary">' . the_field('fact_sheet_summary') . '</span></p>';
}
}

?>
4

2 回答 2

1

你能试试这个吗?它是从 WP Codex 的手册略微修改的(不多)

<ul>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();    

 $args = array(
   'post_type' => 'attachment',
   'post_mime_type' => array('application/pdf'),
   'numberposts' => -1,
   'post_status' => null,
   'post_parent' => $post->ID
  );

  $attachments = get_posts( $args );
     if ( $attachments ) {
        foreach ( $attachments as $attachment ) {
           echo '<li>';
           the_attachment_link( $attachment->ID, true );
           echo '<p>';
           echo apply_filters( 'the_title', $attachment->post_title );
           echo '</p></li>';
          }
     }

 endwhile; endif; ?>
</ul>

如果它有效,也许最好根据您的需要修改工作样本 - 通过添加您的自定义字段。只是我的想法:-)

于 2014-02-24T21:52:19.890 回答
1

我认为最好使用query_posts(),因为您可以使用The Loop default structure

至于自定义字段(使用ACF 插件),您正在使用the_field(),它会自动显检索到的字段值。另一方面,您可以使用get_field()仅返回字段值的函数。

你可以这样做:

// Query all posts from 'fact-sheet' post_type
query_posts(array(
    'numberposts' => -1,
    'post_type' => 'fact-sheet',
    'order' => 'ASC',
    // Getting all posts, limitless
    'posts_per_page' => -1,
));

// Loop throught them
while(have_posts()){
    the_post();

    echo '<span class="fact-sheet-title">' . get_the_title() . '</span><br />';
    echo '<p><span class="fact-sheet-summary">' . get_field('fact_sheet_summary') . '</span></p>';
    // Echos the link to PDF Download
    echo '<p><a href="'. get_field('fact_sheet_pdf_link') .'" target="_blank">Click here to download as PDF</a></p>';

}

// Once you're done, you reset the Default WP Query
wp_reset_query();

如果您可能需要进一步解释wp_reset_query()请查看此内容。

于 2014-02-24T21:58:40.810 回答