カテゴリー
WordPress

【WordPress】最後の文字列を取得して、処理を分岐

ある拡張子だけ別処理をしたかったので使用しました。忘れないうちに写経します。

<?php $files = get_field('xxx');
if(mb_substr("$xxx", -4) == 'xlsx'):?>
  「<?php the_title(); ?>」をダウンロードしています。<br>ダウンロードがはじまらない場合は<a href="<?php the_field('xxx') ?>">こちらをクリック</a>して下さい。
  <?php endif; ?>

追記:
拡張子を取得する関数がありました。
http://php.net/manual/ja/splfileinfo.getextension.php
SplFileInfo::getExtension — ファイルの拡張子を取得する

カテゴリー
WordPress

【WordPress】ループサンプル / ループの入れ子

タクソノミータームごとに、記事一覧を作りたいなどのサンプル

タクソノミーターム名
 記事
 記事
 記事
タクソノミーターム名
 記事
 記事
 記事
タクソノミーターム名
 記事
 記事
 記事

のようになる。

    <?php
    $count = 1;
$args = array(
    'orderby'       => 'name', 
    'order'         => 'ASC',
    'hide_empty'    => false, 
); 
$terms = get_terms( 'doc', $args );
foreach ( $terms as $term ) :
$term_link = get_term_link( $term, 'タクソノミー名' );//リンク取得
echo '<div>' .$term->name. '</div>' .esc_url( $term_link ). '('  .$term->count;
?>
<?php $count++;?>
<?php
    $args = array (
        'post_type'   => 'post_type名',
        'タクソノミー名'     => $term->slug,
        'numberposts' => -1,
        'orderby' =>'meta_value',
        'order' => 'ASC',
    );
    $myposts = get_posts($args);

    foreach ($myposts as $post) :
        setup_postdata($post);
?>

<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>

<?php
endforeach;endforeach;
wp_reset_postdata();
?>