【问题标题】:ACF Relationship Query in the Functions.php to pull into Query ID in Elementor Posts WidgetFunctions.php 中的 ACF 关系查询以拉入 Elementor Posts 小部件中的查询 ID
【发布时间】:2022-02-07 00:37:16
【问题描述】:

我正在尝试在特定类别页面中创建“精选帖子”。我已经建立了一个关系 ACF,这部分 PHP 可以正常工作并生成一个帖子 ID 数组。

$term = get_queried_object();
$things = get_field("catfeatured", $term);

我正在尝试将其转换为一个查询,我可以粘贴在 functions.php 中,然后使用查询 ID 将其拉入我的 elementor 帖子小部件。我已经搞砸了 2 天了,但我似乎离得更近了。这是一长串不起作用的事情中的最新一个:

add_action( 'elementor/query/Cat_Featured', function( $query ) {
    global $post;
    $term = get_queried_object();
    $ids = get_field("catfeatured", $term);
    $query->set( 'post__in', $ids );
});

请有人让我摆脱痛苦。

【问题讨论】:

  • 你为什么不尝试使用简码方法?:wpcodeus.com/how-to-add-php-code-to-elementor
  • 因为我想使用帖子小部件,所以我需要一个查询 ID。
  • 您可以在页面中获取查询ID并将其传递给短代码的函数
  • 你是什么意思?原谅我的无知。可以举个例子吗?
  • 使用$term = get_queried_object(); 获取分类页面中的分类ID;然后调用函数 my_elementor_shortcode( $term );然后你可以在functions.php文件中使用短代码函数中的$term

标签: php wordpress elementor


【解决方案1】:

在分类页面中:

$term = get_queried_object();                                 
my_elementor_shortcode( $term );

然后在functions.php中

    my_elementor_shortcode( $term ) {
$things = get_field("catfeatured", $term); foreach ($things as $value) { echo "$value <br>"; } add_shortcode( 'my_elementor_php_output', 'my_elementor_shortcode');

然后你可以在 elementor 中使用简码 [my_elementor_php_output] 使用简码小部件

【讨论】:

  • 非常感谢,但我不想使用页面中的简码来显示结果,我想使用帖子小部件,这意味着我需要一个查询 ID。
  • 请查看这些文章,您可以结合这些文章来获得答案:developers.elementor.com/docs/hooks/custom-query-filterwordpress.stackexchange.com/questions/172003/…
  • 谢谢。在过去的两天里,我已经经历了 100 次。我有一些令人愤怒的知识差距。
  • 希望这能回答你的问题:github.com/elementor/elementor/issues/5505
  • 谢谢。但是,它不是一个页面。这是我要显示特色帖子的类别档案。我们似乎离答案越来越远了:(简而言之,我想创建一个查询,该查询使用当前类别提取一个名为“catfeatured”的 ACF,并生成一个帖子 ID 数组。我希望它们传递到 elementor pro通过“查询 ID”发布小部件。
猜你喜欢
  • 2021-06-09
  • 2021-08-17
  • 2017-06-20
  • 2021-01-03
  • 2023-04-07
  • 2021-07-29
  • 2021-08-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多