web-dev-qa-db-ja.com

タイトルにクラスを追加する投稿画面にチェックボックスを追加する

私はいくつかの投稿に他のものとは異なるタイトルカラーを持たせる必要があります、そして私はこれが投稿作成/編集スクリーンのある種のチェックボックスによって最も便利に達成されるであろうと考えました。チェックボックスを追加してからテンプレートの値を取得する方法を教えてください。

ありがとう

5
SudoWP

はい、その通りです。 カスタムメタボックス がPost Editing画面にチェックボックスを作成します。

post custom meta box

この場合は、ラジオボタンです。必要な値は1つだけです。

以下はそれを作成するコードです。これをあなたのテーマのfunctions.phpファイルに入れるか、 シンプルなプラグインを作成してください これはテーマに依存しないものになります。

/* Define the custom box */
add_action( 'add_meta_boxes', 'wpse_61041_add_custom_box' );

/* Do something with the data entered */
add_action( 'save_post', 'wpse_61041_save_postdata' );

/* Adds a box to the main column on the Post and Page edit screens */
function wpse_61041_add_custom_box() {
    add_meta_box( 
        'wpse_61041_sectionid',
        'Title Color',
        'wpse_61041_inner_custom_box',
        'post',
        'side',
        'high'
    );
}

/* Prints the box content */
function wpse_61041_inner_custom_box($post)
{
    // Use nonce for verification
    wp_nonce_field( 'wpse_61041_wpse_61041_field_nonce', 'wpse_61041_noncename' );

    // Get saved value, if none exists, "default" is selected
    $saved = get_post_meta( $post->ID, 'title_color', true);
    if( !$saved )
        $saved = 'default';

    $fields = array(
        'red'       => __('Red', 'wpse'),
        'green'     => __('Green', 'wpse'),
        'blue'      => __('Blue', 'wpse'),
        'default'   => __('Default', 'wpse'),
    );

    foreach($fields as $key => $label)
    {
        printf(
            '<input type="radio" name="title_color" value="%1$s" id="title_color[%1$s]" %3$s />'.
            '<label for="title_color[%1$s]"> %2$s ' .
            '</label><br>',
            esc_attr($key),
            esc_html($label),
            checked($saved, $key, false)
        );
    }
}

/* When the post is saved, saves our custom data */
function wpse_61041_save_postdata( $post_id ) 
{
      // verify if this is an auto save routine. 
      // If it is our form has not been submitted, so we dont want to do anything
      if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
          return;

      // 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['wpse_61041_noncename'], 'wpse_61041_wpse_61041_field_nonce' ) )
          return;

      if ( isset($_POST['title_color']) && $_POST['title_color'] != "" ){
            update_post_meta( $post_id, 'title_color', $_POST['title_color'] );
      } 
}

この回答に基づくコード

テーマでそれを使用する方法

適切なCSSルール(h1.defaulth1.redなど)を使い、カラークラスをタイトル(index.phpsingle.phpなど)に適用する場所には、次のようなものを使用します。

<?php $title_color = get_post_meta( get_the_ID(), 'title_color', true); ?>
<h1 class="entry-title <?php echo esc_attr($title_color); ?>"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
9
brasofilo

@brasofiloの作業は素晴らしいですが、コーディングをあまりしたくない場合は、Custom Fieldsから同じ作業を行うことができます。デフォルトでは非表示です。 Screen Optionsから見えるようにすることができます。新しいフィールドを作成してcolorという名前を付け、valueフィールドに色の名前を入れます。それから、このコードをあなたのテーマのheader.phpタグの下の</head>に置きます。

<style type="text/css">
/*Replace [color-name] with you color example: red*/
#red {color:"red";}
#green {color:"green";}
#[color-name] {color:"[color-name]";}
#[color-name] {color:"[color-name]";}
</style>
    <?php
global $post;
$color = get_post_meta(get_the_ID(), 'color', true);
$id = "id="
?>

それから<h1 class="entry-title"を検索し、このコードを<?php echo $id, $color;?>の中に入れます。タイトルカラーを変更したいときは、カスタムフィールドのドロップダウンでcolorを選択し、valueにカラー名を入力します。

1
John Doe