是否从带有ACF字段的CPT发布XML提要?

时间:2020-01-31 作者:Marc Murray

他们正在为一家招聘机构开发一个网站,并希望将他们的工作公告板与事实同步。通过与代表聊天,他们说我们需要以以下格式向提供XML提要:

https://www.indeed.com/intl/en/xmlinfo.html

假设我要创建一个自定义的post类型,并为每个XML节点创建一个ACF字段,那么如何将该post类型中的所有post转换为XML字段呢?

1 个回复
SO网友:Dave Romsey

下面是一个相当完整的示例,说明了一种方法。

让我们注册一个将与feed一起使用的职位类型。

/**
 * Register Job post type
 * https://developer.wordpress.org/reference/functions/register_post_type/
 */
function wpse_register_job_post() {
    $book_args = [
        \'label\'              => __( \'Jobs\', \'textdomain\' ),
        \'public\'             => true,
        \'publicly_queryable\' => true,
        \'show_ui\'            => true,
        \'show_in_menu\'       => true,
        \'query_var\'          => true,
        \'rewrite\'            => [ \'slug\' => \'jobs\' ],
        \'capability_type\'    => \'post\',
        \'has_archive\'        => true,
        \'hierarchical\'       => false,
        \'menu_position\'      => null,
        \'supports\'           => [ \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'comments\', \'custom-fields\' ],
    ];
    register_post_type( \'jobby_job\', $book_args );
}
add_action( \'init\', \'wpse_register_job_post\' );
现在来看好东西。让我们创建自定义提要和渲染器。可以通过访问http://example.com/?feed=indeed

Register Feed

/**
 * Create a custom feed.
 * https://developer.wordpress.org/reference/functions/add_feed/
 */
function wpse_add_indeed_job_feed() {
    add_feed( \'indeed\', \'wpse_render_indeed_job_feed\' );
}
add_action( \'init\', \'wpse_add_indeed_job_feed\' );

Render Feed

/**
 * Render our custom feed.
 */
function wpse_render_indeed_job_feed() {
    header( \'Content-Type: \' . feed_content_type( \'rss2\' ) . \'; charset=\' . get_option( \'blog_charset\' ), true );

    echo \'<?xml version="1.0" encoding="\' . get_option( \'blog_charset\' ) . \'"?\' . \'>\';
    ?>

<source>
<publisher><?php wp_title_rss(); ?></publisher>
<publisherurl><?php echo get_site_url(); ?></publisherurl>
<lastBuildDate><?php echo date( \'D, j M Y G:i:s\' ) . \' GMT\'; ?></lastBuildDate>
<?php
    // Get the job posts. Customize arguments as needed.
    $job_query = new WP_Query( [
        \'post_type\'      => \'jobby_job\',
        \'posts_per_page\' => 42,
    ] );

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

        // Note: You\'ll need to get the various post meta fields and add them below.
        // Use get_post_meta() or for ACF, get_field() can be used.
        // Meta is hard coded in this example.
?>
<job>
    <title><![CDATA[<?php the_title_rss(); ?>]]></title>
    <date><![CDATA[<?php echo mysql2date( \'D, d M Y H:i:s +0000\', get_post_time( \'Y-m-d H:i:s\', true ), false ); ?>]]></date>
    <referencenumber><![CDATA[unique123131]]></referencenumber>
    <url><![CDATA[<?php the_permalink_rss(); ?>]]></url>
    <company><![CDATA[Big ABC Corporation]]></company>
    <city><![CDATA[Phoenix]]></city>
    <state><![CDATA[AZ]]></state>
    <country><![CDATA[US]]></country>
    <postalcode><![CDATA[85003]]></postalcode>
    <description><![CDATA[<?php echo get_the_content_feed( \'rss2\' ); ?>]]></description>
    <salary><![CDATA[$50K per year]]></salary>
    <education><![CDATA[Bachelors]]></education>
    <jobtype><![CDATA[fulltime, parttime]]></jobtype>
    <category><![CDATA[Category1, Category2, CategoryN]]></category>
    <experience><![CDATA[5+ years]]></experience>
</job>
<?php } ?>
<?php } ?>
</source>
<?php
wp_reset_postdata();
}
作为参考,WP的默认RSS提要模板位于wp-includes/feed-rss2.php.

记住在定制提要时要清除缓存,因为浏览器往往会缓存输出。

其他一些资源: