简体   繁体   中英

PHP variable escapes outside of HTML tags

Im trying to show the title of the wordpress category, Im using a standard woocommerce hook, however it is behaving very weird, the PHP variable escapes outside of the html tags, any idea why?

This is the code:

add_action ( 'woocommerce_before_main_content', 'ebani_show_category_name' );
function ebani_show_category_name() {
    if ( is_product_category() ) {
        $category_title = single_cat_title();
        ?>
            <div class='container'>
                <div class='row ebani-title'>
                    <div class='col-xs-12'>
                        <h3 class='title'><?php $category_title ?></h3>
                    </div>
                </div>
            </div>
        <?php
    }
}

It prints in the page this way:

Category 1
<div class='container'>
    <div class='row ebani-title'>
         <div class='col-xs-12'>
              <h3 class='title'></h3>
         </div>
    </div>
</div>

As you can see the category shows above the tags, and the <h3> shows empty in the markup, Im not sure what am I doing wrong here.

single_cat_title title will echo the value when called by default. You can override this with the second param by passing in a false value. Just make sure to add the default value '' (empty string) as the first param if you are not adding a prefix.

Update to either:

add_action ( 'woocommerce_before_main_content', 'ebani_show_category_name' );
function ebani_show_category_name() {
    if ( is_product_category() ) {
        $category_title = single_cat_title('', false);
        ?>
            <div class='container'>
                <div class='row ebani-title'>
                    <div class='col-xs-12'>
                        <h3 class='title'><?php echo $category_title; ?></h3>
                    </div>
                </div>
            </div>
        <?php
    }
}

or

add_action ( 'woocommerce_before_main_content', 'ebani_show_category_name' );
function ebani_show_category_name() {
    if ( is_product_category() ) {
        ?>
            <div class='container'>
                <div class='row ebani-title'>
                    <div class='col-xs-12'>
                        <h3 class='title'><?php single_cat_title(); ?></h3>
                    </div>
                </div>
            </div>
        <?php
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM