• Hi, does anybody know how i can easily add a custom field to all my posts. What im after is a way to add a “Discuss this topic in forum” with link to a url i can specify.

    Would also like a “News Source” link as well so can anybody supply a custom field code or recommend plugin that’s easy to use.

    Thnx

Viewing 2 replies - 1 through 2 (of 2 total)
  • You might be able to add those links to all of your posts withe the Post Layout plugin.

    this is a snippet that might do what you want:
    (add it in functions.php of your theme)

    function addCustomFieldForumLink() {
    /* to add a custom field with a link to a forum to all posts, /
    / update it on 'post edit' or 'post save' /
    / to show in post (within the loop) use <?php echo get_post_meta($post->ID,'forum_link',true); ?>
    / alchymyth 2010 */
    global $wpdb;
    $querystr = "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type = 'post' ";
    $pageposts = $wpdb->get_results($querystr, OBJECT);
    // get a list of all published posts //
    if ($pageposts):
    foreach ($pageposts as $post):
    setup_postdata($post);
    // add the 'forum link' custom field to the post or update it //
    $link_to_forum = 'https://#' // enter the full path to your forum //
    add_post_meta($post->ID, 'forum_link', $link_to_forum, true);
    update_post_meta($post->ID, 'forum_link', $link_to_forum);
    // add the 'news link' custom field to the post or update it //
    $link_to_news = 'https://#' // enter the full path to your news source //
    add_post_meta($post->ID, 'news_link', $link_to_news, true);
    update_post_meta($post->ID, 'news_link', $link_to_news);
    endforeach;
    endif;
    }
    add_action ( 'publish_post', 'addCustomFieldForumLink' );
    add_action ( 'edit_post', 'addCustomFieldForumLink' );

    the function will be called every time you publish a new post, or save edits on existing posts.
    if a post does not have the custom field(s), the code will make and add the customfield and the custom value to the post;
    if the custom field exists, the code will ‘update’ and keep just one value.
    btw: make sure you dont have any empty line after the last ?> in your functions.php; and observe the opening/closing of the php tags.

    hope it works, good luck ??

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