在WordPress的CMS端运行的PHP

时间:2017-01-07 作者:Kirk

我有一些自定义PHP,可以创建一个短代码。我必须在这里有一些错误代码,因为它显示在Wordpress的后端(编辑端)。我正在使用Divi主题,并将其放在一个文本模块中,其中包含一个类“SearchBarArea”。因此,这是它应该位于的DIV的名称,但在前端,它不在该DIV中,它位于“entry content”之后。有什么想法吗?

function searchbar_function() {//Creates shortcode [searchbar]
$MainCategories = get_categories( array(
    \'orderby\' => \'name\',
    \'parent\'  => 0
) );//Grabs just the parent categories
?>
<form action="action_page.php">
<?php 
foreach ( $MainCategories as $MainCategory ) {
    $MainCatID=esc_html($MainCategory->term_id);//Gets Main Category ID
    if($MainCatID!==\'1\' && $MainCatID!==\'153\'){//Exclude Uncategorized or Featured from Main Category List
    $MainCatURL=esc_url( get_category_link( $MainCategory->term_id ) );//Gets Main Category URL
    $MainCatName=esc_html( $MainCategory->name );//Gets Main Category Name
    $MainCatSlug=esc_html( $MainCategory->slug );//Gets Main Category Slug
    $MainCatCount=esc_html( $MainCategory->category_count );
    $MainCat=\'<label>\'.$MainCatName.\'</label>\';
        echo $MainCat;
    $MainCatCode=\'<option value="\'.$MainCatSlug.\'">ALL \'.$MainCatName.\'</option>\';?>
        <select name="<?php echo $MainCatSlug?>-dropdown"> 
    <option value=""><?php echo esc_attr_e( \'Select:\', \'textdomain\' ); ?></option> 
    <?php 
        echo $MainCatCode;//Makes Main Category first choice
    $categories = get_categories( array( \'child_of\' => $MainCatID ) );//Number is the Main Category ID 
    foreach ( $categories as $category ) {
        $SubCatValueName= esc_attr($category->category_nicename);
        $SubCatValue= esc_attr( \'/category/archives/\' . $category->category_nicename );
        $SubCatName=esc_html( $category->cat_name );
        $SubCatCount=esc_html( $category->category_count );
        $SubCatOption =\'<option value="\'.$SubCatValueName.\'">\'.$SubCatName.\'</option>\';
        echo $SubCatOption;
    }
    ?>
        </select>
        <?php
    }
}?>
<input type="submit" value="Search">
        </form>
<?php
}
add_shortcode( \'searchbar\', \'searchbar_function\' );

3 个回复
SO网友:CompuSolver

即使缺少open-php标记,这也不会像您尝试的那样工作。Divi代码模块接受html代码和PHP短代码。我还没有尝试javascript,所以对此不确定。

您需要将代码集成为一个独立的插件。之后,您可以在Divi代码模块中使用shortcode,也应该能够在文本选项卡模式中使用它。

SO网友:Kirk

我想出来了。我重新排列了HTML并使用了变量,这样代码就不会中断。我还删除了

esc_attr_e( \'Select:\', \'textdomain\' );
因为它导致结果出现在DIV之外,所以插入了shortcode。

function searchbar_function() {//Creates shortcode [searchbar]
$MainCategories = get_categories( array(
    \'orderby\' => \'name\',
    \'parent\'  => 0
) );//Grabs just the parent categories
$CodeResult.=\'<form action="action_page.php">\';

foreach ( $MainCategories as $MainCategory ) {
    $MainCatID=esc_html($MainCategory->term_id);//Gets Main Category ID
    if($MainCatID!==\'1\' && $MainCatID!==\'153\'){//Exclude Uncategorized or Featured from Main Category List
    $MainCatURL=esc_url( get_category_link( $MainCategory->term_id ) );//Gets Main Category URL
    $MainCatName=esc_html( $MainCategory->name );//Gets Main Category Name
    $MainCatSlug=esc_html( $MainCategory->slug );//Gets Main Category Slug
    $MainCatCount=esc_html( $MainCategory->category_count );
    $MainCat=\'<label>\'.$MainCatName.\'</label>\';
    $MainCatCode=\'<option value="\'.$MainCatSlug.\'">ALL \'.$MainCatName.\'</option>\';
    $CodeResult.=$MainCat;
    $SelectCode=\'<select name="\'.$MainCatSlug.\'-dropdown"><option value="">Select:</option>\';
        $CodeResult.=$SelectCode;
        $CodeResult.=$MainCatCode;//Makes Main Category first choice
    $categories = get_categories( array( \'child_of\' => $MainCatID ) );//Number is the Main Category ID 
    foreach ( $categories as $category ) {
        $SubCatValueName= esc_attr($category->category_nicename);
        $SubCatValue= esc_attr( \'/category/archives/\' . $category->category_nicename );
        $SubCatName=esc_html( $category->cat_name );
        $SubCatCount=esc_html( $category->category_count );
        $SubCatOption =\'<option value="\'.$SubCatValueName.\'">\'.$SubCatName.\'</option>\';
        $CodeResult.=$SubCatOption;
    }
        $CodeResult.=\'</select>\';
    }
}
$SubmitEnd=\'<input type="submit" value="Search">
        </form>\';
$CodeResult.=$SubmitEnd;
    return $CodeResult;
}
add_shortcode( \'searchbar\', \'searchbar_function\' );

SO网友:Thinkerman

使用ob_start() 在开始使用短代码之前和短代码之后

$my_clean_ob = ob_get_clean();
return $my_clean_ob;
希望有帮助。

相关推荐

Namespaced shortcode?

我正在改造一个旧的WP站点,该站点有许多自定义的短代码,显然由于代码当前的组织方式,这些短代码在性能方面付出了代价。当然,我可以修复优化不好的代码,使用十几个短代码,并且一天就可以完成,但我想知道如何更好地组织它们。根据WordPress\'documentation, 建议将它们放在插件中并在上初始化init. 我们可以通过这样“命名”它们来减少这个钩子中的负载吗?[com.company shortcode attr=\"attr\" prop=\"prop\"] 有人尝试过这样的解决方案吗