如何在PHP代码中添加PHP代码?

时间:2012-04-04 18:11:43

标签: php wordpress

例如(在我的例子中)这里是一些代码,

<?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();
?>

现在我想放置以下PHP代码,以便在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; ?>

如果我必须按字面意思显示它,代码将是这样的:

<?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();
?>

显然,这不是正确的做法。那么,我该怎么做呢?

4 个答案:

答案 0 :(得分:8)

只有一组标签:

<?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();
?>

答案 1 :(得分:2)

有很多方法可以做到这一点。这是一个。

<?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(); ?>

答案 2 :(得分:1)

这将做你想要的(未测试):

<?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

答案 3 :(得分:0)

你可以做到

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

请注意,虽然这很危险。

但在你的情况下,我认为你可以做到

<?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();
?>