初めまして、PHP初心者です。
Wordpressを使用しております。
function.phpにて、ショートコードで任意のPHPファイルをサイドバーに呼び出す方法を行いました。
呼び出すPHPファイルの中でif文があるのですが、else文にあるテキスト文字しか読み込まれませんでした。
直接PHPファイルを呼び出すと正常に出力される為、呼び出す方法に問題があると思われます。
何か良い方法はありませんでしょうか。
【function.phpに追加したコード】
PHP
1//phpファイル読込ショートコードここから 2function test_Include($params = array()) { 3 extract(shortcode_atts(array('file' => 'default'), $params)); 4 ob_start(); 5 include(STYLESHEETPATH . "/$file.php"); 6 return ob_get_clean(); 7} 8add_shortcode('test', 'test_Include'); 9//phpファイル読込ショートコードここまで
【サイドバーにてショートコードを呼び出す】
PHP
1[test file='呼び出したいPHPファイル名']
【追記:呼び出すPHPファイル】
PHP
1<?php 2$categories = get_the_category($post->ID); 3$category_ID = array(); 4foreach($categories as $category): 5 array_push( $category_ID, $category -> cat_ID); 6endforeach ; 7$args = array( 8 'post__not_in' => array($post -> ID), 9 'posts_per_page'=> 10, 10 'category__in' => $category_ID, 11 'orderby' => 'rand', 12); 13$query = new WP_Query($args); ?> 14 <?php if( $query -> have_posts() ): ?> 15 <?php while ($query -> have_posts()) : $query -> the_post(); ?> 16 <div class="related-entry"> 17 <div class="related-entry-thumb"> 18 <a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"> 19 <?php if ( has_post_thumbnail() ): ?> 20 <?php echo get_the_post_thumbnail($post->ID, 'thumb100'); ?> 21 <?php else: ?> 22 <img src="<?php echo get_template_directory_uri(); ?>/images/no-image.png" alt="NO IMAGE" title="NO IMAGE" width="100px" /> 23 <?php endif; ?> 24 </a> 25 </div><!-- /.related-entry-thumb --> 26 27 <div class="related-entry-content"> 28 <h4 class="related-entry-title"> <a href="<?php the_permalink(); ?>"> 29 <?php the_title(); ?> 30 </a></h4> 31 <p class="related-entry-snippet"> 32 <?php echo mb_substr( strip_tags( $post->post_content ), 0, 70 ) . ''; ?></p> 33 <p class="related-entry-read"><a href="<?php the_permalink(); ?>">記事を読む</a></p> 34 </div><!-- /.related-entry-content --> 35 </div><!-- /.new-entry --> 36 37 <?php endwhile;?> 38 39 <?php else:?> 40 <p>記事はありません</p> 41 <?php 42endif; 43wp_reset_postdata(); 44?>
回答1件
あなたの回答
tips
プレビュー