固定ページ内にPHPをショートコードで設置した際
-
固定ページ内にphpを挿入したくて調べていたらショートコードで読み込む方法にたどり着きました。
=============================================
function Include_my_php($params = array()) {
extract(shortcode_atts(array(
‘file’ => ‘default’
), $params));
ob_start();
include(get_theme_root() . ‘/’ . get_template() . “/$file.php”);
return ob_get_clean();
}add_shortcode(‘myphp’, ‘Include_my_php’);
=============================================
こちらのコードをfunction.phpに記入して、phpファイルを作成し、ショートコードを使えるようにしました。phpファイルの内容は
=============================================
<?php $args = array(
‘post_type’ => ‘class’, //投稿タイプは制作事例なのでworksに設定
‘tax_query’ => array( //タクソノミー、タームの設定
array(
‘taxonomy’ => ‘class-tag’, //タクソノミーはエリアなのでareaに設定
‘field’ => ‘slug’, //基本的にslugに指定します(必須)
‘terms’ => ‘tantou’ //タームは国内なのでdomesticに設定
)
)
);
$domestic_post = get_posts($args);
if($domestic_post) : foreach($domestic_post as $post) : setup_postdata( $post ); ?><?php $terms = get_the_terms( $post->ID, ‘type’ );
if ( ! is_wp_error( $terms ) && $terms) :
foreach ( $terms as $term) :
?><?php endforeach; endif;?>
<div class=”class-<?php echo esc_attr( $term->slug ); ?>”>
“>-
<div class=”area”><?php echo get_post_meta($post->ID , ‘地域名’ ,true); ?></div>
<?php echo get_post_meta($post->ID , ‘曜日’ ,true); ?><br>
<?php echo get_post_meta($post->ID , ‘レッスン時間’ ,true); ?>
</div><?php endforeach; ?>
<?php else : //記事が存在しない場合 ?>
<p>表示する記事がありません。</p>
<?php endif;
wp_reset_postdata(); ?>
=============================================
にしており内容の表示はされるのですが、リンク先が同ページになりリンクがうまく機能しません。。。
<?php the_permalink(); ?>
はこの記述では使えないのでしょうか??プログラムに詳しく無いもので調べてこの方法に行き着いたのですが、そのた良い方法があればご教授お願いいたします。。。
-
<div class=”area”><?php echo get_post_meta($post->ID , ‘地域名’ ,true); ?></div>
- トピック「固定ページ内にPHPをショートコードで設置した際」には新たに返信することはできません。