2014-02-24 45 views
0

我有以下代码,需要从我的自定义发布类型称为事实表显示三件事情。WordPress的自定义字段显示文件的URL

  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>'; 
} 
} 

?> 

回答

1

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

对于自定义字段(使用ACF Plugin),你使用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()check this out

+0

这个工程,但对于两件事... 1.链接返回为http://sitename.com/Array而不是http://sitename.com/filename.pdf 2.我们怎样才能得到它通过所有循环帖子,而不仅仅是由wordpress设置(我已设置为5)设置的那些...... – lowercase

+0

@lowercase我编辑并添加了''posts_per_page'=> -1',它将获取所有帖子,无限制。关于你的第一个问题,这是关于你如何在ACF面板中创建自定义字段。编辑您创建的字段并要求它[返回文件URL](http://www.advancedcustomfields.com/resources/field-types/file/)。 – mathielo

+0

正确!非常感谢。我将自定义字段切换为返回网址,posts_per_page效果很好。 备受赞赏! – lowercase

1

你能试试吗?它稍微从WP食品法典委员会(不多)的手动

<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> 

修改如果成功,也许是更好的修改工作示例,以您的需求 - 通过添加自定义字段。只是我的想法:-)

+0

这产生了正确的PDF下载链接。 – lowercase

相关问题