WordPress.org

Ready to get started?Download WordPress

Forums

Shortcode printing the_content outside of html code (8 posts)

  1. gashface
    Member
    Posted 1 year ago #

    I am working on creating a shortcode, but my code is printing the_content outside of the div I am telling it to in my functions file.

    function get_event_container() {
        global $post;
        $ret = '<section class="event_container clearfix">';
        $ret = $ret .  get_event_details(false, true);
        $ret = $ret . '<div class="right-column-long">' . the_content($post->ID) . '</div>';
        $ret =  $ret . '</section>';
    
        return $ret;
    }
    
    function get_event_details() {
        global $post;
        $unixtime = get_post_meta($post->ID, 'event_date', true);
    
        $ret = '<div class="left-image">';
        $ret =  $ret . '<h3>' . $post->post_title . '</h3>';
    
        $ret = $ret . '<p><h4>'.get_post_meta($post->ID, 'event_location', true) . '</h4>';
        $ret = $ret . '</p><p>';
        $ret = $ret . date("F d, Y", $unixtime) . '<br/>';
        $ret = $ret . '<em>' . get_post_meta($post->ID, 'event_start_time', true) . ' - ';
        $ret = $ret . get_post_meta($post->ID, 'event_end_time', true) . '</em>';
     	$ret = $ret . '</div>';
    
        return $ret;
    }

    It is printing the following markup:

    <section class="event_container clearfix"><div class="left-image"><h3>test</h3><p><h4></h4></p><p>October 13, 201211:30 am - 1:30 pm</div><div class="right-column-long"></div></section><p>Testing description area</p>

    So here the_content is printing after the end div and section tag, any ideas why? Confused...

    Is there a posiblity it's because the loop in my actual page is calling the_content(); also, if so how should I reference these differently?

  2. s_ha_dum
    Member
    Posted 1 year ago #

    the_content() echoes content as soon as it runs. The very first line in its Codex entry is "Displays the contents of the current post." You don't want that. You want get_the_content()-- please follow the link and follow the instructions or you will not get the results you expect.

  3. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

  4. s_ha_dum
    Member
    Posted 1 year ago #

    ^^ that is what I meant 0_o ^^

  5. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    I know ;-)

  6. gashface
    Member
    Posted 1 year ago #

    Cool so how do I use the

    <?php
    $content = apply_filters('the_content', $content);
    $content = str_replace(']]>', ']]>', $content);
    ?>

    In my functions file, just delcaring $content inside my functions file doesn't work, but I need to keep the formatting from the_content which get strips out

  7. gashface
    Member
    Posted 1 year ago #

    Notice: Undefined variable: content in /var/www/vhosts/thestudio.effectdigital.com/httpdocs/wp-content/themes/griffin-studio/functions.php on line 332

    function get_event_container() {
        global $post;
       	$content = apply_filters('the_content', $content);
        $content = str_replace(']]>', ']]>', $content);
    
        $ret = '<section class="event_container clearfix">';
        $ret = $ret .  get_event_details(false, true);
        $ret = $ret . '<div class="right-column-long"><div class="right-text">' . $content . '</div></div>';
        $ret =  $ret . '</section>';
    
        return $ret;
    }
  8. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    you ommitted this line from the quoted link:

    <?php $content = get_the_content(); ?>

    in your case, you could also use:

    $content = $post->post_content;

    in total:

    function get_event_container() {
        global $post;
       $content = $post->post_content;
    	$content = apply_filters('the_content', $content);
        $content = str_replace(']]>', ']]>', $content);
    
        $ret = '<section class="event_container clearfix">';
        $ret = $ret .  get_event_details(false, true);
        $ret = $ret . '<div class="right-column-long"><div class="right-text">' . $content . '</div></div>';
        $ret =  $ret . '</section>';
    
        return $ret;
    }

Topic Closed

This topic has been closed to new replies.

About this Topic