Support

Account

Home Forums Add-ons Repeater Field My code doesn't work

Solving

My code doesn't work

  • Hi everyone.

    I’m creating a code to show the information of PDF documents, but I can see the result, because it’s seems to be that I made a mistake, but I can’t found it.

    Updated code:

    <!-- MOSTRAR CAMPOS PERSONALIZADOS -->
    
    	<!-- Documento principal -->
    
    		<?php
    	$attachment_id = get_field('archivo');
    	$url = wp_get_attachment_url($attachment_id);
    	$title = get_the_title($attachment_id);
    	$path_info = pathinfo(get_attached_file($attachment_id));
    	$filesize = filesize( get_attached_file( $attachment_id ) );
    	$filesize = size_format($filesize, 2);
    
    		if(get_field('archivo')): ?>
    			<div class="titulos_secundarios">Ver documento:</div><br/><br/>
    			<a href="<?php echo $url; ?>" target="_blank" title="Abrir documento en una nueva pestaña" style="display: inline;"><?php echo $title?>.<?php echo $path_info['extension'];?> (<?php echo $filesize; ?>)</a>
    		<?php 
    		endif; 
    ?>
    	<!-- Documento secundarios -->
    
    		<br/>
    		<div class="titulos_secundarios">Antecedentes</div>
    		<br/>
    
    <?php if(have_rows('otros_archivos')):
    	while (have_rows('otros_archivos')) : the_row();
    	$post_object = get_sub_field('antecedente');
    		echo "<ol>";
    		if($post_object): 
    			$post = $post_object; setup_postdata($post); ?>
    			<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    		<?php wp_reset_postdata();
    		else:
    			echo "<li>No se ha incluido ningún antecedente aún.</li><br/>";
    		endif;
    	echo "</ol>";
    	endwhile;
    endif; ?>
    <!-- FIN MOSTRAR CAMPOS PERSONALIZADOS -->

    Thanks in advance.

  • First error is a missing <?php tag on line 10, it should be

    
    <?php if(get_field('archivo')): ?>
    
  • I can resolve my problem, however the last loop doesn’t work. The loop repeat the same information and it shows the title and link of the current post.

    Code:

    <?php if(have_rows('otros_archivos')):
    	while (have_rows('otros_archivos')) : the_row();
    	$post_object = get_sub_field('antecedente');
    		echo "<ol>";
    		if($post_object): 
    			$post = $post_object; setup_postdata($post); ?>
    			<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    		<?php wp_reset_postdata();
    		else:
    			echo "<li>No se ha incluido ningún antecedente aún.</li>";
    		endif;
    	echo "</ol>";
    	endwhile;
    endif; ?>

    You can see the results on this webpage.

  • Is your code inside a template file that is loaded using one of the standard WP functions for loading a template file of some kind? like get_templat_part() how is this file loaded? Where is the code located?

  • The code is located on the file “content.php” of the theme Stargazer. I can’t found a standard function. The complete code is presented bellow:

    <article <?php hybrid_attr( 'post' ); ?>>
    
    	<?php if ( is_singular( get_post_type() ) ) : // If viewing a single post. ?>
    
    		<header class="entry-header">
    
    			<h1 <?php hybrid_attr( 'entry-title' ); ?>><?php single_post_title(); ?></h1>
    
    			<div class="entry-byline">
    				<span <?php hybrid_attr( 'entry-author' ); ?>><?php the_author_posts_link(); ?></span>
    				<time <?php hybrid_attr( 'entry-published' ); ?>><?php echo get_the_date(); ?></time>
    				<?php comments_popup_link( number_format_i18n( 0 ), number_format_i18n( 1 ), '%', 'comments-link', '' ); ?>
    				<?php if ( function_exists( 'ev_post_views' ) ) ev_post_views( array( 'text' => '%s' ) ); ?>
    				<?php edit_post_link(); ?>
    			</div><!-- .entry-byline -->
    
    <!-- MOSTRAR CAMPOS PERSONALIZADOS -->
    
    	<!-- Documento principal -->
    
    		<?php
    	$attachment_id = get_field('archivo');
    	$url = wp_get_attachment_url($attachment_id);
    	$title = get_the_title($attachment_id);
    	$path_info = pathinfo(get_attached_file($attachment_id));
    	$filesize = filesize( get_attached_file( $attachment_id ) );
    	$filesize = size_format($filesize, 2);
    
    		if(get_field('archivo')): ?>
    			<div class="titulos_secundarios">Ver documento:</div><br/><br/>
    			<a href="<?php echo $url; ?>" target="_blank" title="Abrir documento en una nueva pestaña" style="display: inline;"><?php echo $title?>.<?php echo $path_info['extension'];?> (<?php echo $filesize; ?>)</a>
    		<?php 
    		endif; 
    ?>
    	<!-- Documentos secundarios -->
    
    		<br/>
    		<div class="titulos_secundarios">Antecedentes</div>
    		<br/>
    
    <?php if(have_rows('otros_archivos')):
    	while (have_rows('otros_archivos')) : the_row();
    	$post_object = get_sub_field('antecedente');
    		echo "<ol>";
    		if($post_object): 
    			$post = $post_object; setup_postdata($post); ?>
    			<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    		<?php wp_reset_postdata();
    		else:
    			echo "<li>No se ha incluido ningún antecedente aún.</li><br/>";
    		endif;
    	echo "</ol>";
    	endwhile;
    endif; ?>
    <!-- FIN MOSTRAR CAMPOS PERSONALIZADOS -->
    
    		</header><!-- .entry-header -->
    
    		<div <?php hybrid_attr( 'entry-content' ); ?>>
    			<?php the_content(); ?>
    			<?php wp_link_pages(); ?>
    		</div><!-- .entry-content -->
    
    		<footer class="entry-footer">
    			<?php hybrid_post_terms( array( 'taxonomy' => 'category', 'text' => __( 'Posted in %s', 'stargazer' ) ) ); ?>
    			<?php hybrid_post_terms( array( 'taxonomy' => 'post_tag', 'text' => __( 'Tagged %s', 'stargazer' ), 'before' => '<br />' ) ); ?>
    		</footer><!-- .entry-footer -->
    
    	<?php else : // If not viewing a single post. ?>
    
    		<?php get_the_image( array( 'size' => 'stargazer-full', 'order' => array( 'featured', 'attachment' ) ) ); ?>
    
    		<header class="entry-header">
    
    			<?php the_title( '<h2 ' . hybrid_get_attr( 'entry-title' ) . '><a href="' . get_permalink() . '" rel="bookmark" itemprop="url">', '</a></h2>' ); ?>
    
    			<div class="entry-byline">
    				<span <?php hybrid_attr( 'entry-author' ); ?>><?php the_author_posts_link(); ?></span>
    				<time <?php hybrid_attr( 'entry-published' ); ?>><?php echo get_the_date(); ?></time>
    				<?php comments_popup_link( number_format_i18n( 0 ), number_format_i18n( 1 ), '%', 'comments-link', '' ); ?>
    				<?php edit_post_link(); ?>
    			</div><!-- .entry-byline -->
    
    		</header><!-- .entry-header -->
    
    		<div <?php hybrid_attr( 'entry-summary' ); ?>>
    			<?php the_excerpt(); ?>
    		</div><!-- .entry-summary -->
    
    	<?php endif; // End single post check. ?>
    
    </article><!-- .entry -->
  • I just took a quick look at the theme, since for a change I could actually look at a themes code. The answer is, no, the theme does not use standard WP functions to include template files. What this means is that the standard global variables (like $post) are not available.

    I think you can correct this by simply declaring the global variables that you need to access, in this case $post

    
    <?php if(have_rows('otros_archivos')):
    	global $post; // add this
    	while (have_rows('otros_archivos')) : the_row();
    	$post_object = get_sub_field('antecedente');
    		echo "<ol>";
    		if($post_object): 
    			$post = $post_object; setup_postdata($post); ?>
    			<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    		<?php wp_reset_postdata();
    		else:
    			echo "<li>No se ha incluido ningún antecedente aún.</li><br/>";
    		endif;
    	echo "</ol>";
    	endwhile;
    endif; ?>
    
Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘My code doesn't work’ is closed to new replies.