简体   繁体   中英

How to add PHP code inside PHP code?

For example (in my case) here's some code,

<?php
    woo_post_inside_before();   
    if ( $woo_options['woo_post_content'] != 'content' AND !is_singular() )
        woo_image( 'width='.$woo_options['woo_thumb_w'].'&height='.$woo_options['woo_thumb_h'].'&class=thumbnail '.$woo_options['woo_thumb_align'] );
    the_title( $title_before, $title_after );
    woo_post_meta();
?>

Now I would like to place the following PHP code so that it's output before woo_post_meta(); :

    <?php if (is_single()) : ?>
    <div class="testb"><img src="http://whatthenerd.com/what/wp-content/themes/canvas/300.jpg" alt="test Ad" /></div>
    <?php endif; ?>

If I have to show it literally, the code would be like this:

<?php
    woo_post_inside_before();   
    if ( $woo_options['woo_post_content'] != 'content' AND !is_singular() )
        woo_image( 'width='.$woo_options['woo_thumb_w'].'&height='.$woo_options['woo_thumb_h'].'&class=thumbnail '.$woo_options['woo_thumb_align'] );
    the_title( $title_before, $title_after );

    <?php if (is_single()) : ?>
    <div class="testthisad"><img src="http://whatthenerd.com/what/wp-content/themes/canvas/300.jpg" alt="test Ad" /></div>
    <?php endif; ?>

    woo_post_meta();
?>

Apparently that's not the right way of doing it. So, how do I do it?

Just have one set of tags:

<?php
    woo_post_inside_before();   
    if ( $woo_options['woo_post_content'] != 'content' AND !is_singular() )
        woo_image( 'width='.$woo_options['woo_thumb_w'].'&height='.$woo_options['woo_thumb_h'].'&class=thumbnail '.$woo_options['woo_thumb_align'] );
    the_title( $title_before, $title_after );

    if (is_single()){
?>
    <div class="testthisad"><img src="http://whatthenerd.com/what/wp-content/themes/canvas/300.jpg" alt="test Ad" /></div>
<?php
    }

    woo_post_meta();
?>

There's many ways to do it. Here's one.

<?php
woo_post_inside_before();   
if ( $woo_options['woo_post_content'] != 'content' AND !is_singular() )
{
     woo_image(   'width='.$woo_options['woo_thumb_w'].'&height='.$woo_options['woo_thumb_h'].'&class=thumbna il '.$woo_options['woo_thumb_align'] );
}
the_title( $title_before, $title_after );
?>
<?php if (is_single()) : ?>
    <div class="testthisad"><img src="http://whatthenerd.com/what/wp-     content/themes/canvas/300.jpg" alt="test Ad" /></div>
    <?php endif; ?>

<?php  woo_post_meta(); ?>

This will do what you want (not tested):

<?php
    woo_post_inside_before();   
    if ( $woo_options['woo_post_content'] != 'content' AND !is_singular() )
        woo_image( 'width='.$woo_options['woo_thumb_w'].'&height='.$woo_options['woo_thumb_h'].'&class=thumbnail '.$woo_options['woo_thumb_align'] );
    the_title( $title_before, $title_after );

    if (is_single()) : ? echo "[HTML code handling slashes]" /></div>;

    woo_post_meta();
?>

http://www.w3schools.com/php/php_if_else.asp?output=print

You can do

<?php
// Code...
eval('my code as a string');

Have in mind though that's crazy dangerous.

But in your case I think you can just do

<?php
    woo_post_inside_before();   
    if ( $woo_options['woo_post_content'] != 'content' AND !is_singular() )
        woo_image( 'width='.$woo_options['woo_thumb_w'].'&height='.$woo_options['woo_thumb_h'].'&class=thumbnail '.$woo_options['woo_thumb_align'] );
    the_title( $title_before, $title_after );

    if (is_single()) : ?>
    <div class="testthisad"><img src="http://whatthenerd.com/what/wp-content/themes/canvas/300.jpg" alt="test Ad" /></div>
    <?php endif; 
    woo_post_meta();
?>

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