Home › Forums › General Issues › Add a lot of custom fields to the_content
Hi i have a template page for a custom type post, and i use a lot of custom fields for build this.
Buy when i set the post private or protected users can see all the content.
For this what i need is add all this code to the_content
<div class="row">
<div class="equipoL col-xs-6 col-md-3">
<div class="alineacionEvento">
<img src="<?php
$teams = get_post_meta( $id, 'sp_team' );
the_field('alineacion_local') ;?>">
</div>
<div class="detallesEquipo">
<?php
$teams = get_post_meta( $id, 'sp_team' );?>
<h4 class="lesionados"><?php the_field('lesionado_local')?></h4>
<h4 class="sancionados"><?php the_field('sancionado_local')?></h4>
<h4 class="apercibidos"><?php the_field('apercibido_local')?></h4>
<h4 class="entrenador"><?php the_field('entrenador', $teams[0])?></h4>
</div>
</div>
<div class="equipoV col-xs-6 col-md-3 col-md-push-6">
<div class="alineacionEvento">
<img src="<?php
$teams = get_post_meta( $id, 'sp_team' );
the_field('alineacion_visitante') ;?>">
</div>
<div class="detallesEquipo">
<?php
$teams = get_post_meta( $id, 'sp_team' );?>
<h4 class="lesionados"><?php the_field('lesionado_visitante')?></h4>
<h4 class="sancionados"><?php the_field('sancionado_visitante')?></h4>
<h4 class="apercibidos"><?php the_field('apercibido_visitante')?></h4>
<h4 class="entrenador"><?php the_field('entrenador', $teams[1])?></h4>
</div>
</div>
<div class="contenidoEvento col-xs-12 col-md-6 col-md-pull-3">
<div class="resumenEvento">
<?php echo get_field('resumen') ?>
</div>
<div class="convocatoriaEvento">
<h4 class="convocados">Convocatoria</h4>
<?php echo get_field('convocatoria') ?>
</div>
<div class="analisisEvento">
<h4 class="analisis">Análisis</h4>
<?php echo get_field('analisis') ?>
</div>
<?php //Arbitro
$post_object = get_field('arbitro_bio');
if( $post_object ):
// override $post
$post = $post_object;
setup_postdata( $post );
?>
<div class="arbitroEvento">
<h4 class="silbato">Árbitro</h4>
<?php the_post_thumbnail('thumbnail'); ?>
<div class="infoArbitro">
<h2><?php the_title(); ?></h2>
<h3><?php the_field('colegio_de_árbitros'); ?></h3>
</div>
<?php the_excerpt(); ?>
<div class="tarjetasArbitro"><h5><span class="mediaTarjetas">Media de tarjetas por partido:</span><span class="tarjetasAmarillas"><?php the_field('tarjetas_m');?></span><span class="tarjetasRojas"><?php the_field('tarjetas_r'); ?></span></h5></div>
<?php wp_reset_postdata();?>
<?php endif; ?>
</div>
<?php if( get_field('mostrarA_1') )
{
echo '<div class="apuestasRecomendadas"><h4>Apuestas recomendadas</h4></div>
<div class="resumenEvento">
<div class="apuestasEvento">
<h5>Apuesta:<span class="tituloApuesta">' . get_field('qapuesta') . '</span></h5>
' . get_field('apuesta') . '
<h5>Cuota:<span>' . get_field('cuota') . '</span></h5>
<h5>Stake:<span>' . get_field('stake') . '</span></h5>
<h5>Casa de apuestas:<img src="' . get_field('casa_apuesta_img') . '">' . get_field('casa_apuesta') . '</h5>
<h5>Resultado:<span class="' . get_field('resultado') . '">' . get_field('resultado') . '</span></h5>
<h5>Beneficio:<span class="' . get_field('resultado') . '">' . get_field('beneficio') . '</span></h5>
</div>
</div>';
}
else
{
echo "";
}?>
<?php if( get_field('mostrarA_2') )
{
echo '<div class="resumenEvento">
<div class="apuestasEvento">
<h5>Apuesta:<span class="tituloApuesta">' . get_field('qapuesta_2') . '</span></h5>
' . get_field('apuesta_2') . '
<h5>Cuota:<span>' . get_field('cuota_2') . '</span></h5>
<h5>Stake:<span>' . get_field('stake_2') . '</span></h5>
<h5>Casa de apuestas:<img src="' . get_field('casa_apuesta_img_2') . '">' . get_field('casa_apuesta_2') . '</h5>
<h5>Resultado:<span class="' . get_field('resultado_2') . '">' . get_field('resultado_2') . '</span></h5>
<h5>Beneficio:<span class="' . get_field('resultado_2') . '">' . get_field('beneficio_2') . '</span></h5>
</div>
</div>';
}
else
{
echo "";
}?>
<?php if( get_field('mostrarA_3') )
{
echo '<div class="resumenEvento">
<div class="apuestasEvento">
<h5>Apuesta:<span class="tituloApuesta">' . get_field('qapuesta_3') . '</span></h5>
' . get_field('apuesta_3') . '
<h5>Cuota:<span>' . get_field('cuota_3') . '</span></h5>
<h5>Stake:<span>' . get_field('stake_3') . '</span></h5>
<h5>Casa de apuestas:<img src="' . get_field('casa_apuesta_img_3') . '">' . get_field('casa_apuesta_3') . '</h5>
<h5>Resultado:<span class="' . get_field('resultado_3') . '">' . get_field('resultado_3') . '</span></h5>
<h5>Beneficio:<span class="' . get_field('resultado_3') . '">' . get_field('beneficio_3') . '</span></h5>
</div>
</div>';
}
else
{
echo "";
}?>
<?php if( get_field('mostrarA_4') )
{
echo '<div class="resumenEvento">
<div class="apuestasEvento">
<h5>Apuesta:<span class="tituloApuesta">' . get_field('qapuesta_4') . '</span></h5>
' . get_field('apuesta_4') . '
<h5>Cuota:<span>' . get_field('cuota_4') . '</span></h5>
<h5>Stake:<span>' . get_field('stake_4') . '</span></h5>
<h5>Casa de apuestas:<img src="' . get_field('casa_apuesta_img_4') . '">' . get_field('casa_apuesta_4') . '</h5>
<h5>Resultado:<span class="' . get_field('resultado_4') . '">' . get_field('resultado_4') . '</span></h5>
<h5>Beneficio:<span class="' . get_field('resultado_4') . '">' . get_field('beneficio_4') . '</span></h5>
</div>
</div>';
}
else
{
echo "";
}?>
<?php if( get_field('mostrarA_sinapuesta') )
{
echo '<div class="apuestasRecomendadas"><h4>Apuestas recomendadas</h4></div>
<div class="resumenEvento">
<div class="apuestasEvento">
<h4>No recomiendo apostar</h4>
' . get_field('sin_apuesta') . '
</div>
</div>';
}
else
{
echo "";
}?>
<?php if( get_field('mostrar_otras_recomendaciones') )
{
echo '<div class="resumenEvento">
<div class="apuestasEvento">
<h4>Otras recomendaciones</h4>
' . get_field('otras_recomendaciones') . '
</div>
</div>';
}
else
{
echo "";
}?>
</div><!--FIN arbitroEvento-->
</div><!--END clearfix-->
</div><!--END ROW-->
If exist another option more easy for the private post tell me too 🙂
Thank you!
You have 2 choices.
The first is to use ACF sortcodes in the content area http://www.advancedcustomfields.com/resources/shortcode/
You can check that the person is allowed to see the page before outputting the your content.
http://codex.wordpress.org/Function_Reference/post_password_required
https://codex.wordpress.org/Function_Reference/is_user_logged_in
Thank you for your fast response!!
I see other option using some code like this:
add_filter( 'the_content', 'my_the_content_filter', 0 );
function my_the_content_filter( $content ) {
if ( is_single() )
{
global $post;
$pgLnk=get_post_meta($post->ID, 'Button', true);
$content .= 'CODE WHAT U WANT TO ADD HERE';
}
return $content;
}
but I don’t know how implement my code here, or if is posible…
Do you want all of the div you posted to be inserted into the div that holds the content? Yes you could do this here. For example you can use output buffering to enclose the content and then add it like this
add_filter( 'the_content', 'my_the_content_filter', 0 );
function my_the_content_filter( $content ) {
if (is_single()) {
global $post;
$pgLnk=get_post_meta($post->ID, 'Button', true);
ob_start();
?>
<div class="row">
... and the rest of the html
... and field you want to add to content
<?php
$content .= ob_get_clean();
}
return $content;
}
I don’t know why, but don’t work, sen me a error in the } before $content .= ob_get_clean();
i paste you here the start and the end of code
START
<?php add_filter( 'the_content', 'contenido_events_add', 0 );
function contenido_events_add( $content ) {
if ($post->post_type == 'sp-event')
global $post;
ob_start();
?>
<div class="row">
... and the rest of the html
... and field you want to add to content
END
<?php
$content .= ob_get_clean();
}
return $content
}
?>
This don’t work but i think is near to te solution, probably if you don’t check the code I pasted before this can be whit some error.
Thank you!
When i try to load the page only display me this error:
Parse error: syntax error, unexpected ‘}’ in /home3/*****/public_html/wp-content/themes/******/single-event.php on line 282
Put a semicolon at the end of the line before it
return $content;
If you are getting a syntax error with unexpected
in the message, this is usually caused by an error in the line before it.
did you remove these lines from the code I gave you?
... and the rest of the html
... and field you want to add to content
This was only to indicate where to put your code and is not proper PHP. It will cause an error if you don’t remove it.
Yes sure i deleted this.
Here a link to see the template code.
But i think the problem probably was open the { into another php tag. Can be this the problem?
You need to clean up your code a bit.
On line 130 of your code
if( $post_object ):
there is no matching
endif;
Sorry, but I don’t understand the problem in this code, the endif;
is in the line 141.
Is very probably I have some error in this template, i don’t know php…
Forget that, you are missing an open { on line 72, the first link in your function
if ($post->post_type == 'sp-event')
change it to
if ($post->post_type == 'sp-event') {
This is really basic debugging stuff. There is an unexpected } which means that something before that point is missing. You need to figure out what’s missing.
thank you a lot!
Now if i use <?php the_content();?>
this should show the added content?
I try this but don’t show nothing yet
Few things (looking at your dropbox file):
1. You probably want global $post;
before if ( $post->post_type == 'sp-event' )
.
2. I see your add_filter
hook (single-event.php) but there’s no the_content();
. Also it might be better to put your hooks in functions.php.
3. Try to start from scratch, as simple as possible. Add code back in piece by piece, and see where it goes wrong.
Good luck!
Thank you Dennis!
But neither works.
I have still searching and find a thing probably can resolve this.
First explain some things:
I used a sports plugin who create a events template, but I need customize this templates and use ACF for this.
The thing I find was this:
public function event_content( $content ) {
if ( is_singular( 'sp_event' ) )
$content = self::add_content( $content, 'event', apply_filters( 'sportspress_event_content_priority', 10 ) );
return $content;
}
the plugin is loading things on the content, how i can add here my code? is possible?
I tried to disable this and use only the code of sp-events.php but don´t work.
I’ve never worked with this plugin. You need to find out if you can override the templates used in the plugin and how to do that. If you can do this then you would modify the right template to add ACF fields to it. Or perhaps the plugin has specific hooks that you need to use to modify what’s output by the templates.
John, Thanks a LOT for the output buffer trick. I found this thread while trying to figure out the same thing.
The topic ‘Add a lot of custom fields to the_content’ 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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.