如何在子主题模板中使用插件函数

时间:2017-02-07 18:42:19

标签: php wordpress

所以,

我一直在尝试制作一个运行插件功能的模板。 这是我试图制作的模板。 以下是我得到的错误

警告:在第39行的C:\ www \ htdocs \ FCN-GIT \ wp-content \ themes \ Avada-Child-Theme \ single-wpt_test.php中为foreach()提供的参数无效

  

致命错误:在第44行的C:\ www \ htdocs \ FCN-GIT \ wp-content \ themes \ Avada-Child-Theme \ single-wpt_test.php中调用未定义的方法WP :: doAction()

<?php
/**
* Template Name: Test
*
* @package WordPress
* @subpackage Avada
* @since 2016
*/
?>

<?php
// Can be overriden in your theme as entry-content-wpt-test-fill-form.php

/* @var $answerIdName string */
/* @var $answerIndex integer */
/* @var $isShowContent boolean */
/* @var $formAttributes array */
/* @var $content string */
/* @var $subTitle string */
/* @var $shortDescription string */
/* @var $test WpTesting_Model_Test */
/* @var $questions WpTesting_Model_Question[] */
/* @var $isFinal boolean */
/* @var $isMultipleAnswers boolean */
/* @var $submitButtonCaption string */
/* @var $stepsCounter string */
/* @var $wp WpTesting_WordPressFacade */
/* @var $hiddens array */
?>
<div class="wpt_test fill_form">

<?php if ($isShowContent): ?>
<div class="content">
    <?php echo $content ?>
</div>
<?php endif ?>

<div class="content"><form
<?php foreach ($formAttributes as $key => $value):?>
    <?php echo $key ?>="<?php echo htmlspecialchars(is_array($value) ? json_encode($value) : $value) ?>"
<?php endforeach ?>>
<?php if ($subTitle): ?><h2 class="subtitle"><?php echo $subTitle ?></h2><?php endif ?>
<?php if ($shortDescription): ?><div class="short-description"><?php echo $wp->autoParagraphise($shortDescription) ?></div><?php endif ?>
<?php $wp->doAction('wp_testing_template_fill_form_questions_before') ?>
<?php foreach($questions as $q => $question): /* @var $question WpTesting_Model_Question */ ?>
    <?php $wp->doAction('wp_testing_template_fill_form_question_before', $question, $q) ?>
    <div class="question">

        <div class="title">
            <span class="number"><?php echo $q+1 ?>.</span><span class="title"><?php echo $question->getTitle() ?>
            <?php $wp->doAction('wp_testing_template_fill_form_label_end', array('required' => true)) ?></span>
        <?php if (!$isMultipleAnswers): ?>
            <input type="hidden" name="<?php echo $answerIdName ?>[<?php echo $answerIndex ?>]" value="" />
        <?php endif ?>
        </div>

    <?php foreach($question->buildAnswers() as $a => $answer): /* @var $answer WpTesting_Model_Answer */ ?>
        <?php $answerId = 'wpt-test-question-' . $question->getId() . '-answer-' . $answer->getId() ?>

        <div class="answer">

            <label for="<?php echo $answerId ?>">
                <input type="<?php echo $isMultipleAnswers ? 'checkbox' : 'radio' ?>" id="<?php echo $answerId ?>"
                    data-errormessage="<?php echo $isMultipleAnswers
                        ? __('Please select at least one answer.', 'wp-testing')
                        : __('Please choose only one answer.', 'wp-testing') ?>"
                    <?php if (0 == $a): ?>required="required" aria-required="true"<?php endif ?>
                    name="<?php echo $answerIdName ?>[<?php echo $answerIndex ?>]" value="<?php echo $answer->getId() ?>" />
                <?php echo $answer->getTitleOnce() ?>
            </label>

        </div>
        <?php if ($isMultipleAnswers) {$answerIndex++;} ?>
    <?php endforeach ?>

    </div>
    <?php $wp->doAction('wp_testing_template_fill_form_question_after', $question, $q) ?>
    <?php if (!$isMultipleAnswers) {$answerIndex++;} ?>
<?php endforeach ?>
<?php $wp->doAction('wp_testing_template_fill_form_questions_after') ?>

<?php if($isFinal): ?>
    <p>
        <input type="submit" class="button" value="<?php echo $submitButtonCaption ?>" />
        <?php if($stepsCounter): ?><span class="steps-counter"><?php echo $stepsCounter ?></span><?php endif ?>
    </p>
<?php else: ?>
    <div class="wpt_warning">
        <h4><?php echo __('Test is under construction', 'wp-testing') ?></h4>
        <p><?php echo __('You can not get any results from it yet.', 'wp-testing') ?></p>
    </div>
<?php endif ?>
<?php foreach($hiddens as $name => $value): ?><input type="hidden" name="<?php echo htmlspecialchars($name) ?>" value="<?php echo htmlspecialchars($value) ?>" /><?php endforeach ?>
</form></div>

</div>

1 个答案:

答案 0 :(得分:0)

我设法解决了这个问题,但是重写了插件的默认模板,我需要有两个模板,这两个模板存在差异,基本上是一个模板,其中包含我的主页和另一个页面的代码。

主页有更改,而另一页没有,它使用默认主题模板。

这是我使用插件代码将两个模板合二为一的解决方案。

<?php
// Can be overriden in your theme as entry-content-wpt-test-fill-form.php

/* @var $answerIdName string */
/* @var $answerIndex integer */
/* @var $isShowContent boolean */
/* @var $formAttributes array */
/* @var $content string */
/* @var $subTitle string */
/* @var $shortDescription string */
/* @var $test WpTesting_Model_Test */
/* @var $questions WpTesting_Model_Question[] */
/* @var $isFinal boolean */
/* @var $isMultipleAnswers boolean */
/* @var $submitButtonCaption string */
/* @var $stepsCounter string */
/* @var $wp WpTesting_WordPressFacade */
/* @var $hiddens array */
?>

<?php if (is_front_page()): ?>
  <div class="home-health-insight wpt_test fill_form">

    <div class="answers">
      <div class="content">
        <form
        <?php foreach ($formAttributes as $key => $value):?>
          <?php echo $key ?>="<?php echo htmlspecialchars(is_array($value) ? json_encode($value) : $value) ?>"
        <?php endforeach; ?>>
        <?php if ($isShowContent): ?><div class="content" style="margin:0;"><?php echo $content ?></div><?php endif ?>    
        <?php if ($shortDescription): ?><div class="short-description"><?php echo $wp->autoParagraphise($shortDescription) ?></div><?php endif ?>
        <?php $wp->doAction('wp_testing_template_fill_form_questions_before') ?>
        <div class="question">      
          <div class="question-titles">
            <div class="question-labels">
              <label class="answerlbl"  for="<?php echo $answerId ?>">
                None of the time
              </label>
              <label class="answerlbl" for="<?php echo $answerId ?>">
                Rarely
              </label>
              <label class="answerlbl" for="<?php echo $answerId ?>">
                Some of the time
              </label>
              <label class="answerlbl" for="<?php echo $answerId ?>">
                Often
              </label>
              <label class="answerlbl"  for="<?php echo $answerId ?>">
                All of the time
              </label>      
            </div>  
          </div>                              
          <?php foreach($questions as $q => $question): /* @var $question WpTesting_Model_Question */ ?>
            <?php $wp->doAction('wp_testing_template_fill_form_question_before', $question, $q) ?>

            <div class="title">
              <span class="question-title" style="width:100%;"><?php echo $question->getTitle() ?>
                <?php $wp->doAction('wp_testing_template_fill_form_label_end', array('required' => true)) ?></span>
                <?php if (!$isMultipleAnswers): ?>
                  <input type="hidden" name="<?php echo $answerIdName ?>[<?php echo $answerIndex ?>]" value="" />
                <?php endif ?>
              </div>
              <div class="answer-block">
                <?php foreach($question->buildAnswers() as $a => $answer): /* @var $answer WpTesting_Model_Answer */ ?>
                  <?php $answerId = 'wpt-test-question-' . $question->getId() . '-answer-' . $answer->getId() ?>

                  <div class="answer">

                    <input type="<?php echo $isMultipleAnswers ? 'checkbox' : 'radio' ?>" id="<?php echo $answerId ?>"
                    data-errormessage="<?php echo $isMultipleAnswers
                    ? __('Please select at least one answer.', 'wp-testing')
                    : __('Please choose only one answer.', 'wp-testing') ?>"
                    <?php if (0 == $a): ?>required="required" aria-required="true"<?php endif ?>
                    name="<?php echo $answerIdName ?>[<?php echo $answerIndex ?>]" value="<?php echo $answer->getId() ?>" />

                  </div>
                  <?php if ($isMultipleAnswers) {$answerIndex++;} ?>
                <?php endforeach ?>
              </div>
              <?php $wp->doAction('wp_testing_template_fill_form_question_after', $question, $q) ?>
              <?php if (!$isMultipleAnswers) {$answerIndex++;} ?>
            <?php endforeach ?>
          </div>

          <?php $wp->doAction('wp_testing_template_fill_form_questions_after') ?>

          <?php if($isFinal): ?>
            <p>
              <input type="submit" class="button" value="<?php echo $submitButtonCaption ?>" />
              <?php if($stepsCounter): ?><span class="steps-counter"><?php echo $stepsCounter ?></span><?php endif ?>
            </p>
          <?php else: ?>
            <div class="wpt_warning">
              <h4><?php echo __('Test is under construction', 'wp-testing') ?></h4>
              <p><?php echo __('You can not get any results from it yet.', 'wp-testing') ?></p>
            </div>
          <?php endif ?>
          <?php foreach($hiddens as $name => $value): ?><input type="hidden" name="<?php echo htmlspecialchars($name) ?>" value="<?php echo htmlspecialchars($value) ?>" /><?php endforeach ?>
        </form>
      </div>
    </div>

  </div>
<?php else: ?>
  <div class="wpt_test fill_form">

  <?php if ($isShowContent): ?>
  <div class="content">
      <?php echo $content ?>
  </div>
  <?php endif ?>

  <div class="content"><form
  <?php foreach ($formAttributes as $key => $value):?>
      <?php echo $key ?>="<?php echo htmlspecialchars(is_array($value) ? json_encode($value) : $value) ?>"
  <?php endforeach ?>>
  <?php if ($subTitle): ?><h2 class="subtitle"><?php echo $subTitle ?></h2><?php endif ?>
  <?php if ($shortDescription): ?><div class="short-description"><?php echo $wp->autoParagraphise($shortDescription) ?></div><?php endif ?>
  <?php $wp->doAction('wp_testing_template_fill_form_questions_before') ?>
  <?php foreach($questions as $q => $question): /* @var $question WpTesting_Model_Question */ ?>
      <?php $wp->doAction('wp_testing_template_fill_form_question_before', $question, $q) ?>
      <div class="question">

          <div class="title">
              <span class="number"><?php echo $q+1 ?>.</span><span class="title"><?php echo $question->getTitle() ?>
              <?php $wp->doAction('wp_testing_template_fill_form_label_end', array('required' => true)) ?></span>
          <?php if (!$isMultipleAnswers): ?>
              <input type="hidden" name="<?php echo $answerIdName ?>[<?php echo $answerIndex ?>]" value="" />
          <?php endif ?>
          </div>

      <?php foreach($question->buildAnswers() as $a => $answer): /* @var $answer WpTesting_Model_Answer */ ?>
          <?php $answerId = 'wpt-test-question-' . $question->getId() . '-answer-' . $answer->getId() ?>

          <div class="answer">

              <label for="<?php echo $answerId ?>">
                  <input type="<?php echo $isMultipleAnswers ? 'checkbox' : 'radio' ?>" id="<?php echo $answerId ?>"
                      data-errormessage="<?php echo $isMultipleAnswers
                          ? __('Please select at least one answer.', 'wp-testing')
                          : __('Please choose only one answer.', 'wp-testing') ?>"
                      <?php if (0 == $a): ?>required="required" aria-required="true"<?php endif ?>
                      name="<?php echo $answerIdName ?>[<?php echo $answerIndex ?>]" value="<?php echo $answer->getId() ?>" />
                  <?php echo $answer->getTitleOnce() ?>
              </label>

          </div>
          <?php if ($isMultipleAnswers) {$answerIndex++;} ?>
      <?php endforeach ?>

      </div>
      <?php $wp->doAction('wp_testing_template_fill_form_question_after', $question, $q) ?>
      <?php if (!$isMultipleAnswers) {$answerIndex++;} ?>
  <?php endforeach ?>
  <?php $wp->doAction('wp_testing_template_fill_form_questions_after') ?>

  <?php if($isFinal): ?>
      <p>
          <input type="submit" class="button" value="<?php echo $submitButtonCaption ?>" />
          <?php if($stepsCounter): ?><span class="steps-counter"><?php echo $stepsCounter ?></span><?php endif ?>
      </p>
  <?php else: ?>
      <div class="wpt_warning">
          <h4><?php echo __('Test is under construction', 'wp-testing') ?></h4>
          <p><?php echo __('You can not get any results from it yet.', 'wp-testing') ?></p>
      </div>
  <?php endif ?>
  <?php foreach($hiddens as $name => $value): ?><input type="hidden" name="<?php echo htmlspecialchars($name) ?>" value="<?php echo htmlspecialchars($value) ?>" /><?php endforeach ?>
  </form></div>

  </div>
<?php endif ?>