微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

WordPress自定义简码输出内容的开始

如何解决WordPress自定义简码输出内容的开始

我正在尝试在我的网站上创建ajax后过滤器,但是类别下拉列表始终在内容的开头而不是内联输出。返回;会正确输出,但会终止函数,但是echo似乎会引起问题。有帮助吗?

 <?PHP 
function blogfiltercustom_function() {
     return '<form action="http://example.com/wp-admin/admin-ajax.PHP" method="POST" id="filter">' ;}

function blogfilterselect_function(){
if( $terms = get_terms( array(
    'taxonomy' => 'category',// to make it simple I use default categories
    'orderby' => 'name'
) ) ) : 
    // if categories exist,display the dropdown#
    
    echo '<select name="categoryfilter"><option value="">Select category...</option>';
    foreach ( $terms as $term ) :
        echo '<option value="' . $term->term_id . '">' . $term->name . '</option>'; // ID of the category as an option value
    endforeach;
        echo '</select>';
    
endif; 

        }
    
function blogfilterclose_function(){
    return '<input type="text" name="price_min" placeholder="Min price" />
    <input type="text" name="price_max" placeholder="Max price" />
    <label>
        <input type="radio" name="date" value="ASC" /> Date: Ascending
    </label>
    <label>
        <input type="radio" name="date" value="DESC" selected="selected" /> Date: Descending
    </label>
    <label>
        <input type="checkBox" name="featured_image" /> Only posts with featured images
    </label><button>Apply filter</button>
    <input type="hidden" name="action" value="myfilter">
</form>
<div id="response"></div>'
;}

add_shortcode('blogfiltercustom','blogfiltercustom_function');
add_shortcode('blogfilterselect','blogfilterselect_function');
add_shortcode('blogfilterclose','blogfilterclose_function');

?> 

解决方法

问题是echo函数应返回内容而不回显结果。 您可以删除回声并返回最后一个字符串,也可以使用ob_start()ob_get_clean()

只需添加功能

 blogfilterselect_function() {
 ob_start();
 // your Code
return ob_get_clean();
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。