简体   繁体   中英

How to insert PHP code properly in the HTML tag which is being added to variable and returned in a taxonomy?

My PHP code gets ignored or I get unexpected error on the page. I just can't find the way to include a line of PHP between HTML code and then assign that HTML code to the variable later on so I could return it in my custom made taxonomy. I know this is a noob question, but other examples was not working for me neither. I'm trying to assign the HTML with some PHP code to $out variable. Here is my code:

add_shortcode('Use_Case', 'use_case_shortcode_query');
function use_case_shortcode_query($atts, $content){
    global $use_case_categories;
  extract(shortcode_atts(array( // a few default values
   'posts_per_page' => '11',
   'post_type' => 'use_cases',
        )
   , $atts));

  global $post;

  $posts = new WP_Query(array( // a few default values
   'posts_per_page' => '11',
   'post_type' => 'use_cases'
        ));
    //print_r($posts);
  $output = '';

$out = '';
$out .= '<ul class="nav nav-tabs nav-justified">
    <li class="active">
        <a data-toggle="tab" href="#all">All</a>
    </li>
    <?php foreach($use_case_categories as $use_case_category) { ?>
        <li>
            <a href="# .return $use_case_category->slug. " data-toggle="tab"><?php return $use_case_category->name ?></a>
        </li>
    <?php } ?>
</ul>';
?>

Be careful with your php open end close tags. To make it more readable and easier to check, you can do it this way :

$out .= '<ul class="nav nav-tabs nav-justified">';
$out .= '<li class="active">';
$out .= '<a data-toggle="tab" href="#all">All</a>';
$out .= '</li>';
foreach($use_case_categories as $use_case_category):
$out .= '<li>';
  $out .= '<a href="#' . $use_case_category->slug . '" data-toggle="tab">' . $use_case_category->name . '</a>';
$out .= '</li>';
endforeach;
$out .= '</ul>';

And if you want to compact it :

$out .= '<ul class="nav nav-tabs nav-justified"><li class="active"><a data-toggle="tab" href="#all">All</a></li>';
foreach($use_case_categories as $use_case_category):
  $out .= '<li><a href="#' . $use_case_category->slug . '" data-toggle="tab">' . $use_case_category->name . '</a></li>';
endforeach;
$out .= '</ul>';

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