【问题标题】:Advanced Custom Fields Relationship - Basic loop + reversed?高级自定义字段关系 - 基本循环 + 反转?
【发布时间】:2016-06-08 23:34:57
【问题描述】:

我想使用高级海关字段将 公司产品 联系起来。这些是我网站上的一些页面:

www.example.com/companies/apple/
www.example.com/companies/microsoft/
www.example.com/products/iphones/
www.example.com/products/ipods/

例如,我想将 Apple 与他们的产品(iPhone 和 iPod)连接起来。

我在 ACF 中添加了一个名为 related_articles 的关系字段,该字段仅在页面父级为 /companies/-page 时可用。

因此,在 Apple 的公司页面上 - 产品 iPhone 和 iPod 已在自定义关系字段 related_articles 中被选中。

在 page.php 我添加了以下内容:

<?php 

$posts = get_field('related_articles');

if( $posts ): ?>
    <?php foreach( $posts as $p ): // variable must NOT be called $post (IMPORTANT) ?>
            <a href="<?php echo get_permalink( $p->ID ); ?>"><?php echo get_the_title( $p->ID ); ?></a><br>
    <?php endforeach; ?>
<?php endif; ?>

在页面www.example.com/companies/apple/返回以下内容:

<a href="http://www.example.com/products/iphones/">iPhones</a><br>
<a href="http:// www.example.com/products/ipods/">iPods</a><br>

这完全符合我的要求。

问题是http://www.example.com/products/iphones/http:// www.example.com/products/ipods/ 是空的。在这里,我希望反转关系并显示&lt;a href="http://www.example.com/companies/apple/"&gt;Apple&lt;/a&gt;&lt;br&gt;。我该如何解决这个问题?是否可以使用两种方式兼容的代码?

我不希望建立从产品到公司的第二次“联系”——这需要从一个页面而不是两个页面来处理。自定义帖子类型也不行。

更新 - 来自Querying relationship fields 文档的新代码:

<?php 

$related_articles = get_posts(array(
    'post_type' => 'post',
    'meta_query' => array(
        array(
            'key' => 'related_articles', // name of custom field
            'value' => '"' . get_the_ID() . '"',
            'compare' => 'LIKE'
        )
    )
));
?>

<?php if( $related_articles ): ?>
    <?php foreach( $related_articles as $article ): ?>
        <a href="<?php echo get_permalink( $article->ID ); ?>"> <?php echo get_the_title( $article->ID ); ?></a><br>
    <?php endforeach; ?>
<?php endif; ?>

这不会返回任何东西(空白)。

【问题讨论】:

    标签: php wordpress advanced-custom-fields


    【解决方案1】:

    这当然是可能的 - ACF 允许对相关项目进行 2 路或反向查询。因此,您的产品页面,您可以执行以下操作(使用您自己的相关 acf 名称)

    global $post;
    $related_company = get_posts(
        array(
            'post_type'=>'company',
            'meta_query'=>array(
                array(
                    'key' => 'related_product',
                    'value' => '"'.$post->ID.'"',
                    'compare' => 'LIKE'
                )
            )
    ));
    

    整个过程在ACF网站上有详细说明:http://www.advancedcustomfields.com/resources/querying-relationship-fields/

    虽然,如果不使用自定义帖子类型这是否可能,我不确定。

    【讨论】:

    • 我根据stackoverflow.com/a/27446295/2531736更新了我的问题'post_type'=&gt;'post'应该可以工作。它没有返回任何东西。
    • 您系统中的公司和产品有什么区别。它们是类别还是自定义帖子类型?
    • 都是页面。将'post_type' =&gt; 'post', 更改为'post_type' =&gt; 'page', 并使用&lt;?php echo $article-&gt;post_title; ?&gt; 而不是&lt;?php echo get_the_title( $article-&gt;ID ); ?&gt; 似乎使它工作。它似乎只适用于“产品”页面而不是“公司”页面。
    • 别在意最后一部分,&lt;?php echo get_the_title( $article-&gt;ID ); ?&gt; 也一样好用。
    猜你喜欢
    • 2013-09-10
    • 2016-09-23
    • 2017-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-12
    • 1970-01-01
    相关资源
    最近更新 更多