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!
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 /> '. $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 '<img src="image link in here" />';
?>
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!
Here's my solution based on yours.
<div id="slider">
<?php
$captions = array();
$tmp = $wp_query;
$wp_query = new WP_Query('cat='.$category.'&posts_per_page=$n_slices' );
if($wp_query->have_posts()) :
while($wp_query->have_posts()) :
$wp_query->the_post();
$captions[] = '<p>'.get_the_title($post->ID).'</p><p>'.get_the_excerpt().'</p>';
$image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'nivothumb');
$mykey_values = get_post_custom_values('home-slide-link');
?>
<a href="<?php echo $mykey_values[0] ?>">
<img src="<?php echo $image[0]; ?>" class="attachment-nivothumb wp-post-image" title="#caption<?php echo count($captions)-1; ?>" alt="<?php the_title_attribute(); ?>" />
</a>
<?php
endwhile;
endif;
$wp_query = $tmp;
?>
</div><!-- close #slider -->
<?php
foreach($captions as $key => $caption) :
?>
<div id="caption<?php echo $key; ?>" class="nivo-html-caption">
<?php echo $caption;?>
</div>
<?php
endforeach;
?>
Related
I am Developing a wordpress testimonials plugin.
I created a custom post type also display and working proper. But now I want to add more fields like "Company Name", "Websites". I know I can manage from this by adding custom fields, but I want to add a separate section for that.
Can anyone help me figure out how to do this?
You can also implement meta boxes which will be displayed on the right hand sidebar the same as you have categories displayed.
You can use "Advanced Custom Fields", which is a perfect solution.
// Save the Metabox Data
function wpt_save_events_meta($post_id, $post) {
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST['eventmeta_noncename'], plugin_basename(__FILE__) )) {
return $post->ID;
}
// Is the user allowed to edit the post or page?
if ( !current_user_can( 'edit_post', $post->ID ))
return $post->ID;
// OK, we're authenticated: we need to find and save the data
// We'll put it into an array to make it easier to loop though.
$events_meta['_address'] = $_POST['_address'];
$events_meta['_phone'] = $_POST['_phone'];
$events_meta['_type'] = $_POST['_type'];
$events_meta['_cap'] = $_POST['_cap'];
// Add values of $events_meta as custom fields
foreach ($events_meta as $key => $value) { // Cycle through the $events_meta array!
if( $post->post_type == 'revision' ) return; // Don't store custom data twice
$value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
update_post_meta($post->ID, $key, $value);
} else { // If the custom field doesn't have a value
add_post_meta($post->ID, $key, $value);
}
if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
}
}
add_action('save_post', 'wpt_save_events_meta', 1, 2); // save the custom fields
// The Event address Metabox
function wpt_events_address() {
global $post;
// Noncename needed to verify where the data originated
echo '<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
// Get the address data if its already been entered
$address = get_post_meta($post->ID, '_address', true);
// Echo out the field
echo '<input type="text" name="_address" value="' . $address . '" class="widefat" />';
}
function wpt_events_phone() {
global $post;
// Noncename needed to verify where the data originated
echo '<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
// Get the address data if its already been entered
$phone = get_post_meta($post->ID, '_phone', true);
// Echo out the field
echo '<input type="text" name="_phone" value="' . $phone . '" class="widefat" />';
}
function wpt_events_type() {
global $post;
// Noncename needed to verify where the data originated
echo '<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
// Get the address data if its already been entered
$type = get_post_meta($post->ID, '_type', true);
// Echo out the field
echo '<input type="text" name="_type" value="' . $type . '" class="widefat" />';
}
function wpt_events_cap() {
global $post;
// Noncename needed to verify where the data originated
echo '<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
// Get the address data if its already been entered
$cap = get_post_meta($post->ID, '_cap', true);
// Echo out the field
echo '<input type="text" name="_cap" value="' . $cap . '" class="widefat" />';
}
$events_meta['_address'] = $_POST['_address'];
$events_meta['_phone'] = $_POST['_phone'];
$events_meta['_type'] = $_POST['_type'];
$events_meta['_cap'] = $_POST['_cap'];
add_action( 'add_meta_boxes', 'add_events_metaboxes' );
// Add the Events Meta Boxes
function add_events_metaboxes() {
add_meta_box('wpt_events_address', 'Address', 'wpt_events_address', 'events', 'normal', 'high');
add_meta_box('wpt_events_phone', 'Phone', 'wpt_events_phone', 'events', 'normal', 'high');
add_meta_box('wpt_events_type', 'Email', 'wpt_events_Type', 'events', 'normal', 'high');
add_meta_box('wpt_events_cap', 'Capacity', 'wpt_events_cap', 'events', 'normal', 'high');
}
thank you for the help in advance. I have been trying to find tutorial about building my own contact form for wordpress (without plugin). I thought i found one (part of the question).
<?php
/*
Plugin Name: Self Catering Contact form plugin
Plugin URI: http://example.com
Description: Simple non-bloated WordPress Contact Form use [contact_form]shortcode
Version: 1.0
Author: Agbonghama Collins
Author URI: http://w3guy.com
*/
function html_form_code() {
echo '<form action="' . esc_url( $_SERVER['REQUEST_URI'] ) . '" method="post">';
echo '<p>';
echo 'Your Name (required) <br />';
echo '<input type="text" name="cf-name" pattern="[a-zA-Z0-9 ]+" value="' . ( isset( $_POST["cf-name"] ) ? esc_attr( $_POST["cf-name"] ) : '' ) . '" size="40" />';
echo '</p>';
echo '<p>';
echo 'Your Email (required) <br />';
echo '<input type="email" name="cf-email" value="' . ( isset( $_POST["cf-email"] ) ? esc_attr( $_POST["cf-email"] ) : '' ) . '" size="40" />';
echo '</p>';
echo '<p>';
echo 'Subject (required) <br />';
echo '<input type="text" name="cf-subject" pattern="[a-zA-Z ]+" value="' . ( isset( $_POST["cf-subject"] ) ? esc_attr( $_POST["cf-subject"] ) : '' ) . '" size="40" />';
echo '</p>';
echo '<p>';
echo 'Your Message (required) <br />';
echo '<textarea rows="10" cols="35" name="cf-message">' . ( isset( $_POST["cf-message"] ) ? esc_attr( $_POST["cf-message"] ) : '' ) . '</textarea>';
echo '</p>';
echo 'Select date <br />';
echo '<p><input type="text" id="datepicker" name="datepicker" value="' . ( isset( $_POST["datepicker"] ) ? esc_attr( $_POST["datepicker"] ) : '' ) . '" size="40" />'.'</p>';
echo '<p><input type="submit" name="cf-submitted" value="Send"/></p>';
echo '</form>';
}
function deliver_mail() {
// if the submit button is clicked, send the email
if ( isset( $_POST['cf-submitted'] ) ) {
// sanitize form values
$name = sanitize_text_field( $_POST["cf-name"] );
$email = sanitize_email( $_POST["cf-email"] );
$subject = sanitize_text_field( $_POST["cf-subject"] );
$message = esc_textarea( $_POST["cf-message"] );
$datepicker = sanitize_text_field($_POST["datepicker"]);
// get the blog administrator's email address
$to = 'xx#xx.co.uk';
$headers = "From: example#yourdomainname.com" . "\r\n";
// If email has been process for sending, display a success message
if ( wp_mail( $to, $subject, $message, $headers ) ) {
echo '<div>';
echo '<p class="red">sent</p>';
echo '</div>';
} else {
echo 'An unexpected error occurred';
}
}
}
function cf_shortcode() {
ob_start();
deliver_mail();
html_form_code();
return ob_get_clean();
}
add_shortcode( 'contact_form', 'cf_shortcode' );
This is what my contact-form.php looks like. My first problem that it doesnt send the email - or it doesnt arrive even tho it says that the mail was sent. I have read a few things for example some hosting providers dont allow you to use different email address to your domain ones... Could somebody explain this?
If you still have enough patience left, could somebody guide me through how to implement datepicker (which i thought i have done), and how to handle or get the data from it to be included in the email?
Thank you very much for your time...
I have five custom fields for loading images but all of them are not required. I mean the user can upload a random number of images from 1 to 5. I am stuck in a simple lack of concept here. What I am missing? Here is my code
$img = array();
$post = get_page_by_title( $pgnr,OBJECT,'post');
$id= $post->ID;
$custom_fields = get_post_custom($id);
$images = Array("image1","image2","image3","image4","image5");
foreach($images as $image){
if(isset($custom_fields[$image]) && (!empty($custom_fields[$image]))) {
$img[] = $custom_fields[$image];
}
}
echo '<div id="showcase" class="showcase">';
foreach ( $img as $value )
{
echo '<div class="showcase-slide">';
echo '<div class="showcase-content">';
echo'<img alt="image" src="'. wp_get_attachment_url( $value ).'" width="600" height="500"/>';
echo '</div>';
echo '<div class="showcase-thumbnail">';
echo '<img alt="thumb" src="'.wp_get_attachment_url( $value ).'" width="140" /> ';
echo '</div>';
}
echo '</div>';
}
Perhaps the array $img[] needs an index between the brackets that only increments each time values are assigned into the array.
i need to show in default value my text and not first tag, how i can do this?
<?php
if ($tags = get_tags( array('orderby' => 'name') ))
{
echo '<form action="'.get_bloginfo('url').'" method="get">';
echo '<select name="tag" id="tag" class="postform">';
foreach ($tags as $tag)
{
echo '<option value="'.$tag->slug.'">'.$tag->name.'</option>';
}
echo '</select> ';
echo '<input type="submit" name="submit" value="view" />';
echo '</form>';
}
?>
If I correctly understood the question, you need to select the default tag in a drop-down list, and not the first one. Then you just need to check the value of slug and add selected attribute to an element when it is needed.
$default = 'default-tag-slug'; // set the default value
foreach ($tags as $tag) {
echo '<option value="'.$tag->slug.'" '.( $tag->slug == $default ? 'selected' : '' ).' >'.$tag->name.'</option>';
}
I'm currently working on creating meta boxes. I used the following tutorial and some self-adapted. Link of tutorial: http://wp.tutsplus.com/tutorials/reusable-custom-meta-boxes-part-1-intro-and-basic-fields/
Now I get the following error message:
Notice: Undefined index: dsmeta_image in
/customers/0/d/a/xxx/httpd.www/wordpress/wp-content/plugins/ds-flexslider/includes/cpt-manager.php
on line 181 Notice: Undefined index: dsmeta_image_caption in
/customers/0/d/a/xxx/httpd.www/wordpress/wp-content/plugins/ds-flexslider/includes/cpt-manager.php
on line 181
It seems that the variable does not exist, I'm using an array fields for Metabox and created a foreach loop walk you through it if I understand correctly.
How is this problem.
It is in any event error when saving the meta boxes ...
The part of setting up the fields array:
// Create the fields array
$prefix = 'dsmeta_';
$custom_meta_fields = array(
array(
'label' => 'Image',
'desc' => '',
'id' => $prefix . 'image',
'type' => 'image'
),
array(
'label' => 'Image caption',
'desc' => '',
'id' => $prefix . 'image_caption',
'type' => 'text'
)
);
Part of the saving function:
add_action('save_post', 'dsslider_manager_save_extras');
function dsslider_manager_save_extras($post_id) {
global $custom_meta_fields;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
// loop through fields and save the data
foreach ($custom_meta_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);
}
} // end foreach
}
Update after request
Here I add the meta box for the fields :
// Add meta box support
// This registers a function to be called when the WordPress admin interface is visited
add_action("admin_init", "dsslider_manager_add_meta");
function dsslider_manager_add_meta(){
// Create this cool new meta box for Portfolio Options
add_meta_box("dsslider-meta", "Brandbox Options", "dsslider_manager_meta_options", "brandbox-slider", "normal", "high");
}
And here is the function for building the meta fields :
function dsslider_manager_meta_options(){
global $custom_meta_fields, $post;
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
// (integer) (optional) The post ID whose custom fields will be retrieved.
// Default: Current post
return $post_id;
?>
<div class="dsslider_manager_extras">
<div class="ds-metabox" data-max_rows="5" data-min_rows="0">
<table class="meta ds-input-table">
<?php
foreach ($custom_meta_fields as $field) {
$custom = get_post_meta($post->ID, $field['id'], true); // Returns a multidimensional array with all custom fields of a particular post or page.
// Past HTML markup
?>
<tbody class="ui-sortable">
<?php
echo '<tr class="row">';
echo '<td class="order"></td>';
echo '<td>';
switch($field['type']) {
// case items will go here
// image
case 'image':
$image = get_template_directory_uri().'/images/image.png';
echo '<span class="custom_default_image" style="display:none">'.$image.'</span>';
if($custom) {
$image = wp_get_attachment_image_src($custom, 'thumbnail');
$image = $image[0];
} // end if statement
echo '<img src="' . $image . '" class="custom_preview_image" alt="" />
<input type="button" class="button add-image" name="' . $field['id'] . '" value="' . $custom . '">Remove Image';
break;
// text
case 'text':
echo '<input type="text" class="text" name="' . $field['id'] . '" value="' . $custom . '">';
break;
} //end switch
echo '</td>';
echo '</tr>';
} // End foreach loop
?>
</tbody>
</table><!-- End .meta ds-input-table -->
<ul class="ds-repeater-footer hl clearfix">
<li class="right">
Add New Slide
</li>
</ul><!-- End ul.hl clearfix repeater-footer -->
</div><!-- End .ds-metabox -->
</div><!-- End .dsslider_manager_extras -->
<?php
}
The problem is that you're using the $custom_meta_fields array to both generate your input fields and grab information from the $_POST array using complimentary key names.
This wouldn't ordinarily be a problem, but the fact is that some of the fields that you're using aren't actually passing information to the $_POST array. An example:
case 'image':
$image = get_template_directory_uri().'/images/image.png';
echo '<span class="custom_default_image" style="display:none">'.$image.'</span>';
if($custom) {
$image = wp_get_attachment_image_src($custom, 'thumbnail');
$image = $image[0];
} // end if statement
echo '<img src="' . $image . '" class="custom_preview_image" alt="" />
<input type="button" class="button add-image" name="' . $field['id'] . '" value="' . $custom . '">Remove Image';
break;
//Later on....
foreach ($custom_meta_fields as $field) {
$old = get_post_meta($post_id, $field['id'], true);
$new = $_POST[$field['id']]; //<-- BOOM
if ($new && $new != $old) {
update_post_meta($post_id, $field['id'], $new);
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field['id'], $old);
}
} // end foreach
In that foreach loop, you're attempting to grab a variable $_POST['dsmeta_image'] which doesn't exist, as your form never passes that particular key. A simple fix would be something like this:
foreach ($custom_meta_fields as $field) {
if(isset($_POST[$field['id'])){
$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);
}
}
else
continue;
} // end foreach
You also need to bear in mind that input fields of type Button DO NOT send information to the $_POST array. If this was your intention, you need to send the information you want via Hidden Field, or something else.
Hope this helps.