WordPress的。如何在 if else 语句中使用简码?

Wordpress. How to use shortcode with if else statement?

我有以下代码,但它在文本中显示短代码,而不是显示短代码的结果。

$curr_user_id = get_current_user_id();
// the value is 0 if the user isn't logged-in
if ( $curr_user_id != 0 ) {
    // we know now the user is logged-in, so we can use his/her ID to get the user meta
    $um_value = get_user_meta( $curr_user_id, 'user_phone', true );
    // now we check if the value is correct
    if ( ! empty( $um_value ) && $um_value == 'French' ) {
        // if so we can output something
        echo '[ld_course_list course_category_name="french" col=4 progress_bar=true]';
    } else {
    // else the code, eg.
    echo '[ld_course_list course_category_name="english" col=4 progress_bar=true]';
}
}

上面的代码将生成下面的文本,而不是显示短代码的结果

[ld_course_list course_category_name="english" col=4 progress_bar=true]

如何更改我的代码以使其实际运行短代码?

谢谢。

以下代码有效。唯一的问题是 do_shortcode 破坏了样式表。有人知道为什么或如何解决它吗?

<?
$curr_user_id = get_current_user_id();
// the value is 0 if the user isn't logged-in
if ( $curr_user_id != 0 ) {
    // we know now the user is logged-in, so we can use his/her ID to get the user meta
    $um_value = get_user_meta( $curr_user_id, 'user_phone', true );
    // now we check if the value is correct
    if ( ! empty( $um_value ) && $um_value == 'French' ) {
        // if so we can output something
        echo do_shortcode('[ld_course_list course_category_name="french" col=4 progress_bar=true]');
    } else {
    // else the code, eg.
    echo do_shortcode('[ld_course_list course_category_name="english" col="4" progress_bar="true"]');
}
}
?>

您使用的方式不对,您尝试的方法在 wordpress 页面、帖子等中使用。

要在插件、主题或任何其他 .php 文件中使用,请使用名为 do_shortcode() 的 WordPress 函数,它可以让您直接在主题和插件中添加短代码。

这样添加:

echo do_shortcode("[ld_course_list course_category_name="english" col=4 progress_bar=true]");