wordpress初心者です。
現在他の方が書いたwordpressサイトを改修しているのですが、
投稿日を表示させるのがうまくいかずにハマっています。
具体的には
$html .= '<li>日付が入ります。</li>';
の日付が入ります。部分に 2018/10/14のように投稿日を表示させたいのですが、
このような書き方だと<?php the_time('Y/m/d'); ?>が反応せずに困っております。
$html .= '<li><?php the_time('Y/m/d'); ?></li>'; だとphpが反映されないので、
書き方をご存知な方がおりましたら、教えていただけますと幸いです。
何卒よろしくお願い致します。
なお具体的なコードは以下になります。
more-disp.php
<?php require_once("../../../wp-load.php"); require_once("../../../wp-config.php"); $now_post_num = $_POST['now_post_num']; $get_post_num = $_POST['get_post_num']; $next_now_post_num = $now_post_num + $get_post_num; $next_get_post_num = $get_post_num + $get_post_num; $sql = "SELECT $wpdb->posts.ID FROM $wpdb->posts WHERE $wpdb->posts.post_type = 'post' AND $wpdb->posts.post_status = 'publish' ORDER BY $wpdb->posts.post_date DESC LIMIT %d, %d"; $pre = $wpdb->prepare($sql,$now_post_num,$get_post_num); $results = $wpdb->get_results($pre); $next_pre = $wpdb->prepare($sql,$next_now_post_num,$next_get_post_num); $next_results = $wpdb->get_results($next_pre); $noDataFlg = 0; if ( count($results) < $get_post_num || !count($next_results) ) { $noDataFlg = 1; } $html = ""; foreach ($results as $result) { $custom_fields = get_post_custom($result->ID); $categories = get_the_terms($result->ID, "category"); $html .= '<div class="news">'; $html .= '<a href="http://example.jp/'.$categories[0]->slug.'/p='.$result->ID.'.html" class="bwWrapper"><img src="'.$custom_fields['display_image'][0].'" alt="'.$custom_fields['display_title'][0].'"></a>'; $html .= '<div class="txtarea"><a href="http://example.jp/'.$categories[0]->slug.'/p='.$result->ID.'.html">'; $html .= '<ul class="clearfix">'; $html .= '<li>'.$custom_fields['display_number'][0].'</li>'; $html .= '<li>'.get_the_time('Y/m/d').'</li>'; $html .= '</ul>'; $html .= '<h3>'.$custom_fields['display_title'][0].'</h3>'; // $html .= '<h3><a href="http://example.jp/'.$categories[0]->slug.'/p='.$result->ID.'.html">'.$custom_fields['display_title'][0].'</a></h3>'; // $html .= '<a href="http://example.jp/'.$categories[0]->slug.'/p='.$result->ID.'.html">'.$custom_fields['display_text'][0].'</a>'; $html .= '</a></div>'; $html .= '</div>'; } $returnObj = array(); $returnObj = array( 'noDataFlg' => $noDataFlg, 'html' => $html, ); $returnObj = json_encode($returnObj); echo $returnObj; ?>
JS
<script type="text/javascript"> var now_post_num = 6; // 現在表示されている数 var get_post_num = 6; // 一度に取得する数 $("a#more_disp").on("click", function() { $.ajax({ type: 'post', url: '<?php bloginfo('template_url'); ?>/more-disp.php', data: { 'now_post_num': now_post_num, 'get_post_num': get_post_num }, success: function(data) { now_post_num = now_post_num + get_post_num; console.log(data); data = JSON.parse(data); console.log(data); $(".post-list").append(data['html']); $('.bwWrapper').BlackAndWhite(); if (data['noDataFlg']) { $("#more_disp").remove(); } } }); return false; }); </script>
function.php
add_action('admin_menu', 'add_custom_fields'); add_action('save_post', 'save_custom_fields'); function add_custom_fields() { add_meta_box( 'my_sectionid', '表示用追加項目', 'my_custom_fields', 'post'); } function my_custom_fields() { global $post; $display_image = get_post_meta($post->ID,'display_image',true); $display_number = get_post_meta($post->ID,'display_number',true); $display_title = get_post_meta($post->ID,'display_title',true); $display_text = get_post_meta($post->ID,'display_text',true); echo '<p>表示するサムネイルの画像パス<br>'; echo '<input type="text" name="display_image" value="'.esc_html($display_image).'" size="60" /></p>'; echo '<p>表示するナンバー<br>'; echo '<input type="text" name="display_number" value="'.esc_html($display_number).'" size="60" /></p>'; echo '<p>表示するタイトル<br>'; echo '<input type="text" name="display_title" value="'.esc_html($display_title).'" size="60" /></p>'; echo '<p>表示するテキスト<br>'; echo '<input type="text" name="display_text" value="'.esc_html($display_text).'" size="60" /></p>'; } function save_custom_fields( $post_id ) { if(!empty($_POST['display_image'])) update_post_meta($post_id, 'display_image', $_POST['display_image'] ); else delete_post_meta($post_id, 'display_image'); if(!empty($_POST['display_number'])) update_post_meta($post_id, 'display_number', $_POST['display_number'] ); else delete_post_meta($post_id, 'display_number'); if(!empty($_POST['display_title'])) update_post_meta($post_id, 'display_title', $_POST['display_title'] ); else delete_post_meta($post_id, 'display_title'); if(!empty($_POST['display_text'])) update_post_meta($post_id, 'display_text', $_POST['display_text'] ); else delete_post_meta($post_id, 'display_text'); }
回答1件
あなたの回答
tips
プレビュー