Support

Account

Home Forums Front-end Issues Display only field with a content + add code inside a field

Solving

Display only field with a content + add code inside a field

  • Hello, I use ACF in this page to display the content of fields.
    Actually i’m using this code to display the fields

    <img src="[acf field='immagine']" width="650" height="366" />
    
    [acf field="descrizione"]
    <strong>PROPRIETARIO:</strong> [acf field="proprietario"]
    <strong>INDIRIZZO:</strong> [acf field="indirizzo"]
    <strong>TELEFONO 1:</strong> [acf field="telefono_1"]
    <strong>TELEFONO 2:</strong> [acf field="telefono_2"]
    <strong>SITO WEB:</strong> <a href="[acf field='sito_web']" target="_blank" rel="noopener noreferrer">[acf field='sito_web']</a>
    <strong>SOCIAL NETWORK:</strong> <a href="[acf field='social_network']" target="_blank" rel="noopener noreferrer">[acf field='social_network']</a>
    <strong>EMAIL:</strong> <a href="mailto:[acf field='email']">[acf field='email']</a>
    <a href="[acf field='listino_prezzi']" target="_blank" rel="noopener noreferrer"><strong>LISTINO PREZZI</strong></a>
    <a href="[acf field='promozioni']" target="_blank" rel="noopener noreferrer"><strong>PROMOZIONI</strong></a>

    1. There is a way to display in the front-end only fields with a content and no show empty ones?

    2. There is a way to add:
    PROPRIETARIO:
    INDIRIZZO:
    TELEFONO 1:
    TELEFONO 2:
    SITO WEB:
    SOCIAL NETWORK:
    EMAIL:
    LISTINO PREZZI
    PROMOZIONI

    By default inside the fields?

    Thanks in advantage for your answer

  • Is there any particular reason why you are using shortcodes and not PHP code in the template(s)? Because that would be the easiest thing with template code instead of shortcodes.

  • I’m just not good at coding and i know a little of html.

  • There is any tutorial i can read that show how to do what i asked using php?

  • There really are not any tutorials here on basic template coding for WP.

    Once you have that figured out then showing a field only when it has content is simple.

    For example:
    <strong>PROPRIETARIO:</strong> [acf field="proprietario"]
    in PHP becomes

    
    if (get_field('proprietario')) {
      ?><strong>PROPRIETARIO:</strong> <?php the_field('proprietario'); ?><br />
    }
    

    These types of examples for fields can be found in the documentation for each field type.

    Another option would be to build your own shortcode. There are a lot of tutorials on the web for how to built custom shortcodes

    ShortcodesUltimate has a shortcode creator add on to create shortcodes without messing with theme files https://getshortcodes.com/add-ons/shortcode-creator/

  • I tried to add the code

    <!--?php
    if (get_field('proprietario')) {
      ?><strong>PROPRIETARIO:</strong> <?php the_field('proprietario'); ?><br />
    }
    ?>

    inside the wordpress post field but it didn’t work, maybe i add it in the wrong place?

  • You cannot add php code inside of the WP editor, you must alter the templates. WYSIWYG editors are not meant for coding PHP.

  • Here is a quick and basic tutorial:

    Assuming your pages are using the default page template (page.php in your theme directory on the server) and you only want the field values on certain specific pages, not everywhere:

    1. Make a copy of page.php and name it whatever you like (e. g. page-acf.php)
    2. Open the PHP file in a code editor or a plain text editor of your choice
    3. At the very top of the file, add this code to let the system know that this is a global template:
      
      <?php
      	/*
      		Template Name: Page with custom fields
      		Template Post Type: page
      	*/
      	get_header();
      …
      …
      

      (you can use whatever text you like for the template name)

    4. Add the template code for which John Huebner gave an example after the the_content() function, like so:
      
      the_content();
      if (get_field('proprietario')) {
      ?>
      <strong>PROPRIETARIO:</strong> <?php the_field('proprietario'); ?><br />
      <?php
      }
      if (get_field('indirizzo')) {
      ?>
      <strong>INDIRIZZO:</strong> <?php the_field('indirizzo'); ?><br />
      <?php
      }
      …
      …
      …
      

      and so on and so forth.

    5. In the admin area, go to the ACF field groups and edit the group that contains these fields. Change the location rules to “page template equals Page with custom fields” (or whatever you named your page template)
    6. And lastly, edit the pages where you want the fields to show up and in the sidebar under “Document”, open the “Page attributes” panel and select the template.

    That sounds like a lot but it’s just a one-time setup and then the only thing you need to do is select the appropriate page template whenever you want to show the fields.

  • Sorry for the late answer, I tried to do what you said, I created a new file (acf.php) and uploaded it but when I try to change the page template it didn’t even show up.
    This is the code of acf.php

    <?php
    	/*
    		Template Name: Page with custom fields
    		Template Post Type: ACF
    	*/
    	get_header();
    the_content();
    if (get_field('proprietario')) {
    ?>
    <strong>PROPRIETARIO:</strong> <?php the_field('proprietario'); ?><br />
    <?php
    }
    if (get_field('indirizzo')) {
    ?>
    <strong>INDIRIZZO:</strong> <?php the_field('indirizzo'); ?><br />
    ?>
    <?php
    /**
     * The template for displaying all pages.
     *
     * This is the template that displays all pages by default.
     * Please note that this is the WordPress construct of pages
     * and that other 'pages' on your WordPress site will use a
     * different template.
     *
     * @package Patus
     */
    
    get_header(); ?>
    
    	<div id="primary" class="content-area">
    		<main id="main" class="site-main" role="main">
    
    			<?php while ( have_posts() ) : the_post(); ?>
    
    				<?php get_template_part( 'template-parts/content', 'page' ); ?>
    
    				<?php
    					// If comments are open or we have at least one comment, load up the comment template
    					if ( comments_open() || get_comments_number() ) :
    						comments_template();
    					endif;
    				?>
    
    			<?php endwhile; // end of the loop. ?>
    
    		</main><!-- #main -->
    	</div><!-- #primary -->
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>
  • Yeah, you mixed something up in your template (get_header() twice, a missing closing bracket etc.). Change it to this right here:

    
    <?php
    	/*
    		Template Name: Page with custom fields
    		Template Post Type: ACF
    	*/
    
    get_header();
    ?>
    
    	<div id="primary" class="content-area">
    		<main id="main" class="site-main" role="main">
    
    			<?php while ( have_posts() ) : the_post(); ?>
    
    				<?php get_template_part( 'template-parts/content', 'page' ); ?>
    
    				<?php
    					// If comments are open or we have at least one comment, load up the comment template
    					if ( comments_open() || get_comments_number() ) :
    						comments_template();
    					endif;
    				?>
    
    			<?php
    				endwhile; // end of the loop.
    				if (get_field('proprietario')) {
    			?>
    			<strong>PROPRIETARIO:</strong> <?php the_field('proprietario'); ?><br />
    			<?php
    				}
    				if (get_field('indirizzo')) {
    			?>
    			<strong>INDIRIZZO:</strong> <?php the_field('indirizzo'); ?><br />
    			<?php
    				}
    			?>
    		</main><!-- #main -->
    	</div><!-- #primary -->
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>
    

    The file should be in your theme folder. See if you can select the template now. Here is an explanation on how to create a page template, just for your information (or to compare whether you’ve done everything correctly): https://developer.wordpress.org/themes/template-files-section/page-template-files/#creating-custom-page-templates-for-global-use

  • Hello, I copied your code, pasted and uploaded the file in the theme directory but when i try to change template it didn’t show up, I don’t know if it can help, the theme I use is Patus.
    Thanks in advantage for your answer

Viewing 13 posts - 1 through 13 (of 13 total)

The topic ‘Display only field with a content + add code inside a field’ is closed to new replies.