WordPress備忘録

■カテゴリー一覧表示(リンク付き)

show posts category list with links to its archive page.
アーカイブページへのリンク付きカテゴリー一覧

<!-- category list -->
<ul>
	<?php wp_list_categories(array(
		'orderby'    => 'id',
		'show_count' => true,
		'hide_empty' => false,
		'title_li' => "",
	)); ?>
</ul>

<ul class="page_news_category c_selector">
	<li><a href="<?php echo home_url(); ?>/news/#anchor_target">すべて</a></li>
	<?php $categories = get_categories();
	foreach ($categories as $category) {
		echo '<li><a href="' . get_category_link($category->term_id) . '#anchor_target">' . $category->name . '</a></li>';
	}; ?>
</ul>

アーカイブの各記事のスラッグ表示

 <ul class="topics_list__category">
        <?php $categories = get_the_category($post->id);
        foreach ($categories as $category) {
            echo '<li><a href="' . esc_html(get_category_link($category->term_id)) . '#anchor_target">' . esc_html($category->slug) . '</a></li>';
        }; ?>
    </ul><a class="topics_list__title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>

■アーカイブページでの記事のカテゴリー表示(リンク付き)

ul+liでのシンプル表示

<?php the_category(); ?>

■wp-query basic loop

<?php
 
$args = array(
    // Arguments for your query.
);
 
// Custom query.
$query = new WP_Query( $args );
 
// Check that we have query results.
if ( $query->have_posts() ) {
 
    // Start looping over the query results.
    while ( $query->have_posts() ) {
 
        $query->the_post();
 
        // Contents of the queried post results go here.
 
    }
 
}
 
// Restore original post data.
wp_reset_postdata();
 
?>
        <?php
        $args = array();
        $query = new WP_Query($args);
        if ($query->have_posts()) :
            while ($query->have_posts()) :
                $query->the_post(); ?>


            <?php endwhile; ?>
        <?php endif; ?>
        <?php wp_reset_postdata(); ?>

■ページネーション

functions.php

/**
 * post pagenation
 */
function pagination($pages = '', $range = 4)
{
	$showitems = ($range * 2) + 1;
	global $paged;
	if (empty($paged)) $paged = 1;
	if ($pages == '') {
		global $wp_query;
		$pages = $wp_query->max_num_pages;
		if (!$pages) {
			$pages = 1;
		}
	}
	if (1 != $pages) {
		echo "<div class=\"post_pagination__body\">";
		if ($paged > 2 && $paged > $range + 1 && $showitems < $pages) echo "<a href='" . get_pagenum_link(1) . "#anchor_target'>&laquo; First</a>";
		if ($paged > 1 && $showitems < $pages) echo "<a href='" . get_pagenum_link($paged - 1) . "#anchor_target'>&lsaquo; 前</a>";
		for ($i = 1; $i <= $pages; $i++) {
			if (1 != $pages && (!($i >= $paged + $range + 1 || $i <= $paged - $range - 1) || $pages <= $showitems)) {
				echo ($paged == $i) ? "<span class=\"current\">" . $i . "</span>" : "<a href='" . get_pagenum_link($i) . "#anchor_target' class=\"inactive\">" . $i . "</a>";
			}
		}
		if ($paged < $pages && $showitems < $pages) echo "<a href=\"" . get_pagenum_link($paged + 1) . "#anchor_target \">次 &rsaquo;</a>";
		if ($paged < $pages - 1 && $paged + $range - 1 < $pages && $showitems < $pages) echo "<a href='" . get_pagenum_link($pages) . "#anchor_target'>最後 &raquo;</a>";
		echo "</div>\n";
	}
}

code to show the pagination

<?php pagination($query->max_num_pages); ?>

for post typs “‘paged’ => get_query_var(‘paged’),” is necessary in wp_query arg.

 $result_query_args = array(
                            'post_type' => 'result',
                            'paged' => get_query_var('paged'),
                        );

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です