admin管理员组

文章数量:1418333

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 5 years ago.

Improve this question

I want to group my custom post types into years.

I should look like

2019 Post 1 Post 2 Post 3

2018 Post 1 Post 2

2015 Post 1

YEAR Post 1..n

Following this example here: Order and group posts by acf by month and year I've set up my code like this.

<?php get_header(); ?>
<?php /* Template Name: Konzerte */ 
// Überschrift-Element mit Headline, Bild, Schwung und Text; 800px hoch
require('inc/stage.php'); ?>
<?php 
// get posts
$posts = get_posts(array(
'post_type'         => array('concertreport', 'concertannouncement'),
'posts_per_page'    => -1,
'meta_key'          => 'concertdate',
'orderby'           => 'meta_value',
'order'             => 'DESC'
)); 
?>


<?php
    $group_posts = array();

        if( $posts ) {
            foreach( $posts as $post ) {
                $date = get_field('concertdate', $post->ID, false);
                $date = new DateTime($date);
                $year = $date->format('Y');
                $group_posts[$year][] = array($post, $date);
            }

        }
        foreach ($group_posts as $yearKey => $years) { ?>

    <h2><?php echo $yearKey; ?></h2>
    <?php foreach ($years as $postKey => $posts) { ?>
      <?php echo $posts[1]->format('d-m-Y');
          echo ' / ';
          echo $posts[0]->post_title;
          echo '<br>';
          // Instead of "echo $posts[0]->post_title;" I want to display the ACF-(Sub-)fields like teaser-text, image, etc. 

        } 
    }
?>

<?php get_footer(); ?>

Result, which is really fine:

As you can see in my comment, I'd like to show the ACF fields there. Custom image, custom teaser-text, etc. I don't know why, if I copy my code from ACF there, for example the title instead of the comment:

<div class="col-md-7 bg-wso-white">
  <?php the_title( '<h3 class="font-weight-bold text-left text-wso-black text-uppercase pb-1"><a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="bookmark">', '</a></h3>' ); ?>
</div>

Result, which isn't so fine ;-):

It only displays the text of the oldest date repeatedly for all posts. My acf sub-fields doesn't display at all. Can anybody help me?

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 5 years ago.

Improve this question

I want to group my custom post types into years.

I should look like

2019 Post 1 Post 2 Post 3

2018 Post 1 Post 2

2015 Post 1

YEAR Post 1..n

Following this example here: Order and group posts by acf by month and year I've set up my code like this.

<?php get_header(); ?>
<?php /* Template Name: Konzerte */ 
// Überschrift-Element mit Headline, Bild, Schwung und Text; 800px hoch
require('inc/stage.php'); ?>
<?php 
// get posts
$posts = get_posts(array(
'post_type'         => array('concertreport', 'concertannouncement'),
'posts_per_page'    => -1,
'meta_key'          => 'concertdate',
'orderby'           => 'meta_value',
'order'             => 'DESC'
)); 
?>


<?php
    $group_posts = array();

        if( $posts ) {
            foreach( $posts as $post ) {
                $date = get_field('concertdate', $post->ID, false);
                $date = new DateTime($date);
                $year = $date->format('Y');
                $group_posts[$year][] = array($post, $date);
            }

        }
        foreach ($group_posts as $yearKey => $years) { ?>

    <h2><?php echo $yearKey; ?></h2>
    <?php foreach ($years as $postKey => $posts) { ?>
      <?php echo $posts[1]->format('d-m-Y');
          echo ' / ';
          echo $posts[0]->post_title;
          echo '<br>';
          // Instead of "echo $posts[0]->post_title;" I want to display the ACF-(Sub-)fields like teaser-text, image, etc. 

        } 
    }
?>

<?php get_footer(); ?>

Result, which is really fine:

As you can see in my comment, I'd like to show the ACF fields there. Custom image, custom teaser-text, etc. I don't know why, if I copy my code from ACF there, for example the title instead of the comment:

<div class="col-md-7 bg-wso-white">
  <?php the_title( '<h3 class="font-weight-bold text-left text-wso-black text-uppercase pb-1"><a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="bookmark">', '</a></h3>' ); ?>
</div>

Result, which isn't so fine ;-):

It only displays the text of the oldest date repeatedly for all posts. My acf sub-fields doesn't display at all. Can anybody help me?

Share Improve this question edited Jul 28, 2019 at 18:26 nullstein asked Jul 28, 2019 at 17:30 nullsteinnullstein 32 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

The reason you can't use ACF or functions such as the_title() is because they can only be accessed from inside The Loop (see here https://codex.wordpress/The_Loop).

Its unnecessary to have multiple loops so I've merged them into one which will display any post data you need. Within this loop it checks whether the current post's year is equal to the previous post's year before it displays it.

<?php /* Template Name: Konzerte */
// Überschrift-Element mit Headline, Bild, Schwung und Text; 800px hoch
require('inc/stage.php');

    global $post;

    // get posts
  $posts = get_posts(array(
      'post_type'         => array('concertreport', 'concertannouncement'),
      'posts_per_page'    => -1,
      'meta_key'          => 'concertdate',
      'orderby'           => 'meta_value',
      'order'             => 'DESC'
  ));

  $group_posts = array();

    if ($posts) {
      $previous_year = '';
      foreach ($posts as $post) {
        $date = get_field('concertdate', $post->ID, false);
        $date = new DateTime($date);
        $current_year = $date->format('Y');

        // If $previous_year is empty, display $current_year
        if ($previous_year == '') {
            echo '<h2>' . $current_year . '</h2>';
        }
        // Else if $previous_year is not equal to $current_year, display $current_year
        else if ($previous_year !== $current_year) {
            echo '<h2>' . $current_year . '</h2>';
        }

        $previous_year = $date->format('Y');
        echo $date->format('d-m-Y');
        echo ' / ';
        echo get_the_title();
        echo '<br>';
      }
    }
?>

<?php get_footer(); ?>

本文标签: sortACF pro Group teaser by datepicker year and display custom sub fields