基本上,如果字段有值,我需要调用一个字段(包装在一个 div 中)。如果字段没有值,我不希望显示字段或 div。我的 PHP 知识很糟糕,但这是我必须使用的。
以下是提供给我的有关如何按 ID 调用自定义字段的说明:
$cust_1 = $this->fields->getFieldByCaption('Custom Text'); // getFieldByCaption() allow you to get the field by the Caption. This is not the best way to get a field since changing the caption in the back-end will break the reference.
echo '<br />Field ID: ' . $cust_1->getId();
$cust_2 = $this->fields->getFieldById(29); // getFieldById() is the ideal way of getting a field. The ID can be found at 'Custom Fields' section in Mosets Tree's back-end.
echo '<br />Name: ' . $cust_2->getName();
echo '<br />Has Caption? ' . (($cust_2->hasCaption()) ? 'Yes' : 'No');
echo '<br />Caption: ' . $cust_1->getCaption();
echo '<br />Value: ' . $cust_2->getValue();
echo '<br />Output: ' . $cust_2->getOutput(1);
echo '<hr />';
$this->fields->resetPointer();
while( $this->fields->hasNext() ) {
$field = $this->fields->getField();
echo '<br /><strong>' . $field->getCaption() . '</strong>';
echo ': ';
echo $field->getOutput(1); // getOutput() returns the formatted value of the field. ie: For a youtube video, the youtube player will be loaded
// echo $field->getValue(); // getValue() returns the raw value without additional formatting. ie: When getting value from a Online Video field type, it will return the URL.
$this->fields->next();
}
这是我正在使用的代码,需要帮助来尝试执行我需要的操作,如上所述:
<?php
if( !is_null($this->fields->getFieldById(35)) ) {
$value = $field->getValue();
$hasValue = $field->hasValue();
$field = $this->fields->getField();
if( $hasValue )
{
echo '<div class="lin" id="lphn">'; {
echo $cust_2 = $this->fields->getFieldById(35);
echo $cust_2->getOutput(1);
echo '</div>';
}
else {
// Print nothing.
}
}
}
?>