Search code examples
wordpressslidernivo-slider

wordpress nivo slider creating custom permalink option during post?


I am not using the nivo slider wordpress plugin, instead I am using regular nivo slider jquery and implemented it to work in wordpress, and currently my "read more" button is as follows:

<a href='".get_permalink()."'>Read More</a>

What I want to implement is something like a get_permalinkpage? So basically I want to be able to make the read more link to a wordpress page of my choosing instead of the post's permalink. But I do not know how to implement a custom option to the posts page that would allow the user to say "Choose from pages to link the nivo slider slide to: (then shows pages on website)" and then output that selection.

Any help? This is the last thing I need to implement for our website!


Solution

  • Right, I have your answer here as it's something I did myself recently. This is really a question about custom metaboxes. Here's some resources on it - I got sent a link on it from a mate who recommends this;

    http://www.deluxeblogtips.com/meta-box/

    And in the Bones theme the author recommends this one;

    https://github.com/jaredatch/Custom-Metaboxes-and-Fields-for-WordPress

    I will post some code here if you want to quickly get going with it. I would place the following code in a file in it's own and include it from your functions.php, ie;

     require_once('includes/metabox-post.php');
    

    Create an includes directory in your theme directory and create a file containing this code;

    <?php
    
    /* CUSTOM METABOX  -----------------------------------------------------*/
    
    //We create an array called $meta_box and set the array key to the relevant post type
    $meta_box_post['post'] = array( 
    
    //This is the id applied to the meta box
    'id' => 'post-format-meta',   
    
    //This is the title that appears on the meta box container
    'title' => 'My Custom Metabox',    
    
    //This defines the part of the page where the edit screen section should be shown
    'context' => 'normal',    
    
    //This sets the priority within the context where the boxes should show
    'priority' => 'high', 
    
    //Here we define all the fields we want in the meta box
    'fields' => array(  
        array(
            'name' => 'Home Slider Link',
            'desc' => 'You can create a custom link for the home slider image (ie to link to the shop).  If left blank, it will by default link through to this post.',
            'id' => 'home-slide-link',
            'type' => 'text',
            'default' => ''
        )
    
       )
    
    );
    
    add_action('admin_menu', 'meta_add_box_post');
    
    
     //Add meta boxes to post types
    function meta_add_box_post() {
    global $meta_box_post;
    
    foreach($meta_box_post as $post_type => $value) {
        add_meta_box($value['id'], $value['title'], 'meta_format_box_post', $post_type, $value['context'], $value['priority']);
      }
    }
    
    //Format meta boxes
    function meta_format_box_post() {
      global $meta_box_post, $post;
    
      // Use nonce for verification
      echo '<input type="hidden" name="plib_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
    
      echo '<table class="form-table">';
    
      foreach ($meta_box_post[$post->post_type]['fields'] as $field) {
          // get current post meta data
          $meta = get_post_meta($post->ID, $field['id'], true);
    
          echo '<tr>'.
                  '<th style="width:20%"><label for="'. $field['id'] .'">'. $field['name']. '</label></th>'.
                  '<td>';
      switch ($field['type']) {
          case 'text':
              echo '<input type="text" name="'. $field['id']. '" id="'. $field['id'] .'" value="'. ($meta ? $meta : $field['default']) . '" size="30" style="width:97%" />'. '<br />'. $field['desc'];
              break;
          case 'textarea':
              echo '<textarea name="'. $field['id']. '" id="'. $field['id']. '" cols="60" rows="4" style="width:97%">'. ($meta ? $meta : $field['default']) . '</textarea>'. '<br />'. $field['desc'];
              break;
          case 'select':
              echo '<select name="'. $field['id'] . '" id="'. $field['id'] . '">';
              foreach ($field['options'] as $option) {
                  echo '<option '. ( $meta == $option ? ' selected="selected"' : '' ) . '>'. $option . '</option>';
              }
              echo '</select>';
              break;
          case 'radio':
              foreach ($field['options'] as $option) {
                  echo '<input type="radio" name="' . $field['id'] . '" value="' . $option['value'] . '"' . ( $meta == $option['value'] ? ' checked="checked"' : '' ) . ' />' . $option['name'];
              }
              break;
          case 'checkbox':
              echo '<input type="checkbox" name="' . $field['id'] . '" id="' . $field['id'] . '"' . ( $meta ? ' checked="checked"' : '' ) . ' /<br />&nbsp;&nbsp;'. $field['desc'];
              break;
      }
      echo     '<td>'.'</tr>';
      }
    
      echo '</table>';
    
    }
    
    // Save data from meta box
    function meta_save_data_post($post_id) {
        global $meta_box_post,  $post;
    
       //Verify nonce
        if (!wp_verify_nonce($_POST['plib_meta_box_nonce'], basename(__FILE__))) {
            return $post_id;
        }
    
        //Check autosave
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
            return $post_id;
        }
    
        //Check permissions
        if ('page' == $_POST['post_type']) {
           if (!current_user_can('edit_page', $post_id)) {
                return $post_id;
            }
        } elseif (!current_user_can('edit_post', $post_id)) {
           return $post_id;
        }
    
        foreach ($meta_box_post[$post->post_type]['fields'] as $field) {
            $old = get_post_meta($post_id, $field['id'], true);
            $new = $_POST[$field['id']];
    
            if ($new && $new != $old) {
                update_post_meta($post_id, $field['id'], $new);
            } elseif ('' == $new && $old) {
                delete_post_meta($post_id, $field['id'], $old);
            }
        }
    }
    
    add_action('save_post', 'meta_save_data_post');
    
    
    ?>
    

    The this will add a new custom metabox to your posts which you can type in an alternative url into. This custom option will have the id home-slide-link. To use that url you would include the following in your template loop whilst building up the list of Nivoslider images;

     <?php
    if ( get_post_meta($post->ID, 'home-slide-link', true) ) :
        $slideLink = get_post_meta($post->ID, 'home-slide-link', true); 
    else :
        $slideLink = get_permalink();
    endif;
    
       echo '<a href="'. $slideLink .'"><img src="image link in here" /></a>';
     ?>
    

    So if the post has a url set for the slider link then it uses that, if not it defaults to the permalink.

    Hope this helps you a bit!