首页 > 解决方案 > Wordpress ACF php 关系

问题描述

我整天都在阅读论坛,我正在努力想出解决这个问题的方法。

我有 2 种自定义帖子类型:人员职位

使用 ACF 关系,我有一个名为 Job Client (job_client) 的 2 向关系字段,我们可以将相关的客户/客户链接到作业。

我想在工作页面上显示的是从“人员”自定义帖子类型中提取的客户的一些基本信息。雇主、联系方式等。这些设置为 ACF 字段。

到目前为止,我已经得到了以下代码。它可以很好地显示标准的 wordpress 字段(标题、永久链接),但我在 ACF 字段上苦苦挣扎。任何帮助/建议将不胜感激。

<?php
$job_client_connect = get_posts(array(
    'post_type' => 'people',
    'meta_query' => array(
        array(
            'key' => 'job_client', // name of custom field
            'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
            'compare' => 'LIKE'
        )
    )
));

?>
<?php if( $job_client_connect ): ?>
    <table>
    <?php foreach( $job_client_connect as $job_client_connect ):?>
        <tr>
            <td>
                <a href="<?php echo get_permalink( $job_client_connect->ID ); ?>">
                    <?php echo get_the_title( $job_client_connect->ID ); ?>
                </a>
                <p><?php echo get_field('employer', $job_client_connect->ID ); ?></p>
            </td>
        </tr>
    <?php endforeach; ?>
    </table>
<?php endif; ?>

标签: phpwordpressadvanced-custom-fields

解决方案


任何可能觉得这很有用的人的完整工作代码

<?php
$job_client_connect = get_posts(array(
    'post_type' => 'people', // name of post type
    'meta_query' => array(
        array(
            'key' => 'job_client', // name of custom field
            'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
            'compare' => 'LIKE'
        )
    )
));

?>

<?php if( $job_client_connect ): ?>
    <table>
    <?php foreach( $job_client_connect as $job_client_connect ):?>
        <?php $employer = get_field('employer', $job_client_connect->ID ); ?>
        <tr>
            <td>
                <a href="<?php echo get_permalink( $job_client_connect->ID ); ?>">
                    <?php echo get_the_title( $job_client_connect->ID ); ?>
                </a>
                <p><?php echo $employer[0]->post_title; ?></p>



            </td>
        </tr>
    <?php endforeach; ?>
    </table>
<?php endif; ?>

推荐阅读