• Resolved lucianofiorini

    (@lucianofiorini)


    Hello everyone, I was looking to do this, but because I could not find a solution, I decided to share in the forum for those who really know this. Here goes:

    What I need to do is, when inserting a field of custom fields in a post, this complete a shortcode on the function.php to be called in the post that I want in the following way [hello].

    Here are step example of the codes I use.

    First create the shortcode with this code:

    function mi_shortcode() {
       return 'Hello Manolo!';
    }
    
    add_shortcode('hello', 'mi_shortcode'

    And what i need is that adding a custom is called “manolo” this is completed within the short code.

    So I tried the following code, but I have not worked:

    add_shortcode('hola', 'myshortcode'
    function myshortcode() {
       return 'Hola <?php echo get_post_meta($post->ID, 'manolo', true); ?>!';
    }

    Could anyone help me? Did I explain well what I need?

    Thanks in advance for any help you can give me!

    Greetings!

Viewing 9 replies - 1 through 9 (of 9 total)
  • why not write the code directly in the template?
    First check ob the custom field exist, and if yes

    echo '[' . $customfield . ']';

    Thread Starter lucianofiorini

    (@lucianofiorini)

    nsathees, i dont write the code directly in the template because i want to put it in the middle of the text on the post content, so i think that using shortcodes [example] is the best way, but i dont know how to make the code.

    Thanks for any help you can give me!

    Greetings!

    Thread Starter lucianofiorini

    (@lucianofiorini)

    Hi, I tried with the following code, but I have not worked:

    add_shortcode(‘hola’, ‘myshortcode’);
    function myshortcode()
    {return ‘Hola’ . get_post_meta($post->ID, ‘prueba’, true) . ;}

    Your code has a stray “.” in it. Try:

    add_shortcode( 'hola', 'myshortcode' );
    function myshortcode() {
        global $post;
        return 'Hola ' . get_post_meta( $post->ID, 'prueba', true );
    }

    hi, to interrupt this conversation:

    1. whats the difference in using shortcode and hacking the template?

    2. dont they both use functions.php?

    3. is shortcode like bbcodes?

    1. Shortcodes are used by the user in posts, pages, and (if you enable this) widgets. Putting the code directly in the template will require the user to manually edit that template if they want to change something.

    2. They can.

    3. A bit:

    https://codex.www.ads-software.com/Shortcode_API
    https://wpcodesnippets.info/blog/shortcode-fundamentals-part-1.html

    And what if i as admin, need custom fields to display in a certain way and lock the user into a certain display?

    eg. if I need WP “excerpt” to display RED AND BOLD by default?

    Shortcodes return whatever you want them to.

    $output = '<span style="color: red; font-weight: bold;">';
    $output .= /* Whatever this shortcode is supposed to return */;
    $output .= '</span>';
    
    return $output;
    Thread Starter lucianofiorini

    (@lucianofiorini)

    Thanks Big Bagel! This is the code i need!

    Thanks to all!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Custom Fields and Shortcode together on Functions.php’ is closed to new replies.