admin管理员组

文章数量:1134064

I'm trying to use shortcode in index.php but when I use PHP code in my shortcode, it displays out of shortcode structure.

this HTML code works

<?php echo do_shortcode('[myshortcode label="sample title" Description="Content Goes Here"]'); ?>

this code doesn't work and pushes PHP code out of short code structure

<?php echo do_shortcode('[myshortcode label="'. the_title() .'" Description="Content Goes Here"]'); ?>

it outputs like this:

sample title
<div class="hidden-content">
<span class="show-content"></span>
<div class="showme hidden" style="display: none;">Content Goes Here</div>
</div>

But it must be like this

<div class="hidden-content">
<span class="showcontent">sample title</span>
<div class="showme hidden" style="display: none;">Content Goes Here</div>
</div>

I'm trying to use shortcode in index.php but when I use PHP code in my shortcode, it displays out of shortcode structure.

this HTML code works

<?php echo do_shortcode('[myshortcode label="sample title" Description="Content Goes Here"]'); ?>

this code doesn't work and pushes PHP code out of short code structure

<?php echo do_shortcode('[myshortcode label="'. the_title() .'" Description="Content Goes Here"]'); ?>

it outputs like this:

sample title
<div class="hidden-content">
<span class="show-content"></span>
<div class="showme hidden" style="display: none;">Content Goes Here</div>
</div>

But it must be like this

<div class="hidden-content">
<span class="showcontent">sample title</span>
<div class="showme hidden" style="display: none;">Content Goes Here</div>
</div>
Share Improve this question asked Oct 8, 2023 at 1:49 DragutDragut 1732 gold badges2 silver badges11 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

You need to change the_title() to get_the_title().

You are asking the shortcode to echo out a string that is returning the title.

It would be good for you to research the difference between the_title and get_the_title.

The main difference is that the_title is actually returning the title without any other requirement (this is simplified: there are filters happening as well) but it is why you see it first before the rest of the code.

本文标签: php codes ruins shortcode structure