スポンサーサイト
2021年2月22日
皆さんこんにちは!こんばんは!!ロト君です。
今回は、先日投稿した記事の完全版を作りましたので、そちらの紹介をしたいと思います。
尚、簡単な確認できるテーマの方もザックリ作りましたので一緒に掲載します。(痛風で出来ることは、これぐらいでしたので、作りましたwww)
1.indexphp
index.phpはこんな感じです。var_dump()は必要な際に、コメント外して下さい。
設定環境は前回同様に、組んでいます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?php get_header(); ?> <article> <?php $paged = ( get_query_var(' paged' )) ? get_query_var( 'paged' ) : 1; $args = array( 'post_type' => array( 'post', 'info' ), ); $wp_query = new WP_Query( $args ); while($wp_query->have_posts()){ ?> <section class="inline-block"> <?php the_post(); the_title( '<h2>', '</h2>' ); the_post_thumbnail( 'thumbnail' ); echo '<h3>' . get_post_type( $post ) . '</h3>'; echo '<p>POST_id ' . get_the_ID() . '</p>'; echo '<p>SCF ID ' . SCF::gets()[ 'imggroup' ][ 0 ][ 'img' ] . '</p>'; //var_dump( SCF::gets() ); ?> </section> <?php } ?> </article> <?php get_footer(); ?> |
2.functions.php
functions.phpはこんな感じです。この、functions.phpが最も重要ですね!!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
<?php // SCF::get( 'field-name' )により取得可能 function my_def_thumbnail() { $post_id = get_the_ID(); $post_type = get_post_type( $post_id ); $arr_types = array( 'post', 'info' ); if ( in_array( $post_type, $arr_types ) ) { //if (get_post_type() == 'post' || get_post_type() == 'info') { // $wpdbのグローバル有効化 global $wpdb; // 投稿IDを取得 //$post_id = get_the_ID(); // クエリの作成 // imgのpost_idを検出する $query = " SELECT meta_value FROM $wpdb->postmeta WHERE post_id = $post_id AND meta_key = 'img' "; // クエリの実行 $datas = $wpdb->get_results($query); // 取得したIDでアイキャッチ画像のレコードを更新 update_post_meta($post_id, '_thumbnail_id', $datas[0]->meta_value); } } // 公開・更新などでの関数の実行 add_action('updated_postmeta', 'my_def_thumbnail'); ?> <?php // アイキャッチを投稿エディタで有効化 add_theme_support( 'post-thumbnails' ); ?> <?php function add_files() { // サイト共通のCSSの読み込み wp_enqueue_style( 'main', get_template_directory_uri() . '/style.css', "", date( 'U' ) ); } add_action( 'wp_enqueue_scripts', 'add_files' ); ?> |
Pointは前回同様、save_postではなく、updated_postmetaですね!!
3.まとめ
知り合いと、数日考えて出来た次第です。もっとシンプルに、コードを書けるかも知れませんが、私の現状ではこんな感じでしたww
何かの役に立てればと思います・・・・。下記のリンクにテーマを置いときます!!
では、今回はここまで!(*^-^*)ノシミ★