• Resolved sofielyr

    (@sofielyr)


    I am trying to call custom fields to my excerpt for each post but when I change the code in the functions.php for excerpts my site breaks.

    I currently have:

    function excerpt($limit) {
      $excerpt = explode(' ', get_the_excerpt(), $limit);
      if (count($excerpt)>=$limit) {
        array_pop($excerpt);
        $excerpt = implode(" ",$excerpt);
      } else {
        $excerpt = implode(" ",$excerpt);
      }
      $excerpt = preg_replace('<code>[[^]]*]</code>','',$excerpt);
      return $excerpt;
    }

    I want to call three custom fields that I have set up in my posts but when I try to add code the site breaks…

    Any ideas?

Viewing 1 replies (of 1 total)
  • Thread Starter sofielyr

    (@sofielyr)

    I figured out how to add one custom field… It looks like this in functions.php:

    function custom_field_excerpt() {
    
    	global $post;
    
    	$text = get_field('price');
    
    	if ( '' != $text ) {
    
    		$text = strip_shortcodes( $text );
    
    		$text = apply_filters('the_content', $text);
    
    		$text = str_replace(']]>', ']]>', $text);
    
    		$excerpt_length = 20; // 20 words
    
    		$excerpt_more = apply_filters('excerpt_more', ' ' . '[Hello...]');
    
    		$text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
    
    	}
    
    	return apply_filters('the_excerpt', $text);
    
    }

    And I added a div in the index.php

    <div class="customfieldsim"><span><?php echo custom_field_excerpt(); ?></span></div>

    Now my problem is the following…

    I have added a backround picture to my div but I would prefer to add a clickable picture that shows up on the same line before the custom field that I have called. Basically my first custom field is “price” and the value for my custom field is “low price” but I want an image in front of it that I can click on that will lead me to to the correct post…

    Then…

    I want to add several custom fields below the price field.. How do I do that?

Viewing 1 replies (of 1 total)
  • The topic ‘Add custom fields to excerpt’ is closed to new replies.