2016-12-22 12 views
0

"Resource"という名前の投稿のためのメタボックスを作成しました。そこでは、バックエンドの事前定義されたselect要素から値を選択できます。それを保存した後、フロントエンドに表示されます。画像をご覧ください。wordpressのthe_tags()やthe_category()のような関数を作成するには?

Back-end

Front-end

今、私は、これは、タグ、著者とカテゴリのような仕事をしたいです。クリックすると、同じ値で選択されたすべての投稿が表示されます。しかし、どこから始めるべきかわからない。

私は、メタボックスプラグインを使用してfunction.php

add_filter('rwmb_meta_boxes', 'resource_meta_box'); 
function resource_meta_box($meta_boxes) { 

    $meta_boxes[] = array(
     'id'   => 'resource_box', 
     'title'  => esc_html__('Resource'), 
     'post_types' => array('post'), 
     'context' => 'side', 
     'priority' => 'high', 
     'autosave' => true, 

     // List of meta fields 
     'fields'  => array(
      // RESOURCE BOX 
      array(
       'name'  => esc_html__(''), 
       'id'   => "resource", 
       'type'  => 'select', 
       // Array of 'value' => 'Label' pairs for select box 
       'options'  => array(
        'Item 1' => esc_html__('Item 1'), 
        'Item 2' => esc_html__('Item 2'), 
        'Item 3' => esc_html__('Item 3'), 
        'Item 4' => esc_html__('Item 4'), 
        'Item 5' => esc_html__('Item 5'), 
       ), 
       // Placeholder of Select. 
       'placeholder' => esc_html__('None') 
      ) 
     ) 
    ); 
    return $meta_boxes; 
} 

と、それは事前に<?php echo rwmb_meta('resource'); ?>

感謝を表示するために必要な次のコードでは、次のコードをしました。

+0

投稿のメタデータをどのように取得したのかを読んでから、その投稿をしてください。あなたが途中で立ち往生した場合は、戻ってきて試したことを私たちに見せてください。しかし、努力したことを示す必要があります。 –

+0

https://developer.wordpress.org/reference/functions/get_post_meta/ –

答えて

0

カスタムタクソノミーのために行く必要があります。なぜなら、それはあなたがそれに組み込まれた機能を必要とするからです。投稿や 'のために(例えば、「ポスト」をテキストポスト型スラグと「あなたのポスト型-スラグ」を置き換えるここ

$labels = array(
    'name'      => _x('Resources', 'taxonomy general name', 'textdomain'), 
    'singular_name'    => _x('Resource', 'taxonomy singular name', 'textdomain'), 
    'search_items'    => __('Search Resources', 'textdomain'), 
    'popular_items'    => __('Popular Resources', 'textdomain'), 
    'all_items'     => __('All Resources', 'textdomain'), 
    'parent_item'    => null, 
    'parent_item_colon'   => null, 
    'edit_item'     => __('Edit Resource', 'textdomain'), 
    'update_item'    => __('Update WResource', 'textdomain'), 
    'add_new_item'    => __('Add New Resource', 'textdomain'), 
    'new_item_name'    => __('New Resource Name', 'textdomain'), 
    'separate_items_with_commas' => __('Separate Resources with commas', 'textdomain'), 
    'add_or_remove_items'  => __('Add or remove Resources', 'textdomain'), 
    'choose_from_most_used'  => __('Choose from the most used Resources', 'textdomain'), 
    'not_found'     => __('No Resources found.', 'textdomain'), 
    'menu_name'     => __('Resources', 'textdomain'), 
); 

$args = array(
    'hierarchical'   => true, 
    'labels'    => $labels, 
    'show_ui'    => true, 
    'show_admin_column'  => true, 
    'update_count_callback' => '_update_post_term_count', 
    'query_var'    => true, 
    'rewrite'    => array('slug' => 'resource'), 
); 

register_taxonomy('resource', 'your-post-type-slug', $args); 

あなたのテーマのfunctions.phpのfile.enterコードで怒鳴る与えられたコードを貼り付けページの場合は「ページ」など)を使用して、分類を関連付けることができます。

その後、「リソース」分類の下

get_terms('resource', array(
    'hide_empty' => false, 
)); 

を作成したすべての用語は、フィルタリング(新しく追加された分類による)後のリストを表示するには、archive.phpから分類-resource.phpをテンプレートファイルを作成、取得するには以下のコードを使用します。

the_terms(get_the_ID(),'resource', $before, $sep, $after);を使用すると、ループ内の特定の投稿のリソースを選択できます。

+0

こんにちは、詳細コードと説明に感謝します。うまく動作し、URLも作成されますが、問題は404が見つかりませんでした。 –

関連する問題