【问题标题】:Hook into a Posts Widget in Elementor挂钩到 Elementor 中的帖子小部件
【发布时间】:2022-08-03 21:24:32
【问题描述】:

我正在寻找一种连接到 Elementor Posts Widget 的方法,以便在每个帖子的帖子标题下显示一个额外的 H2 标签。

然后,我将从单个帖子 ACF 字段中获取此 H2 值。

从我正在阅读的其他内容来看,有一些方法可以将输出的整个 HTML 作为字符串获取,但这需要大量的字符串替换,因此不是很有未来的证明。例如: Hook into elementor widget? https://developers.elementor.com/docs/hooks/render-widget-content/

如果我使用这样的代码,有没有办法在帖子标题之后挂钩?还是字符串替换是解决这个问题的最佳方法?

function change_heading_widget_content( $widget_content, $widget ) {

if ( \'posts\' === $widget->get_name() ) {
    $settings = $widget->get_settings();
    $post_id = \"Somehow get the post id (maybe look for in the $widget_content string per post?)\";

    if ( ! empty( $settings[\'link\'][\'is_external\'] ) ) {
        $widget_content .= \'<h2>\'. get_field(\"extra_heading\", $post_id) .\'<h2>\';
    }
}

return $widget_content;

}
add_filter( \'elementor/widget/render_content\', \'change_heading_widget_content\', 10, 2 );

我感谢所有和任何帮助。 谢谢

    标签: php wordpress elementor


    【解决方案1】:

    如果您深入研究 Elementor Pro 源代码,您会发现一个很好的提示: Dynamic Tags -> ACF Module

    get_queried_object()
    

    或者试试这个: Dynamic Tags -> ACF Module Rendering

    function get_queried_object_meta( $meta_key ) {
            $value = '';
            if ( is_singular() ) {
                $value = get_post_meta( get_the_ID(), $meta_key, true );
            } elseif ( is_tax() || is_category() || is_tag() ) {
                $value = get_term_meta( get_queried_object_id(), $meta_key, true );
            }
    
            return $value;
        }
    

    或者只使用 get_field('my-field') 而不使用 $post_id

    【讨论】:

      猜你喜欢
      • 2021-03-13
      • 2021-08-31
      • 2021-07-19
      • 2016-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-02
      • 2022-12-11
      相关资源
      最近更新 更多