【问题标题】:How to not repeat some duplicate custom fields in Wordpress如何在 Wordpress 中不重复某些重复的自定义字段
【发布时间】:2017-05-15 23:51:38
【问题描述】:

我将举一个例子,询问我计划做的事情是否可以使用 Wordpress,以及最终如何。

[WordPress 网站] 完全通过自定义字段数据发布的帖子

Single post structure:
 - (A) Custom field artist name
 - (B) Custom field artist profil pic
 - (C) Custom field element description
 - (D) Custom field element image

Post #1:
 - Custom field artist name (John Cena)
 - Custom field artist profil pic (johncena.jpg)
 - Custom field element description (John Cena F-U to Hogan)
 - Custom field element image (johncena_fu_hogan.jpg)

Post #2:
 - Custom field artist name (John Cena)
 - Custom field artist profil pic (johncena.jpg)
 - Custom field element description (John Cena destroyed by Lesnar)
 - Custom field element image (johncena_F5eved.jpg)

Post #3:
 - Custom field artist name (John Cena)
 - Custom field artist profil pic (johncena.jpg)
 - Custom field element description (John Cena announces retirement)
 - Custom field element image (johncena_retirement.jpg)

Post #4:
 - Custom field artist name (Randy Orton)
 - Custom field artist profil pic (randyorton.jpg)
 - Custom field element description (Orton vs. Undertaker)
 - Custom field element image (orton_vs_taker.jpg)

Post #5:
 - Custom field artist name (Randy Orton)
 - Custom field artist profil pic (randyorton.jpg)
 - Custom field element description (Orton injured by Lesnar)
 - Custom field element image (orton_injury_lesnar.jpg)

如您所见,每篇关于某位艺术家的帖子都有相同的前 2 个自定义字段,用作介绍。而剩下的 2 个总是不同的。

现在我的问题...

有没有办法自定义搜索结果页面(由自定义搜索,标签或某个类别列表调用,通常是index.php)某个艺术家的所有帖子,仅显示前2个自定义字段一次,以及所有当前帖子的剩余 2 个字段?

示例: 网址:mysite.com/tag/john-cena

Result:
 - Custom field A
 - Custom field B

 - Custom field C
 - Custom field D

 - Custom field C
 - Custom field D

 - Custom field C
 - Custom field D

这在技术上是否可以通过插入某个变量来实现,例如“如果 A 和 B = true,请勿重复”??

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    对于标签/类别页面,您可以使用以下内容获取查询对象的 slug/名称,例如约翰塞纳

    $queried_object = get_queried_object(); var_dump( $queried_object );

    然后,您可以将其输入到元查询中,以收集所有将艺术家姓名字段作为查询对象的帖子。但是,要使其正常工作,您在字段中输入的名称需要与标签/类别的名称相匹配。

    元查询信息可以在这里找到:https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

    希望这会为您指明正确的方向。

    【讨论】:

    • 所以你的意思是这样的? $args = array('meta_value' => 'John Cena', 'meta_value' => 'johncena.jpg' ); $query = new WP_Query($args);
    • 更像 $args = array( 'meta_value' => 'John Cena', 'meta_key' => 'field_name' ); $query = new WP_Query($args);如果 field_name 是您为艺术家名称创建的自定义字段的名称
    猜你喜欢
    • 1970-01-01
    • 2020-06-25
    • 2019-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多