简体   繁体   English

在echo do_shortcode()函数中使用php语句(WordPress)

[英]Using a php statement within a echo do_shortcode() function (WordPress)

I've been looking for a solution to this, but can't see to the right way to implement this. 我一直在寻找解决方案,但是找不到实现该方案的正确方法。

Here is the page I am working on: 这是我正在处理的页面:

http://indiaelder.sites.uofmhosting.net/health-topics/alzheimers-disease/ http://indiaelder.sites.uofmhosting.net/health-topics/alzheimers-disease/

As you can see, this is a placeholder for a very early medical page. 如您所见,这是一个非常早期的医学页面的占位符。 I have a tabbed menu that list (with some filler text) a bunch of information about Alzheimer's disease, organized by categories (symptoms, causes, etc). 我有一个选项卡式菜单,其中列出了一些有关阿尔茨海默氏病的信息(带有一些填充文字),这些信息按类别(症状,原因等)进行组织。

This is achieved in my code by doing the following: 这是通过执行以下操作在我的代码中实现的:

<?php
/*This gets the shortcode code to open the tabs */
                echo do_shortcode('
                [su_tabs vertical="yes"]
                    [su_tab title="Symptoms"][acf field="symptoms"][/su_tab]
                    [su_tab title="Causes"][acf field="causes"][/su_tab]
                    [su_tab title="Prognosis"][acf field="prognosis"][/su_tab]
                    [su_tab title="Diagnosis"][acf field="diagnosis"][/su_tab]
                    [su_tab title="Treatment"][acf field="treatment"][/su_tab]
                    [su_tab title="Prevention"][acf field="prevention"][/su_tab]
                [/su_tabs]
                ');
                ?>

I would like to make it so that if a user does not enter any information in one of these fields in the backend, that the corresponding line will not appear. 我要这样做,以便如果用户未在后端的这些字段之一中输入任何信息,则相应的行将不会出现。

So if I had a variable named $symptoms, and I set that equal to the value of the field, I was thinking it'd be something like 因此,如果我有一个名为$ symptoms的变量,并且将其设置为等于该字段的值,那我想它就像

$symptoms = get_field('symptoms');

And run a conditional to see if it is not '', and then not echo the corresponding line (in this example, [su_tab title="Symptoms"][acf field="symptoms"][/su_tab] would not be included in the echo statement). 并运行一个条件来查看它是否不是“”,然后不回显相应的行(在此示例中,[su_tab title =“症状”] [acf field =“症状”] [/ su_tab]将不包括在内echo语句)。

I've had a great deal of challenge so far in doing this. 到目前为止,我在执行此操作时遇到了很多挑战。 My first thought was to break the shortcode into parts, but this seems to break the functionality, so I think it all has to be echoed at once. 我的第一个想法是将短代码分成几部分,但这似乎破坏了功能,因此我认为必须立即将其全部回显。 I feel like their must be a way to do this, and I've seen some examples but nothing seems to apply to my particular case. 我觉得他们一定是这样做的一种方式,而且我已经看到了一些示例,但是似乎没有什么适用于我的特殊情况。 Does anyone have any thoughts on how I might do this? 有人对我该怎么做有任何想法吗?

Have you tried building your statement before calling the shortcode? 在调用短代码之前,您是否尝试过构建语句? Like this: 像这样:

<?php
    $output = 'shortcode_snippet = "foo"';
     if ( $var2 ) {
         $output .= 'shortcode_snippet2 = "bar"';   
    }

    echo do_shortcode('[ '. $output .' ]');
?>

That way you can get all of the logic out of the way prior to calling your shortcode. 这样一来,您就可以在调用短代码之前摆脱所有逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM