Wordpress:使用来自ACF自定义字段

时间:2015-05-28 05:22:40

标签: wordpress export-to-csv advanced-custom-fields

我尝试将自己的帖子从自定义帖子类型导出到csv文件。我稍后想回到的一点是,我的帖子包含使用高级自定义字段插件创建的自定义字段。

我发现这可以通过编写数据库查询来完成,甚至可以找到各种插件来获取所有细节。我面临的问题是我的帖子中的一个自定义字段是'关系帖子对象'字段和我用来将帖子导出为CSV的任何方法都将此post对象字段值作为数组提供。有没有办法获得在前端输出的实际内容?

1 个答案:

答案 0 :(得分:0)

如果将前端CSV的数据输出到表格中。 使用此Jquery片段创建CSV。

在前端,你需要一个表和一个带有类.getfile

的标签

将其添加到您的JS文件中。

function exportTableToCSV($table, filename) {

var $rows = $table.find('tr:has(td)'),

    // Temporary delimiter characters unlikely to be typed by keyboard
    // This is to avoid accidentally splitting the actual contents
    tmpColDelim = String.fromCharCode(11), // vertical tab character
    tmpRowDelim = String.fromCharCode(0), // null character

    // actual delimiter characters for CSV format
    colDelim = '","',
    rowDelim = '"\r\n"',

    // Grab text from table into CSV formatted string
    csv = '"' + $rows.map(function (i, row) {
        var $row = $(row),
            $cols = $row.find('td');

        return $cols.map(function (j, col) {
            var $col = $(col),
                text = $col.text();

            return text.replace('"', '""'); // escape double quotes

        }).get().join(tmpColDelim);

    }).get().join(tmpRowDelim)
        .split(tmpRowDelim).join(rowDelim)
        .split(tmpColDelim).join(colDelim) + '"',

    // Data URI
    csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csv);

    $(this)
        .attr({
        'download': filename,
            'href': csvData,
            'target': '_blank'
    });
}

var now = $.datepicker.formatDate('dd/mm/yy', new Date());
var title = $('title').text();
$('.getfile').click(function() { 
    exportTableToCSV.apply(this, [$('#DLTable'), 'Exported ' + title + ' ' + now + '.csv']);
});

我也使用ACF,很棒的插件。希望这可以帮助。

按要求提供的HTML

<h1 class="title">title</h1>
  <a href="#" class="getfile button">Export as CSV</a>          
<table id="DLTable" border="1" cellpadding="5" cellspacing="0">
  <tr>    
     <td> // loop this with ACF repeater field </td>
  </tr>
</table>