0

我正在使用 Pods 在 WordPress 中设置自定义帖子类型。

我有两种自定义帖子类型:汽车和车库。在 Car 中,我设置了与 Garage 的关系字段,因此在编辑 Car 帖子时,我可以从下拉列表中选择其中一个可用的 Garage 帖子。

查看汽车帖子时,我想显示所选车库帖子的名称和链接。我该怎么做?

以下是来自http://pods.io/tutorials/get-values-from-a-custom-relationship-field的示例

//get Pods object for current post
$pod = pods( 'pod_name', get_the_id() );
//get the value for the relationship field
$related = $pod->field( 'relationship_field' );
//loop through related field, creating links to their own pages
//only if there is anything to loop through
if ( ! empty( $related ) ) {
    foreach ( $related as $rel ) { 
        //get id for related post and put in ID
        //for advanced content types use $id = $rel[ 'id' ];
        $id = $rel[ 'ID' ];
        //show the related post name as link
        echo '<a href="'.get_permalink($id).'">'.get_the_title( $id ).'</a>';
        //get the value for some_field in related post and echo it
    } //end of foreach
} //endif ! empty ( $related )

而不是车库帖子的永久链接,我得到了几个我正在查看的汽车帖子的永久链接。

有任何想法吗?

4

0 回答 0