带条件结果的快捷码(如果登录)

时间:2020-11-15 作者:Tiyo

我需要一些帮助,我正在通过wordpress学习php,所以我不太懂php的基本知识,也不太懂wordpress。所以我有一个短代码函数:

function links( $atts, $content = null ){
    $args = shortcode_atts( array(
        \'s\' => \'\',
        \'l\' => \'\',
    ), $atts);
    
    $api = file_get_contents(\'API URL\'.$args[\'l\']);
    $result1 = \'<a href="\'. $api .\'">\'. $args[\'s\'] .\'</a>\';
    $result2 = \'<a href="\'. $args[\'l\'] .\'">\'. $args[\'s\'] .\'</a>\';

    return THIS IS I NEED THE IF LOGGED IN SHOWS $result2 AND THE OTHERWISE
}
add_shortcode (\'link\', \'links\');
该快捷码已从foreach写入自定义元框组文本区域。

[link s="Google" l="https://google.com"]
我把这个写在single.php

echo do_shortcode($linkx[\'link_box\']);
提前谢谢你。

1 个回复
最合适的回答,由SO网友:bobbingwide 整理而成

使用is_user_logged_in() 在逻辑上决定返回什么。看见https://developer.wordpress.org/reference/functions/is_user_logged_in/

我想$linkx[\'link_box\'] 包含短代码。

相关推荐

Image Shortcode from ACF ID

我想创建一个快捷码,用于显示ACF自定义字段中的图像,该字段当前的返回格式设置为Image ID. 短代码需要接受post\\u id,以便从特定页面获取字段。因此,短代码是:[acf_featured_item post_id=\"88\"] 我知道有一个可用的ACF快捷码,但已经有另一个位置依赖于ID的返回格式。因此我无法更改此位置。我当前的代码是:function acf_featured_item_shortcode( $atts ) { // Attributes $a