Home › Forums › General Issues › ACF in attachment-single.php
In the attachment-single.php page the media file requested from the the_content function is shown.
The problem I have is that in the get_field page is showing the result of get_field and the results in the_content (both), should not show the_content.
Why it can be happening?
The get_field configuration is : http://i.imgur.com/QZcxPP2.jpg
I am not sure if someone will understand the problem. Can you explain it differently? I dont see how that screenshot is connected with the problem you are trying to describe. What’s exactly the issue?
The screen only shows the type of field that I use.
The code I use is as follows:
<?php
if ( get_field( ‘chapo’ ) )
{ ?>
<?php echo get_field( ‘chapo’ ); ?>
<?php
} ?>
<?php the_content(); ?>
In get_field(‘Chapo’) is being added what is on the_content(); and the content of get_field(‘Chapo’).
The same goes for other get_field I have on the page.
OK, it’s an error in the php logic. In your code you are saying IF chapo, display ‘chapo’ and also the_content(). You need to use ELSE to display one or another, such as:
<?php $field = get_field('chapo');?>
<?php if( !empty($field) ) { ?>
<?php echo $field; ?>
<?php } else { ?>
<?php the_content(); ?>
<?php } ?>
I am also adding a condition that says: if ‘chapo’ is not empty. Which is helpful most of the time.
Cheers!
No, I’m sorry if I was unclear. The problem is that the *content* of the post is being displayed where **get_field** is called. For example, on attachment pages, the attachment content appears before the content of the field:
<audio><!-- attachment audio stuff... --></audio>
<p>WYSIWYG content from get field</p>
The entire **<audio/>** tag should not appear there, it is not called in the code and is not in the WYSIWYG content of the field.
This error only appears with WYSIWYG fields.
can you specify:
1) what are you puting in the chapo field.
2) what are you putting in the content editor
3) where / how is the attachment being added
4) whats the code in your php file
5) what should be the desired output?
1) what are you puting in the chapo field.
<p>WYSIWYG content from get field</p>
2) what are you putting in the content editor
<audio><!-- attachment audio stuff... --></audio>
(this really is the media file)
3) where / how is the attachment being added
From the media library (wp-admin/media-new.php)
4) whats the code in your php file
page.php
<?php
get_header();
while (have_posts())
{
the_post();
if ( get_post_type() == 'attachment' )
{
get_template_part( 'attachment', 'single' );
}
else
{
get_template_part( 'content', 'page' );
}
}
get_footer();
attachment-single.php
<div class="chapo">
<?php
if ( get_field( 'chapo' ) )
{ ?>
<?php echo get_field( 'chapo' ); ?>
<?php
} ?>
</div>
<div class="content">
<?php the_content(); ?>
</div>
5) what should be the desired output?
<div class="chapo"><p>WYSIWYG content from get field</p></div>
<div class="content"><audio><!-- attachment audio stuff... --></audio></div>
6. What actually appears?
<div class="chapo"><audio><!-- attachment audio stuff... --></audio><p>WYSIWYG content from get field</p></div>
<div class="content"><audio><!-- attachment audio stuff... --></audio></div>
Note: This happens with any media file, not just audio.
Ok, now it’s clearer.
My question would be: if you just need to output text in a WYSIWYG field, why dont you just write the text in the_content() and attach the audio to that field as well? Why two fields?
But try this code and tell me if it works:
<div class="chapo">
<?php the_field('chapo'); ?>
</div>
<div class="content">
<?php the_content(); ?>
</div>
If it doesn’t, can you inspect the html with chrome or firefox and tell me what’s being displayed inside these divs?
It makes no difference whether I use the_field
or get_field
. I have tried both.
I have no problem displaying the content. Please do not concern yourself with the_content
.
The problem is that what is returned from get_field
(or displayed by the_field
) __includes__ the content.
The topic ‘ACF in attachment-single.php’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.