gpt4 book ai didi

php - 必须是数组类型,字符串给定

转载 作者:行者123 更新时间:2023-12-05 06:15:26 26 4
gpt4 key购买 nike

我正在尝试创建自定义元素或小部件;但是 add_link_attributes 似乎不支持链接的字符串。由于弹出以下错误:

Must be of the type array, string given

如何将保存在 $course_link 中的这个字符串变成“类型数组”?

protected function render() {
$settings = $this->get_settings_for_display();
$current_user = wp_get_current_user();
$course_link = get_post_permalink( $settings['training']);

$this->add_render_attribute( 'wrapper', 'class', 'elementor-button-wrapper' );

//Als gebruiker géén toegang heeft tot de training plaats saleslink
if (get_user_meta( $current_user->ID, 'course_' . $settings['training'] . '_access_from' , true ) || current_user_can('administrator')) {
$this->add_link_attributes( 'button', $course_link );
$this->add_render_attribute( 'button', 'class', 'elementor-button-link' );
}
elseif ( ! empty( $settings['sales-link']['url'] ) && !get_user_meta( $current_user->ID, 'course_' . $settings['training'] . '_access_from' , true )) {
$this->add_link_attributes( 'button', $settings['sales-link'] );
$this->add_render_attribute( 'button', 'class', 'elementor-button-link' );
}

if ( ! empty( $settings['size'] ) ) {
$this->add_render_attribute( 'button', 'class', 'elementor-size-' . $settings['size'] );
}

$this->add_render_attribute( 'button', 'class', 'elementor-button' );
$this->add_render_attribute( 'button', 'role', 'button' );

if ( $settings['hover_animation'] ) {
$this->add_render_attribute( 'button', 'class', 'elementor-animation-' . $settings['hover_animation'] );
}

?>
<div <?php echo $this->get_render_attribute_string( 'wrapper' ); ?>>
<a <?php echo $this->get_render_attribute_string( 'button' ); ?>>
<?php $this->render_text(); ?>
</a>
</div>
<?php
}

最佳答案

你可以像这样类型转换一个字符串(或其他)到一个数组:

$course_link = (array) $course_link;

因此,在您的函数中,您可以将第四行编辑为:

$course_link = (array) get_post_permalink( $settings['training'] );

或者第八行:

$this->add_link_attributes( 'button', (array) $course_link );

在这里您可以阅读more

关于php - 必须是数组类型,字符串给定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62536502/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com