• Resolved wpmhweb

    (@wppit)


    I need help creating a function that looks for specific tag in a post. Then I need a shortcode to call the function and put a link.
    Something like this:
    If tag “basic” exist in the post, then call link (with a shortcode) “basic link” a place in the post, else if the tag is “intermediate” place “intermediate link” in post, else if the tag is “advanced” then place “advanced link” in the post.

    Can anyone help me with this?

    I hope whoever read this understands that I am trying to accomplish. thanks!!

Viewing 5 replies - 1 through 5 (of 5 total)
  • This sample function should get you started:

    function mam_insert_link_for_tag () {
         // use shortcode [mam_tag_link]
         $output = '';
         foreach (get_the_tags() as $tag) {
           $name = $tag->name;
           if ($name == 'basic') {
             $output = '<a href="https://someurl.com/basic.html"> Basic Link</a>';
             break;
           } elseif ($name == 'intermediate') {
             $output = '<a href="https://someurl.com/intermediate.html"> Intermediate Link</a>';
             break;
           } elseif ($name == 'advanced') {
             $output = '<a href="https://someurl.com/advanced.html"> Advanced Link</a>';
             break;
           }
         }
         return $output;
       }
       add_shortcode('mam_tag_link','mam_insert_link_for_tag');
    Thread Starter wpmhweb

    (@wppit)

    First, thank you for taking the time and coding this!

    One more question.
    I dont know what I am doing wrong. The code looks right, I changed the urls with the real urls and then went to single.php and I tried to call the shortcode like this: <div><?php mam_tag_link ?></div> and this didnt work also tried <div> <?php mam_insert_insert_link_for_tag ?></div> and nothing happens…

    I dont know if i ask right is this a shortcode <div> <?php mam_insert_insert_link_for_tag ?></div> if not, can you tell what is the name of this?

    Again thank you for your help!

    Thread Starter wpmhweb

    (@wppit)

    I have to apologize!
    I asked wrong, I did put the code in a post and it worked putting the link.

    but what I really meant was to call the function from in a single.php and from the index.php with something like this. <div><?php mam_tag_link ?></div>

    how do i call the function with the example I showed you.

    Thanks!

    Thread Starter wpmhweb

    (@wppit)

    Got it!!!
    I found this https://codex.www.ads-software.com/Function_Reference/get_option
    that helped me a little bit with the code below.

    <div class=”right”> <?php echo mam_insert_link_for_tag () ?> </div>

    Thank you again for taking the time the code works 100%!!!

    WordPress rocks!!!

    You are welcome. Now, please use the dropdown at top right to mark this topic ‘Resolved’.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘crate function and shortcode to place a link on posts’ is closed to new replies.