this turned out to be fairll simple at the end. Just added indicator
<?php
$options = get_option( ‘cmby_settings’ );
// Output Shortcode
function cmby_list_widgets($atts, $content = null){
static $i=0;
$atts = shortcode_atts(array(
‘title’ => ‘Cambrick Yard Widgets’,
‘special’ => $ran
), $atts);
global $options;
$output = ‘<div class=”widgetcontainer”>’;
$output .= ‘<div class=”widgetblock”>’;
$output .= $options[“cmby_textarea_field_$i”].'<br/></div>’;
$i++;
return $output;
}
add_shortcode(‘cambrickyardwidgets’, ‘cmby_list_widgets’);
Hi brveljkovic
Does it matter if the shortcodes are in one post, or from multiple posts in a page?
To count the shortcodes being output you can add a static counter to your shortcode
http://php.net/manual/en/language.variables.scope.php#language.variables.scope.static
Here’s an example shortcode
function my_content_shortcode( $atts, $content = null ) {
static $count = 0;
$count++;
return '<span class="shortcode-count">My Shortcode ' . $count . '</span>';
}
add_shortcode( 'my_content', 'my_content_shortcode' );
Haha, you beat me by 20 seconds!
Glad you’ve solved it
Hi keesiemeijer.
I have a problem here. It seems WordPress will do shortcode twice times?
If I call 3 times same shortcode on my post content. It will do the shortcode 6 times. The first 3 times like wordpress just doing the shortcode and will not print html, last 3 times will print html.
When I using like your code on wordpress 4.8.2:
function my_content_shortcode( $atts, $content = null ) {
static $count = 0;
$count++;
return '<span class="shortcode-count">My Shortcode ' . $count . '</span>';
}
add_shortcode( 'my_content', 'my_content_shortcode' );
The post content:
No.1
[my_content][/my_content]
No.2
[my_content][/my_content]
No.3
[my_content][/my_content]
And the post show
No.1
My Shortcode 4
No.2
My Shortcode 5
No.3
My Shortcode 6
Like the first N times to run the global, and then run N times to output. (N equal same shortcode was called)
How can i solve it?
Thank you.
-
This reply was modified 6 years, 6 months ago by kn007.
That might be because your theme or a plugin is doing stuff with the content before it’s displayed.
I can only replicate your issue with something like this:
<?php
// Store the content
ob_start();
the_content();
$content = ob_get_clean();
// Display the content
the_content();
?>
Can you replicate the problem using the default Twenty Seventeen theme with all other plugins deactivated?
Emmm, when i using the default Twenty Seventeen theme, it was working fine.
But my theme do not using any ob_*, when i let all of plugins deactivated, still have the issue.
Any idea for this?
Thanks a lot.
I will track it on local later.
-
This reply was modified 6 years, 6 months ago by kn007.
-
This reply was modified 6 years, 6 months ago by kn007.
Another way the counter could be incremented is by using do_shortcode()
with your shortcodes in the content
https://developer.wordpress.org/reference/functions/do_shortcode/
Or by applying the filters of the content. Something like this.
$content = apply_filters('the_content', $content);
Thank you.
I will check it later.
A lot of filters to apply for the content.
Thank you, @keesiemeijer
Found the problem, my header.php using like
$content = apply_filters('the_content', $content);
for html description meta tag.
Now it’s ok.
Thanks a lot.