简体   繁体   English

在category.php中的帖子循环内显示ACF字段

[英]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. 确定,因此我需要在自定义category.php文件的帖子循环中显示ACF自定义字段。 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? 如何在category.php模板中显示它? Custom field (my own short desc) is on all pages. 自定义字段(我自己的简短说明)在所有页面上。

You can retrieve the ACF field value using get_field('field_name'). 您可以使用get_field('field_name')检索ACF字段值。 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;
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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