P
P
Pavel Didenko2018-06-17 14:12:21
WordPress
Pavel Didenko, 2018-06-17 14:12:21

How to display the latest posts on the main page with thumbnails?

The site has several custom post types, for example, there are "promotions" and "articles"
Now I'm struggling with displaying the last 6 posts from the custom post type "promotions" on the main page, please help.

<?php 
  $args = array(
    'numberposts' => 6,
    'post_type' => 'akcii',
    'post_status' => 'publish',
    ); 

  $result = wp_get_recent_posts($args);

    foreach( $result as $p ){ 
  ?>
    <div class="col-12 col-sm-6 col-lg-4">
      <div class="main-articles_block">
        <div class="main-articles_header"><?php echo $p['post_title'] ?></div>
          <a href="<?php echo get_permalink($p['ID']) ?>">
            <?php the_post_thumbnail('actions-preview', ''); ?>
          </a>
          <div class="main-articles_descr">
            <div>
              <a href="<?php echo get_permalink($p['ID']) ?>">Далеко-далеко за словесными горами в стране, гласных и согласных живут рыбные тексты.</a>
            </div>
          </div>
        </div>
      </div>
<?php 
  } 
?>

Question: Why doesn't the code work? <?php the_post_thumbnail('actions-preview', ''); ?>
This code is used to display the post thumbnail in the custom post type 'akcii' prntscr.com/jvwke5, just <?php the_post_thumbnail(); ?> also tried, nothing worked.
The posts themselves are displayed on the main page, the title and link are displayed, but the picture is not displayed.
And another question: A snippet of text from the post preview in the akcii section is displayed using ACF like this prntscr.com/jvwlaf , with this implementation method, this preview cannot be displayed on the main page? Because if you insert this line into the preview text, it remains empty, and it is not an option to display by ID.
How the result looks like this now prntscr.com/jvwln0- there is not enough image and an excerpt from the entry itself should be used

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Kozlov, 2018-06-17
@Dasslier

Fix your query and loop like this. Will work.

<?php
    $args   = array(
      'post_type'      => 'akcii',
      'posts_per_page' => 6,
    );
    $recent = new WP_Query( $args );
    while ( $recent->have_posts() ) {
      $recent->the_post();
      ?>
      <div class="col-12 col-sm-6 col-lg-4">
        <div class="main-articles_block">
          <div class="main-articles_header"><?php echo get_the_title(); ?></div>
          <a href="<?php echo get_permalink();?>">
          <?php the_post_thumbnail('actions-preview', ''); ?>
          </a>
          <div class="main-articles_descr">
          <div>
            <a href="<?php echo get_permalink(); ?>">Далеко-далеко за словесными горами в стране, гласных и согласных живут рыбные тексты.</a>
          </div>
          </div>
        </div>
      </div>
      <?php
    }
    wp_reset_postdata();
    wp_reset_query();
    ?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question