• masini2002

    (@masini2002)


    Sorry for my bad English. I have this code to be able to use a shortcode with the custom fields I want in an entry or page.

    add_shortcode(‘field’, ‘shortcode_field’);
    function shortcode_field($atts){
    extract(shortcode_atts(array(
    ‘post_id’ => NULL,
    ), $atts));
    if(!isset($atts[0])) return;
    $field = esc_attr($atts[0]);
    global $post;
    $post_id = (NULL === $post_id) ? $post->ID : $post_id;
    return get_post_meta($post_id, $field, true);
    }

    The problem is that it does not work for post titles and I would need to add a custom field to the title (in addition to the text you add). And I’m a little lost of how to do it. So I would pray if someone can help me and explain it to me.

    Thank you

Viewing 4 replies - 1 through 4 (of 4 total)
  • Joy

    (@joyously)

    Your question makes no sense to me.
    You have a shortcode that you can put into a post, like [field myfield] and that will get the post meta called myfield for the current post. Or [field myfield post_id=12] will get the post meta called myfield for post 12.
    What are you trying to do? Shortcodes are not evaluated for post titles, and post titles are not meta, so I don’t understand. If you want something in a post title, just type it there. If you want another post’s title in your post, just type it there. (the editor has a search for you to get a link to other posts)

    Moderator bcworkz

    (@bcworkz)

    Add this to where ever your other custom code is:
    add_filter('the_title', 'do_shortcode');

    I’m assuming your theme outputs post titles by calling the_title(). If not, what ever your theme does use needs a similar filter we can hook.

    Thread Starter masini2002

    (@masini2002)

    Thanks, the code works. The shortcode appears in the title.

    I use the All Import plugin that dynamically creates the entries from an xml file. That’s why I need the title to be a custom field.

    Would it be possible for the url to include that custom field?

    Thanks for the help.

    Moderator bcworkz

    (@bcworkz)

    You could possibly insert terms into the permalink, but you would not be able to dynamically alter the page slug itself. If you did that, WP would not be able to find the page data in the database. In the case of slugs, Joy is very much correct in advising that you just type what you want.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom fields in post title’ is closed to new replies.