admin管理员组

文章数量:1316030

I have a cpt (item), in which each post is assigned a single custom taxonomy tag (item_tags).

I want to list all tags, with description and linked titles of posts so-tagged.

I've had previous help here, and have found other code to list the tags and posts, but I can't paginate it - because they each use 'get_terms'.

The two existing options are (and, as pasted, have extra html formatting for my requirements):

$terms = get_terms('item_tags');
foreach ($terms as $term):
    ?>

    <?php echo apply_filters('the_content', $term->description) . '</dt>'; ?>
    <?php
    $posts = get_posts(array(
        'post_type' => 'item',
        'taxonomy' => $term->taxonomy,
        'term' => $term->slug,
        'posts_per_page' => -1,
        'order' => 'ASC',
    ));
    foreach ($posts as $post): // begin cycle through posts of this taxonmy
        setup_postdata($post); //set up post data for use in the loop (enables the_title(), etc without specifying a post ID)
        ?>
        <dd><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></dd>
    <?php endforeach;
    echo '</dl>' . "\n" . '<hr />' . "\n\n"; ?>
<?php endforeach; ?>

And:

global $post;
$terms = get_terms(array(
    'taxonomy' => 'item_tags',
    'hide_empty' => false,
));
if (!empty($terms) && !is_wp_error($terms)) {
    foreach ($terms as $term) {
        echo apply_filters('the_content', $term->description);
        echo '</dt>' . "\n";
        $items = get_posts(array(
            'post_type' => 'item',
            'tax_query' => array(
                array(
                    'taxonomy' => 'item_tags',
                    'terms' => $term->term_id,
                ),
            ),
            'posts_per_page' => -1,
            'order' => 'ASC',
        ));
        foreach ($items as $post) {
            setup_postdata($post);
            ?>
            <dd><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></dd>
            <?php
        }
        echo '</dl>' . "\n" . '<hr />' . "\n\n";
    }
    wp_reset_postdata();
}

FURTHER UPDATE:

Although I still want to get the solution from Max Yudin working, I've an interim alternative using code Max and also from (suggested by DHL17).

I'll include it here, as it may be useful to someone.

// Terms are formatted as ul.
// Posts are nested ul.  
// Has "\n" for line-break in source-code.

$args = array( 'hide_empty' => 1 );

$page = ( get_query_var('paged') ) ? get_query_var( 'paged' ) : 1;

// Number of tags to show per-page.
$per_page = 13;
$offset = ( $page-1 ) * $per_page;
$args = array( 'number' => $per_page, 'offset' => $offset,     'hide_empty' => 1 );


$taxonomy = 'item_tags';
$tax_terms = get_terms( $taxonomy, $args );

// Open ul for terms.
echo '<ul>' . "\n\n";

// Cycle through taxonomy terms, and open sub-ul for posts.
foreach ($tax_terms as $tax_term) {
echo '<li>' . '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a></li>' . "\n" . '<ul>' . "\n";

$post_type = 'item';
$taxonomy = 'item_tags';

// Cycle through posts for this term.
$items = get_posts( array (
'post_type'   => $post_type,
'tax_query'   => array(
array(
'taxonomy' => $taxonomy,
'terms'    => $tax_term->term_id,
), ),

// Set variables for posts-per-term
'numberposts' => -1,
'orderby' => 'date',
'order' => 'asc',
) );

global $post;

foreach ( $items as $item ) {

// Assign $item to global $post.
$post = $item;

// Post info to display.
setup_postdata( $post );
echo '<li><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></li>' . "\n";
}

// Close sub-ul for posts.
echo '</ul>' . "\n\n";

// Reset.
wp_reset_postdata();
}

// Close ul for terms.
echo '</ul>' . "\n\n";


// Pagination.
$total_terms = wp_count_terms( 'item_tags' );
$pages = ceil($total_terms/$per_page);

if( $pages > 1 ):
echo '<ul>' . "\n";

for ($pagecount=1; $pagecount <= $pages; $pagecount++):
echo '<li><a href="'.get_permalink() . 'page/' . $pagecount . '/">' .     $pagecount . '</a></li>' . "\n";
endfor;
echo '</ul>';
endif;

I have a cpt (item), in which each post is assigned a single custom taxonomy tag (item_tags).

I want to list all tags, with description and linked titles of posts so-tagged.

I've had previous help here, and have found other code to list the tags and posts, but I can't paginate it - because they each use 'get_terms'.

The two existing options are (and, as pasted, have extra html formatting for my requirements):

$terms = get_terms('item_tags');
foreach ($terms as $term):
    ?>

    <?php echo apply_filters('the_content', $term->description) . '</dt>'; ?>
    <?php
    $posts = get_posts(array(
        'post_type' => 'item',
        'taxonomy' => $term->taxonomy,
        'term' => $term->slug,
        'posts_per_page' => -1,
        'order' => 'ASC',
    ));
    foreach ($posts as $post): // begin cycle through posts of this taxonmy
        setup_postdata($post); //set up post data for use in the loop (enables the_title(), etc without specifying a post ID)
        ?>
        <dd><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></dd>
    <?php endforeach;
    echo '</dl>' . "\n" . '<hr />' . "\n\n"; ?>
<?php endforeach; ?>

And:

global $post;
$terms = get_terms(array(
    'taxonomy' => 'item_tags',
    'hide_empty' => false,
));
if (!empty($terms) && !is_wp_error($terms)) {
    foreach ($terms as $term) {
        echo apply_filters('the_content', $term->description);
        echo '</dt>' . "\n";
        $items = get_posts(array(
            'post_type' => 'item',
            'tax_query' => array(
                array(
                    'taxonomy' => 'item_tags',
                    'terms' => $term->term_id,
                ),
            ),
            'posts_per_page' => -1,
            'order' => 'ASC',
        ));
        foreach ($items as $post) {
            setup_postdata($post);
            ?>
            <dd><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></dd>
            <?php
        }
        echo '</dl>' . "\n" . '<hr />' . "\n\n";
    }
    wp_reset_postdata();
}

FURTHER UPDATE:

Although I still want to get the solution from Max Yudin working, I've an interim alternative using code Max and also from https://wordpress.stackexchange/a/22101/125227 (suggested by DHL17).

I'll include it here, as it may be useful to someone.

// Terms are formatted as ul.
// Posts are nested ul.  
// Has "\n" for line-break in source-code.

$args = array( 'hide_empty' => 1 );

$page = ( get_query_var('paged') ) ? get_query_var( 'paged' ) : 1;

// Number of tags to show per-page.
$per_page = 13;
$offset = ( $page-1 ) * $per_page;
$args = array( 'number' => $per_page, 'offset' => $offset,     'hide_empty' => 1 );


$taxonomy = 'item_tags';
$tax_terms = get_terms( $taxonomy, $args );

// Open ul for terms.
echo '<ul>' . "\n\n";

// Cycle through taxonomy terms, and open sub-ul for posts.
foreach ($tax_terms as $tax_term) {
echo '<li>' . '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a></li>' . "\n" . '<ul>' . "\n";

$post_type = 'item';
$taxonomy = 'item_tags';

// Cycle through posts for this term.
$items = get_posts( array (
'post_type'   => $post_type,
'tax_query'   => array(
array(
'taxonomy' => $taxonomy,
'terms'    => $tax_term->term_id,
), ),

// Set variables for posts-per-term
'numberposts' => -1,
'orderby' => 'date',
'order' => 'asc',
) );

global $post;

foreach ( $items as $item ) {

// Assign $item to global $post.
$post = $item;

// Post info to display.
setup_postdata( $post );
echo '<li><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></li>' . "\n";
}

// Close sub-ul for posts.
echo '</ul>' . "\n\n";

// Reset.
wp_reset_postdata();
}

// Close ul for terms.
echo '</ul>' . "\n\n";


// Pagination.
$total_terms = wp_count_terms( 'item_tags' );
$pages = ceil($total_terms/$per_page);

if( $pages > 1 ):
echo '<ul>' . "\n";

for ($pagecount=1; $pagecount <= $pages; $pagecount++):
echo '<li><a href="'.get_permalink() . 'page/' . $pagecount . '/">' .     $pagecount . '</a></li>' . "\n";
endfor;
echo '</ul>';
endif;
Share Improve this question edited May 10, 2018 at 19:25 glvr asked May 10, 2018 at 8:35 glvrglvr 8813 gold badges11 silver badges28 bronze badges 2
  • Refer to this answer wordpress.stackexchange/a/22101/125227 – DHL17 Commented May 10, 2018 at 10:44
  • @DHL17 Thanks. I'd looked at that before posting, but my brain went into fade-mode and I clearly didn't fully get it... I think because I was confused with the pagination I use for my WP-Query. I'll still need to find a way to insert the posts (which wasn't mentioned in my original title, now edited). – glvr Commented May 10, 2018 at 11:10
Add a comment  | 

1 Answer 1

Reset to default 1

This code was not tested!

Tested

Based on @Pieter Goosen answer .

<?php
$post_type = 'item';
$taxonomy = 'item_tags';

// count the number of terms for correct pagination
$term_count = get_terms( array (
    'taxonomy' => $taxonomy,
    'fields'   => 'count',
) );

// define the number of terms per page
$terms_per_page = 10;

// find out the number of pages to use in pagination
$max_num_pages = ceil( $term_count / $terms_per_page );

// get the page number from URL query
$current_page = get_query_var( 'paged', 1 );

// calculate the offset, if there is one.
$offset = 0; // initial
// or changed the if not the first (0)
if( ! 0 == $current_page) {
    $offset = ( $terms_per_page * $current_page ) - $terms_per_page;
}

// get all taxonomy terms
$terms = get_terms( array (
    'taxonomy' => $taxonomy,
    'order'    => 'ASC',
    'orderby'  => 'name',
    'number'   => $terms_per_page,
    'offset'   => $offset,
) );

echo '<dl>';

// cycle through taxonomy terms
foreach ( $terms as $term ) {

    echo '<dt>' . $term->description . '</dt>';
    echo '<dd>'; 
    echo '<ul>'; // because you can't have multiple <dd>s for one <dt>

    // cycle through posts having this term
    $items = get_posts( array (
        'post_type'   => $post_type,
        'tax_query'   => array(
            array(
                'taxonomy' => $taxonomy,
                'terms'    => $term->term_id,
            ),
        ),
        'numberposts' => -1, // different from WP_Query (see Code Ref)
    ) );

    // essential, see comments inside foreach() loop
    global $post;

    foreach ( $items as $item ) {

        // assign $item to global $post 
        $post = $item;
        // and now set up 
        setup_postdata( $post );

        echo '<li><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></li>';
        // wp_reset_postdata(); // see below
    }
    wp_reset_postdata(); // moved outside the foreach() loop

    // end posts cycle
    echo '</ul>';
    echo '</dd>';
}
// end term cycle
echo '</dl>';

// Pagination
// See the Code Reference for more arguments
echo paginate_links( array (
    'total'   => $max_num_pages,
    'current' => $current_page,
) );

本文标签: paginationHow can I get a paginated list of custom taxonomy tags with posts