sort - ACF pro: Group teaser by datepicker year and display custom sub fields

Closed. This question is off-topic. It is not currently accepting answers.Your question should be specific to WordPress.

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(); ?>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745289571a4620772.html

相关推荐

  • sort - ACF pro: Group teaser by datepicker year and display custom sub fields

    Closed. This question is off-topic. It is not currently accepting answers.Your question should be specific to WordPress.

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信