【问题标题】:Wordpress Custom fields display file URLWordpress 自定义字段显示文件 URL
【发布时间】:2014-02-24 21:31:14
【问题描述】:

我有以下代码,需要从我的自定义帖子类型(称为事实表)中显示三件事。

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

?>

【问题讨论】:

    标签: php wordpress custom-fields advanced-custom-fields


    【解决方案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 的进一步解释。

    【讨论】:

    • 这可行,但有两件事...... 1.链接返回为sitename.com/Array而不是sitename.com/filename.pdf 2.我们如何让它循环遍历所有帖子,而不仅仅是那些由wordpress设置设置的帖子(我设置为 5)...
    • @lowercase 我编辑并添加了'posts_per_page' =&gt; -1,它将无限获取所有帖子。关于您的第一个问题,它是关于您如何在 ACF 面板中创建自定义字段的。编辑您创建的字段并将其发送给return the file URL
    • 正确!非常感谢。我将自定义字段切换为返回 URL,posts_per_page 效果很好。非常感谢!
    • @小写随时:)
    【解决方案2】:

    你可以试试这个吗?从 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>
    

    如果可行,也许最好根据您的需要修改一个工作示例 - 通过添加您的自定义字段。只是我的想法:-)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多