我尝试在Joomla 3中构建一个简单的FAQ模块。
我在模块后端有4个字段。问题1 &;问题2 & &;回答2"。
我的php和Html是这样构建的:
<div id="accordion">
<h3 class="question"> <?php echo $params->get('question1'); ?></h3>
<div class="question">
<?php echo $params->get('answer1'); ?>
</div>
<h3 class="question"> <?php echo $params->get('question2'); ?></h3>
<div class="question">
<?php echo $params->get('answer2'); ?>
</div>
</div>
我喜欢添加一些php的if语句,这样代码块只呈现,如果文本字段在后端,如果他们是由用户填写:
(如果字段"question3"有任何文本,显示此代码)
<?php if ($this->params->get('question3')) : ?> //This is the line of code i do not know how to do.
<h3 class="question"> <?php echo $params->get('question2'); ?></h3>
<div class="question">
<?php echo $params->get('answer2'); ?>
</div>
</div>
<?php endif; ?>
XML是这样的:
<!-- Question 1 -->
<field
label="Question 1"
default=""
name="question1"
description="Fill in the question"
type="text"
size="60"
/>
<!-- End of Question 1 -->
<!-- Answer 1 -->
<field
label="Answer1"
default="Fill in the answer"
name="answerswer1"
description="Fill in the answer"
type="editor"
width="200"
height="100"
hide="readmore,pagebreak"
filter="safehtml"
size="260"
/>
<!-- End of Answer 1 -->
<field type="spacer" name="questionspacer2" label="<b>Question and Answer 2 1</b>" />
<!-- Question 2 -->
<field
label="Question 2"
default=""
name="question2"
description="Fill in the question"
type="text"
size="60"
/>
<!-- End of Question 2 -->
<!-- Answer 2 -->
<field
label="Answer2"
default=""
name="answerswer2"
description="Fill in the answer"
type="editor"
width="200"
height="100"
hide="readmore,pagebreak"
filter="safehtml"
size="260"
/>
<!-- End of Answer 2 -->
任何提示都会很棒!我已经找了一晚上了
根据您如何设置参数,如果没有填写,您需要检查输出如下:
$answer2 = $params->get('answer2'));
// (Applies if the not filled data returns NULL etc.)
if ( isset($answer2) ) { YOUR CODE }
// (Applies if the not filled data returns an empty string)
if ( !empty($answer2) ) { YOUR CODE }
没有测试,但应该可以工作;)
编辑:就像下面的评论中讨论的那样,让它看起来像这样:)