1
votes

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. 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();
?>
1
Have you added the custom fields with info? How did you do this? You can do this as recommended here codex.wordpress.org/Custom_Fields or to make your own custom fields - omukiguy
@omukiguy I created the custom field with ACF plugin and added data. Now I want to display this data not only on the single.php template and in the page-blog.php (for example) loop where I display a thumbnail, title etc. - Yaroslav Saenko

1 Answers

0
votes

ACF has two powerful functions get_field() and the_field(). To retrieve a field value as a variable, use the get_field() function. 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.

Now you need to get the name of the field e.g if it is '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();
?>