Answer the question
In order to leave comments, you need to log in
How to replace this cycle?
I'm trying to display tags as titles and posts with those tags underneath them.
Wrote this shitty code
$args = array(
'posts_per_page' => 1000,
'orderby' => 'ABS',
'post_type' => 'master',
);
$lastposts = get_posts( $args );
foreach( $lastposts as $post ) {
$posttags = get_the_tags();
if( $posttags ){
foreach( $posttags as $tag ){
echo $tag->name . ' ';
}
}
}
wp_reset_postdata()
Notice: Trying to get property of non-object in
Answer the question
In order to leave comments, you need to log in
If you understand correctly, then at first you get only the labels, for this there is get_terms
Then go through the results in a loop, get the posts of this label, for this get_posts. Just pass in the request parameters that you are interested in posts with such and such a label.
Then output the tags themselves and another cycle through the posts of this tag inside the tags cycle.
Schematically like this:
$tags = get_terms(); // параметры смотрите в доке
foreach( $tags as $tag ):
$posts = get_posts([ // передаете $tag сюда согласно доке ])
?>
<h2> <?= $tag->name; ?> </h2>
<?php
foreach( $posts as $post ):
setup_postdata( $post );
?>
<article>
<h3> <?= the_title(); > </h3>
......
</article>
<?php
endforeach;
wp_reset_postdata;
endforeach;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question