Forums

[resolved] How do I add PHP code to a shortcode? (2 posts)

  1. michealp
    Member
    Posted 7 months ago #

    I am trying to use a shortcode in the single.php file of my theme that allows me to display text inside a box that is generated by the shortcode. What I want to do is make it display the excerpt for each post inside that box.

    Here is the shortcode for the box as it's displayed on my site:

    <?php echo do_shortcode('[box type="tick" style="rounded" border="full"]<center><h3><b>The Offer</b></h5></center>[/box]'); ?>

    Here's an example of it in use on my site: (it's the greenbox with the rounded corners)
    http://imediaprofits.com/directory/listing/bar-1/

    When I place the php code for the excerpt into the shortcode, it does not display. Here is what I tried to do to get the excerpt to display inside the shortcode:

    <?php echo do_shortcode('[box type="tick" style="rounded" border="full"]<center><h3><b>The Offer</b></h5></center><?php the_excerpt(); ?>[/box]'); ?>

    The excerpt that should be pulled and placed into the box is displayed just below the box on the example page I listed above. It says "Free cover between 7pm and 9pm." I want that to display inside that box to draw attention to it and make it easy for visitors to see.

    Can anyone tell me how to get it to display the way I want it to? I'm not sure if I'm doing something wrong, or if what I'm trying is just not possible.

    Thanks,
    Mike

  2. michealp
    Member
    Posted 7 months ago #

    Ok, I figured it out at this site:

    http://wordpress.stackexchange.com/questions/21840/help-with-wordpress-function-inside-a-shortcode

    Here is the code I had to use to do what I wanted:

    <?php echo do_shortcode('[box type="tick" style="rounded" border="full"] <center><h3><b>The Offer</b></h5></center>'.get_the_excerpt().' [/box]');?>

    Essentially I had to strip out some of the code. I was trying to put the full code for pulling the excerpt, the code I was using to pull the excerpt was:

    <?php the_excerpt(); ?>

    What ended up making it work was this:

    '.get_the_excerpt().'

    So if you're looking to use PHP code inside some other PHP code, keep this in mind.

Reply

You must log in to post.

About this Topic