简体   繁体   English

在Wordpress帖子循环中显示自定义数据

[英]show custom data in Wordpress posts loop

I need to display some custom data in my category posts loop. 我需要在类别发布循环中显示一些自定义数据。 I mean I want to create special div on my post template and I want to show data from this div in this posts loop. 我的意思是我想在我的帖子模板上创建特殊的div,并希望在此帖子循环中显示该div的数据。 Can anyone help me? 谁能帮我? Thank you 谢谢

<?php
    if ( have_posts() ) :
    query_posts('cat=7'); 
    while (have_posts()) : the_post(); ?>
    <div class = "item">
    <div class="item_image"><?php the_post_thumbnail(); ?></div>
        <div class = "item_title"><?php the_title(); ?></div>
        <div class = "item_excerpt"><?php the_excerpt(10); ?></div>
        <!-- here I want to display data from each post -->
        <div class = "my_custom_data">custom data</div>
        <a href = "<?php the_permalink(); ?>" class = "item_link">Show more...</a>
    </div>
    <?php endwhile;               
    endif;
    wp_reset_query();
?>

ACF has two powerful functions get_field() and the_field(). ACF具有两个强大的函数get_field()和the_field()。 To retrieve a field value as a variable, use the get_field() function. 要将字段值检索为变量,请使用get_field()函数。 This is the most versatile function which will always return a value for any type of field. 这是最通用的函数,它将始终为任何类型的字段返回一个值。

To display a field, use the the_field() in a similar fashion. 若要显示字段,请以类似方式使用the_field()。

Now you need to get the name of the field eg if it is 'custom_title' 现在,您需要获取字段名称,例如,如果它是“ custom_title”

<?php
    if ( have_posts() ) :
    query_posts('cat=7'); 
    while (have_posts()) : the_post(); ?>
    <div class = "item">
    <div class="item_image"><?php the_post_thumbnail(); ?></div>
        <div class = "item_title"><?php the_title(); ?></div>
        <div class = "item_excerpt"><?php the_excerpt(10); ?></div>
        <!-- here I want to display data from each post -->
        <div class = "my_custom_data"><?php the_field('custom_title'); ?></div>
        <a href = "<?php the_permalink(); ?>" class = "item_link">Show more...</a>
    </div>
    <?php endwhile;               
    endif;
    wp_reset_query();
?>

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

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