简体   繁体   中英

Display ACF field inside posts loop in category.php

OK, so I need to display ACF custom field inside my posts loop in my custom category.php file. Here is the loop:

<div class="container">
<div class="row">

<?php
if ( have_posts() ) : ?>
<?php
/* Start the Loop */
while ( have_posts() ) : the_post();
?>

<div class="col-xs-12 col-sm-4">
    <?php the_title( '<h2><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h2>' ); ?>
    <div><?php MY_ACF_FIELD_GOES_HERE ?></div>
</div> 

<?php 
/* End the Loop */    
endwhile;
?>
</div><!-- .row -->
</div><!-- .container -->

As you can see loop displays pages from category (titles), but I need also display short description. I know I could use:

<?php the_excerpt(); ?>

But not in this case because excerpt contains text that I do not need inside loop. So I need to create my own short description field for every page. How can I display it in category.php template? Custom field (my own short desc) is on all pages.

You can retrieve the ACF field value using get_field('field_name'). Example-

<?php
$args = array( 'post_type' => 'speakers', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );

while ( $loop->have_posts() ) : $loop->the_post();
    echo '<div class="entry-content">';
        echo '<h2 class="speaker-name">';
            the_title();
        echo '</h2>';

            echo '<img src="' . get_field('field_name') . '" alt="" />';

            echo '<span class="speaker-title">';
                the_field('title'); echo ' / '; the_field('company_name');
            echo '</p>';

            the_content();                    

    echo '</div>';

endwhile;
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM