【问题标题】:How to echo out a php variable into an html link in a wordpress shortcode如何将 php 变量回显到 wordpress 短代码中的 html 链接中
【发布时间】:2013-10-30 12:11:13
【问题描述】:

在我的短代码中,我动态抓取一些自定义帖子并显示它们。这些自定义帖子有一个标记为“url”的自定义字段。我要做的是从该自定义字段中获取值并将其放入锚标记的 href 中。问题是我似乎无法在短代码中使用 echo 。似乎函数 do_shortcode 可能是答案,但我不确定如何在我的情况下使用它。问题出在这一行:

$retour .= "<a href='".echo $meta_values;."'>";

这是短代码的其余代码

function sc_liste($atts, $content = null) {
    extract(shortcode_atts(array(
            "cat" => ''
    ), $atts));
    global $post;

    $myposts = get_posts('post_type=section_modules&category_name='.$cat.'&order=ASC');
    $retour = "<div class='container-fluid sectionBoxContainer'><div class='row-fluid'>";
    foreach($myposts as $post) :
    $meta_values = get_post_meta( $post->ID, 'url', true );
         $retour .= "<a href='".echo do_shortcode();."'>";
         $retour.="<div class='sectionBox span4'><h2>".$post->post_title."</h2><div class='hrule_black'></div><p>".$post->post_content."</p></div>";
         $retour .="</a>";
    endforeach;
    $retour .= "</div></div>";

    return $retour;


}

【问题讨论】:

    标签: php html wordpress shortcode


    【解决方案1】:

    您不会回显到变量中,您只需将其连接起来:

    $retour .= "<a href='".$meta_values."'>";
    echo $retour;
    

    【讨论】:

      【解决方案2】:

      您不应该在短代码中 echo 一个项目。 只需将它们存储在一个变量中并在短代码函数的末尾返回结果。在你的情况下,你可以这样做:

      $retour .= "&lt;a href='" . $meta_values . "'&gt;";

      最后——

      return $retour;

      【讨论】:

        【解决方案3】:

        也许我误解了你的问题,但是当你省略回声并将$meta_values 放入你的字符串时,这应该可以工作

        $retour .= "<a href='$meta_values'>";
        

        【讨论】:

          猜你喜欢
          • 2014-08-27
          • 2019-03-08
          • 1970-01-01
          • 2012-11-05
          • 2020-01-29
          • 1970-01-01
          • 2017-10-21
          • 1970-01-01
          • 2015-04-20
          相关资源
          最近更新 更多