Problem returns function inside shortcode in Wordpress

Hey guys, I have problems encoding shortcodes. I want the code to display a div with a submission counter function, and then a link with the contents of the code files as a URL.

View_count (); the function works fine when called inside the theme files, and I really managed to show it, but then it appears before the_content (); (gray column) when I wanted it within the content under the element.

(1) Here is what I have:

function video_block( $atts, $content = null ) { 
    return '<div class="video-block"><span class="ViewCount"><?php the_views(); ?></span> <a class="dl-source" href="' . $content . '">Link</a></div>';
}

(2) Here is the code that appears at the top of the page:

function video_block( $atts, $content = null ) { ?>
    <div class="video-block"><span class="ViewCount"><?php the_views(); ?></span> <a class="dl-source" href="<?php echo $content; ?>">Link</a></div>
<?php }

(3) This code displays the above views and the link in the right place:

function video_block( $atts, $content = null ) {
    $views = the_views();
    return '<div class="video-block"><span class="ViewCount"><?php $views; ?></span> <a class="dl-source" href="<?php echo $content; ?>">Link</a></div>';
}

- Wordpress, ( ) , , , html $content.

: http://nvrt.me/4Qf1 ( №2)

. , - .


EDIT:

the_views(); . , , , .

### Function: Display The Post Views
function the_views($display = true, $prefix = '', $postfix = '', $always = false) {
    $post_views = intval(post_custom('views'));
    $views_options = get_option('views_options');
    if ($always || should_views_be_displayed($views_options)) {
        $output = $prefix.str_replace('%VIEW_COUNT%', number_format_i18n($post_views), $views_options['template']).$postfix;
        if($display) {
            echo apply_filters('the_views', $output);
        } else {
            return apply_filters('the_views', $output);
        }
    }
    elseif (!$display) {
        return '';
    }
}
+3
3

, Wordpress , , , , .

, , , ( ;-)).

the_views() . $display , . $display True ( ), . $display False, .

, , :

1,

, the_views(), , : the_views (false)

<?php

function video_block( $atts, $content = null ) { 
  return "<div class=\"video-block\"><span class=\"ViewCount\">" . the_views(false) .  "</span><a class=\"dl-source\" href=\"$content\">Link</a></div>";
}

?>

* 2. *

, the_views(), .

<?php

function video_block( $atts, $content = null ) { 
  echo "<div class=\"video-block\"><span class=\"ViewCount\">";
  the_views();
  echo "</span><a class=\"dl-source\" href=\"$content\">Link</a></div>";
}

?>

, , .

+5

@BLewis, , , - :

function my_shortcode() {
  ob_start();
  a_function_to_display();
  $data = ob_get_clean();
  return $data;
}
add_shortcode( 'shortcode_name', 'my_shortcode' );
+3

.

, , .

HTML, echo, , , echo. , , echo_r print var_dump, HTML.

Since PHP only works inside cover tags <?phpand ?>you should use this to your advantage.

But since we cannot just do it here, because you can lose your order of content, you can use some storage of the object. Basically, before you start your HTML block (maybe at the beginning of your shortcode function it will be fine), just add ob_start();to save the code. Then, before completing your function, you can returnuse your data with $data = ob_get_clean();and return $data;.

Good luck with WordPress.

+2
source

Source: https://habr.com/ru/post/1790388/


All Articles