我在函数中添加了此代码段。php文件,但它不执行foreach中的类别。它只显示关键字字段,工作正常,但不显示类别名称。
function jobsform_function($atts) {
return \'
<form class="home-job-search" method="GET" action="https://website.com/jobs/jobs/">
<div class="home-keywords">
<input type="text" id="search_keywords" name="search_keywords" placeholder="Enter Keywords" />
</div>
<div class="home-categories">
<select id="search_category" name="search_category">
<?php foreach ( get_job_listing_categories() as $cat ) : ?>
<option value="<?php echo esc_attr( $cat->term_id ); ?>">
<?php echo esc_html( $cat->name ); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="home-search-button">
<input type="submit" value="Search" />
</div>
</form>\';
}
add_shortcode(\'jobsform\', \'jobsform_function\');
HTML未读取foreach查询
<form class="home-job-search" method="GET" action="https://design.muntomedia.com/jobs/jobs/">
<div class="home-keywords">
<input type="text" id="search_keywords" name="search_keywords" placeholder="Enter Keywords">
</div>
<div class="home-categories">
<select id="search_category" name="search_category">
<!--?php foreach ( get_job_listing_categories() as $cat ) : ?-->
<option value="<?php echo esc_attr( $cat->term_id ); ?>">
<!--?php echo esc_html( $cat--->name ); ?>;
</option>
<!--?php endforeach; ?-->;
</select>
</div>
<div class="home-search-button">
<input type="submit" value="Search">
</div>
</form>
这就是结果
有什么建议吗?
SO网友:Awais
您将以一个大字符串的形式返回短代码内容get_job_listing_categories
未正确调用。
此外,我建议使用PHP输出缓冲,这对于WordPress短代码来说是非常好的。
请尝试以下代码:
function jobsform_function($atts) {
ob_start();
?>
<form class="home-job-search" method="GET" action="https://website.com/jobs/jobs/">
<div class="home-keywords">
<input type="text" id="search_keywords" name="search_keywords" placeholder="Enter Keywords" />
</div>
<div class="home-categories">
<select id="search_category" name="search_category">
<?php foreach (get_job_listing_categories() as $cat): ?>
<option value="<?php echo esc_attr($cat->term_id); ?>">
<?php echo esc_html($cat->name); ?>
</option>
<?php endforeach;?>
</select>
</div>
<div class="home-search-button">
<input type="submit" value="Search" />
</div>
</form>
<?php
$output = ob_get_contents();
ob_end_clean();
return $output;
}
add_shortcode(\'jobsform\', \'jobsform_function\');