0

私はnivoスライダーwordpressプラグインを使用していません。代わりに、通常のnivoスライダーjqueryを使用して、wordpressで機能するように実装しました。現在、[続きを読む]ボタンは次のとおりです。

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

私が実装したいのはget_permalinkpage?したがって、基本的には、投稿のパーマリンクではなく、自分が選択したワードプレスページへの続きを読むリンクを作成できるようにしたいと考えています。しかし、投稿ページにカスタムオプションを実装して、ユーザーが「ページから選択してnivoスライダースライドをリンク先にリンクする(次にWebサイトにページを表示する)」と言って、その選択を出力できるようにする方法がわかりません。

何か助けはありますか?これは私が私たちのウェブサイトに実装する必要がある最後のものです!

4

2 に答える 2

1

そうです、最近自分でやったことなので、ここにあなたの答えがあります。これは本当にカスタムメタボックスについての質問です。ここにいくつかのリソースがあります-私はこれを推薦する仲間からそれに関するリンクを送られました。

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

そして、Bonesテーマでは、作者はこれを推奨しています。

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

すぐに使いたい場合は、ここにコードを投稿します。次のコードを独自のファイルに配置し、functions.phpからインクルードします。

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

テーマディレクトリにincludesディレクトリを作成し、このコードを含むファイルを作成します。

<?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');


?>

これにより、新しいカスタムメタボックスが投稿に追加され、別のURLを入力できます。このカスタムオプションのIDはhome-slide-linkになります。そのURLを使用するには、Nivoslider画像のリストを作成するときに、テンプレートループに次のURLを含めます。

 <?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>';
 ?>

したがって、投稿にスライダーリンクのURLが設定されている場合はそれを使用し、そうでない場合はデフォルトでパーマリンクになります。

これが少しお役に立てば幸いです。

于 2012-07-25T08:15:59.557 に答える
1

これがあなたに基づいた私の解決策です。

<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;
?>  
于 2012-08-28T04:11:45.047 に答える