php - Can A Shortcode Get HTMLText From Content And Return It Twice With Added HTML?

admin2025-01-07  4

I'm trying to cut down on some work for myself, and I was hoping someone might know if this is possible.

These are images setup in a slider that shows a text overlay on the image on the next slide in a multipage post.

Here's what I'm trying to do. Take this:

<h2>Image Title</h2>
[caption]<img src="http">[/caption]
text for slide

And output them like this:

<div class="singe_slide">
<h2>Image Title</h2>
[caption]<img src="http">[/caption]
</div>
<!--nextpage-->
<div class="singe_slide">
<h2>Image Title</h2>  //Same Image Next Page
[caption]<img src="http">[/caption]
<div class="slide_caption"><div class="captExt">
text for slide
</div></div>
<!--nextpage-->

This is what I'm currently using for my shortcodes, but it takes just as long to just manually add the div's, and it's very messy. It looks like this:

[Slide]<h2>Image Title</h2>
[caption]<img src="http">[/caption][/Slide]
<!--nextpage-->
[Slide]<h2>Image Title</h2> //Same Image Next Page
[caption]<img src="http">[/caption]
[SlideCap]text for slide[/SlideCap][/Slide]
<!--nextpage-->

And these are my 2 shortcodes:

function Slide($atts, $content = null) {
    $content = wpautop(trim($content));
    return '<div class="singe_slide">' . do_shortcode($content) . '</div>';
}
add_shortcode('Slide', 'Slide');

function SlideCap($atts, $content = null) {
    $content = wpautop(trim($content));
   return '<div class="slide_caption"><div class="captExt">' . do_shortcode($content) . '</div></div>';
}
add_shortcode('SlideCap', 'SlideCap');

I'm open to suggestions on an easier way to do this. This is my very first shortcode, so I'm new to it.

Is it possible to get something between shortcodes as a variable? [shortcode]This Part Here[/shortcode]

I'm trying to cut down on some work for myself, and I was hoping someone might know if this is possible.

These are images setup in a slider that shows a text overlay on the image on the next slide in a multipage post.

Here's what I'm trying to do. Take this:

<h2>Image Title</h2>
[caption]<img src="http">[/caption]
text for slide

And output them like this:

<div class="singe_slide">
<h2>Image Title</h2>
[caption]<img src="http">[/caption]
</div>
<!--nextpage-->
<div class="singe_slide">
<h2>Image Title</h2>  //Same Image Next Page
[caption]<img src="http">[/caption]
<div class="slide_caption"><div class="captExt">
text for slide
</div></div>
<!--nextpage-->

This is what I'm currently using for my shortcodes, but it takes just as long to just manually add the div's, and it's very messy. It looks like this:

[Slide]<h2>Image Title</h2>
[caption]<img src="http">[/caption][/Slide]
<!--nextpage-->
[Slide]<h2>Image Title</h2> //Same Image Next Page
[caption]<img src="http">[/caption]
[SlideCap]text for slide[/SlideCap][/Slide]
<!--nextpage-->

And these are my 2 shortcodes:

function Slide($atts, $content = null) {
    $content = wpautop(trim($content));
    return '<div class="singe_slide">' . do_shortcode($content) . '</div>';
}
add_shortcode('Slide', 'Slide');

function SlideCap($atts, $content = null) {
    $content = wpautop(trim($content));
   return '<div class="slide_caption"><div class="captExt">' . do_shortcode($content) . '</div></div>';
}
add_shortcode('SlideCap', 'SlideCap');

I'm open to suggestions on an easier way to do this. This is my very first shortcode, so I'm new to it.

Is it possible to get something between shortcodes as a variable? [shortcode]This Part Here[/shortcode]

Share Improve this question edited Jul 19, 2024 at 7:16 bueltge 17.1k7 gold badges61 silver badges97 bronze badges asked May 21, 2016 at 0:44 DRKDRK 293 bronze badges 8
  • 2 I don't think I quite understand. The second argument in your shortcode handler - $content - will get the content that is between your shortcodes. Is that what you mean? I'm not sure why you're running this through do_shortcode() again... – Tim Malone Commented May 21, 2016 at 1:47
  • @TimMalone I added more info to the post that might help. First off I'm not a php programmer so a lot of this is just trial and error. I ran it twice because I have a shortcode within a shortcode and I don't know how else to do it. I added how I write my shortcode currently when doing a post in WP. I'm trying to return the heading and image twice with a nextpage separating it and the text once on the second page all wrapped in div's. – DRK Commented May 21, 2016 at 12:00
  • What isn't clear in your question is what html output you are trying to achieve. You've shared lots of code, but you haven't said "I have this shortcode X and I want it to produce this html Y. This code Z is what I am trying to use to make it, but instead its producing W. Why is that?" – ToniWidmo Commented May 21, 2016 at 13:03
  • @AntonChanning Bare with me, I'm not a programmer. I put up what I'm trying to achieve and what I am currently doing to achieve it. The problem is it's very tedious to add all of the shortcodes and heading/images twice. It's just as time consuming as putting in the html myself instead of shortcodes too. What I want to do is make it so my shortcode takes the $content and outputs it the way I want. Essentially combining my shortcodes into one. I just don't know how to get each item I need in between the shortcodes and output it the way I want. Do I need to split it into an array first? – DRK Commented May 21, 2016 at 13:42
  • I have a programmer working on it now. If he can do it I'll post up what he did. – DRK Commented May 21, 2016 at 14:37
 |  Show 3 more comments

1 Answer 1

Reset to default 0

Okay, I think I've figured out what you're actually asking. You're asking what the variable for the bit between the shortcodes. If that is all you are asking, then the answer is the parameter variable $content. You are already processing it in your code.

Also, in your example, you seem to have a third shortcode, [caption] that you aren't processing. Is that deliberate? It doesn't look like you really want to produce final html with that in, so this solution assumes you don't want that.

I'm going to assume that what you want is a simple to use shortcode format such as:

[slide=ImageTitle][slideimg]http://some.domain/slide.jpg[/slideimg][slidecap]This is the caption.[/slidecap][/slide]

Which you can then process simply by adjusting your functions like so:

function shortcode_slide($atts, $content = null) {
    if(empty($atts)) {
        $img_title='';
    } else {
        // [slide=Title]...[/slide]
        // [slide="Multi word title"]...[/slide]
        $atts = $this->attributefix( $atts );
        $img_title = '<h2>'.trim(array_shift($atts),'="').'</h2>'; //Remove quotes and equals.
    }

    $content = wpautop(trim($content));
    return '<div class="singe_slide">'.$img_title. do_shortcode($content) . '</div>';
}
add_shortcode('slide', 'shortcode_slide');

function shortcode_slidecap($atts, $content = null) {
    $content = wpautop(trim($content));
    return '<div class="slide_caption"><div class="captExt">' . do_shortcode($content) . '</div></div>';
}
add_shortcode('slidecap', 'shortcode_slidecap');

function shortcode_slideimg($atts, $content = null) {
    $content = wpautop(trim($content));
    return '<img src="'.$content.'" />';
}
add_shortcode('slideimg', 'shortcode_slideimg');

This should produce working html.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736256624a375.html

最新回复(0)