How to add PHP code inside PHP code?

Go To StackoverFlow.com

2

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?

2012-04-04 18:11
by its_me
<div class="testbitch">? ^ - Dion 2012-04-04 18:15


8

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();
?>
2012-04-04 18:13
by Cal
Wow, that was easy, and clear as well. Perfect example! Thanks - its_me 2012-04-04 18:14
@AahanKrish this is the better way to do this - Hans Z 2012-07-25 12:46
@HansZ Thanks for the heads up. Marked it as the answer. : - its_me 2012-07-25 13:17


2

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(); ?>
2012-04-04 18:14
by aziz punjani


1

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

2012-04-04 18:16
by RyanS
Turns out you can just use '[code]' instead of " which is what I meant not slashes lol. Also I edited your post for you. Cheers.....http://www.w3schools.com/php/funcstringecho.as - RyanS 2012-04-04 18:23


0

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();
?>
2012-04-04 18:13
by gosukiwi
Thanks for the code. But can you please use it with the example code in my question? (Noob here. - its_me 2012-04-04 18:15
I'll edit my answer lol I cant post code her - gosukiwi 2012-04-04 18:17
sure, thanks : - its_me 2012-04-04 18:18
Ads