Forums

[resolved] Call same content on page multiple times. (16 posts)

  1. Elliotvs
    Member
    Posted 1 year ago #

    Hello,

    I am looking for a code or even plugin which can allow me to type some content such as 'James' at the top in a code then make it so whever I enter a certain code throughout the page/post it will show that content.

    I would prefer it if the text at the top is not viewable on the published page, but don't worry about that for now if not possible.

    Example:

    <text1>James</text1>
    
    The current leader is [text1].
    ................
    ........
    Congratulations to [text1]

    Thanks,

    Elliot

  2. vtxyzzy
    Member
    Posted 1 year ago #

    I think this will do what you want. Add this function to functions.php:

    function repeat_text_func($atts, $text = '') {
       static $saved_text = '';
       if ($text != '') {
          $saved_text = $text;
          return '';
       } else {
          return $saved_text;
       }
    }
    add_shortcode('repeat_text','repeat_text_func');

    Be careful though, you may need to wrap it in php tags. If you make a mistake, your site will be unusable and you will need to correct the error using ftp to edit the file.

    Then code your post like this:

    [repeat_text]James[/repeat_text]
    
    The current leader is [repeat_text].
    ................
    ........
    Congratulations to [repeat_text]
  3. Elliotvs
    Member
    Posted 1 year ago #

    @vtxyzzy thanks alot, I will try it out now and let you know how it goes.

  4. Elliotvs
    Member
    Posted 1 year ago #

    @vtxyzzy - That worked great, however is it possible to make it so that I can have it multiple times for different words? (Without having to enter it multiple times into the functions.php.

    Thanks.

  5. Elliotvs
    Member
    Posted 1 year ago #

    I really need to also get it to work multiple times for multiple words.

  6. vtxyzzy
    Member
    Posted 1 year ago #

    Just make multiple copies of the function and shortcode with different names. You might want to use a meaningful name like 'repeat-lastname' so it is easy to tell which is which.

  7. Elliotvs
    Member
    Posted 1 year ago #

    @vtxyzzy

    I tried this, but it doesnt work correctly. It just shows one of them for all shortcodes.

    function repeat_text_func($atts, $text = '') {
       static $saved_text = '';
       if ($text != '') {
          $saved_text = $text;
          return '';
       } else {
          return $saved_text;
       }
    }
    add_shortcode('repeat_text1','repeat_text_func');
    add_shortcode('repeat_text2','repeat_text_func');
    add_shortcode('repeat_text3','repeat_text_func');
    add_shortcode('repeat_text4','repeat_text_func');
    [repeat_text1]James[/repeat_text1]
    [repeat_text2]Tom[/repeat_text2]
    [repeat_text3]Sam[/repeat_text3]
    [repeat_text4]Chris[/repeat_text4]
    
    1st: [repeat_text1]
    2nd: [repeat_text2]
    3rd: [repeat_text3]
    4th: [repeat_text4]

    etc...

  8. Elliotvs
    Member
    Posted 1 year ago #

    vtxyzzy - do you mean copies of the full function? If so I'll try that but is there any way to do it without copying it lots of times?

    Thanks,

    Elliot.

  9. vtxyzzy
    Member
    Posted 1 year ago #

    You must have a separate function for each shortcode.

    function repeat_text1($atts, $text = '') {
    . . .
    }
    add_shortcode('repeat_text1','repeat_text1_func');
    function repeat_text2($atts, $text = '') {
    . . .
    }
    add_shortcode('repeat_text2','repeat_text2_func');
    . . .
  10. Elliotvs
    Member
    Posted 1 year ago #

    Ok thanks, so how would I type the whole functions.php code out for it?

  11. vtxyzzy
    Member
    Posted 1 year ago #

    Just make a complete copy of the function and add_shortcode line that I first sent and change all occurrences of repeat_text to repeat_text1 (3 places).

  12. Elliotvs
    Member
    Posted 1 year ago #

    Thanks.

    Thats a very long winded way to do it but its worked. Say if I was it 20 times then it is very long.

    If you think of a way to have it shorter so it creates multiple itself let me know please.

    Cheers.

  13. vtxyzzy
    Member
    Posted 1 year ago #

    I really wish you had told me that at the start. Here is the new function:

    function repeat_text_func($atts) {
    	extract( shortcode_atts( array(
    		   'id' => 0,
    		   'text' => '',
    		   ), $atts ) );
       static $saved_text = array();
       if( intval($id) > 0 ) {
          if ($text != '') {
             $saved_text[$id] = $text;
          } else {
             return $saved_text[$id];
          }
       }
       return '';
    }
    add_shortcode('repeat_text','repeat_text_func');

    It uses an id number to identify the piece of text to store or show. Call it like this to store the text:

    [repeat_text id="1" text="text for id 1"]

    Call it like this to show the text for id 1:

    [repeat_text id="1"]
  14. Elliotvs
    Member
    Posted 1 year ago #

    Thanks a lot vtxyzzy.

    Sorry for your misunderstanding at the beginning.

    You have been a great help, thank you very much.

  15. vtxyzzy
    Member
    Posted 1 year ago #

    If your problem has been solved, please use the dropdown on the right to mark this topic 'Resolved' so that anyone else with this question can see that there is a solution.

  16. Elliotvs
    Member
    Posted 1 year ago #

    No problem, thanks alot.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.