• Resolved powerj

    (@powerj)


    This filter is working fine except that its applying to all instances of the widget not just the one I want

    //*Add custom fields to Used Equipment Widgets
    add_filter( 'gsfc_after_post_content', 'wt_used_equip_custom_fields' );
    function wt_used_equip_custom_fields() {
         $year = get_field('year');
        $hours = get_field('hours');
        $price = get_field('price'); 
    
        echo "<div class='equip-descrip'><p>";
        echo $year . ", " . $hours . " hours" . "</p>";
        echo "<div class='used-price'>";
        if(get_field('price')) :
            echo '&dollar;' . number_format((get_field('price')), 0, '.', ',') . " + GST";
        endif;
        echo "<a class='button see-details' href=" . the_shortlink . ">See Details</a>";
        echo "</div>";
    }
Viewing 8 replies - 1 through 8 (of 8 total)
  • I see a couple areas that things go off the rails. For starters, I don’t see how that link could work for you. There’s not function call in there, and I don’t think you can use the_shortlink() anyway in this way.

    The other thing is that your quotes got a little messed up, which led you to an extra ‘.’ in an echo statement. When using quotes, try to use single quotes for php and double quotes for html. This should allow you to also use the double quotes for php when you need the php to evaluate something within your quotes, like a variable.

    So you should have something like:

    //*Add custom fields to Used Equipment Widgets
    add_filter( 'gsfc_after_post_content', 'wt_used_equip_custom_fields' );
    function wt_used_equip_custom_fields() {
    	$year = get_field( 'year' );
    	$hours = get_field( 'hours' );
    	$price = get_field( 'price' );
    
    	echo '<div class="equip-descrip"><p>';
    	echo $year . ', ' . $hours . ' hours</p>';
    	echo '<div class="used-price">';
    	if ( get_field( 'price' ) ) :
    	    echo '&dollar;' . number_format( ( get_field( 'price' ) ), 0, '.', ',' ) . ' + GST';
    	endif;
    //	echo '<a class="button see-details" href="' . the_shortlink . '">See Details</a>';
    	echo '<a class="button see-details" href="' . the_permalink() . '">See Details</a>';
    
    	echo '</div>';
    }

    Notice the parentheses are missing on your shortlink, but I doubt they would get you where you want to go anyway.

    Thread Starter powerj

    (@powerj)

    Hi Paul,

    Thanks for those corrections – yes it should be permalink, not shortlink (not sure what I was thinking there).

    My main problem is that the gsfc_after_post_content filter is adding the html to all instances of the Genesis Sandbox Featured Content Widget. ie. I’m using the widget for 3 different areas. Two of them are for used equipment (where I need the additional custom fields added) and the third is to display news articles. At the moment the filter is adding the custom fields to the news articles and I don’t want it there. How can I tell the filter which instances of the widget to target?

    For starters, let me say that I don’t know the plugin in question at all. I’m just looking at what you’ve presented. To have the code run only for certain types, you could have a conditional in there, I’m thinking maybe a Post Category.

    // Add custom fields to Used Equipment Widgets
    add_filter( 'gsfc_after_post_content', 'wt_used_equip_custom_fields' );
    function wt_used_equip_custom_fields() {
    	$year = get_field( 'year' );
    	$hours = get_field( 'hours' );
    	$price = get_field( 'price' );
    
    if ( is_category( 'used-equipment' ) ) {
    	echo '<div class="equip-descrip"><p>';
    	echo $year . ', ' . $hours . ' hours</p>';
    	echo '<div class="used-price">';
    	   if ( get_field( 'price' ) ) :
    	       echo '$' . number_format( ( get_field( 'price' ) ), 0, '.', ',' ) . ' + GST';
    	   endif;
    	echo '</div>';
    	echo '<a class="button see-details" href="' . the_permalink() . '">See Details</a>';
    	echo '</div>';
       }
    }

    Also looks like we might have missed a closing </div> tag.

    Thread Starter powerj

    (@powerj)

    No that doesn’t work. Its custom post type ‘used’ so I think it would be something more like this, but it doesn’t work either

    // Add custom fields to Used Equipment Widgets
    add_filter( 'gsfc_after_post_content', 'wt_used_equip_custom_fields' );
    function wt_used_equip_custom_fields() {
    	$year = get_field( 'year' );
    	$hours = get_field( 'hours' );
    	$price = get_field( 'price' );
    
    if ( is_singular( 'used' ) ) {
    	echo '<div class="equip-descrip"><p>';
    	echo $year . ', ' . $hours . ' hours</p>';
    	echo '<div class="used-price">';
    	   if ( get_field( 'price' ) ) :
    	       echo '$' . number_format( ( get_field( 'price' ) ), 0, '.', ',' ) . ' + GST';
    	   endif;
    	echo '</div>';
    	echo '<a class="button see-details" href="' . the_permalink() . '">See Details</a>';
    	echo '</div>';
       }
    }

    OK, it sounds like what you’re filtering is the query more than the display, and for Custom Post Types, try this on for size.

    Thread Starter powerj

    (@powerj)

    OK I have it working – the plugin is based on the Genesis Featured Widget Amplified so I looked through their documentation – found this good article

    One small thing I still can’t get is there is 2 instances of the widget ‘used-equipment’ and ‘used-equipment-for-sale’ but I can’t seem to add an OR || to the IF statement.

    // Add custom fields to Used Equipment Widgets
    add_action( 'gsfc_after_post_content', 'wt_used_equip_custom_fields' );
    function wt_used_equip_custom_fields($instance) {
    	$year = get_field( 'year' );
    	$hours = get_field( 'hours' );
    	$price = get_field( 'price' );
    
    if ( $instance['custom_field'] == 'used-equipment') {
    	echo '<div class="equip-descrip"><p>';
    	echo $year . ', ' . $hours . ' hours</p>';
    	echo '<div class="used-price">';
    	   if ( get_field( 'price' ) ) :
    	       echo '$' . number_format( ( get_field( 'price' ) ), 0, '.', ',' ) . ' + GST';
    	   endif;
    	echo '</div>';
    	echo '<a class="button see-details" href="' . the_permalink() . '">See Details</a>';
    	echo '</div>';
        }
    }

    Try this:

    // Add custom fields to Used Equipment Widgets
    add_action( 'gsfc_after_post_content', 'wt_used_equip_custom_fields' );
    function wt_used_equip_custom_fields( $instance ) {
    	$year = get_field( 'year' );
    	$hours = get_field( 'hours' );
    	$price = get_field( 'price' );
    
    	if ( 'used-equipment' === $instance['custom_field'] || 'used-equipment-for-sale' === $instance['custom_field'] ) {
    		echo '<div class="equip-descrip"><p>';
    		echo $year . ', ' . $hours . ' hours</p>';
    		echo '<div class="used-price">';
    		if ( get_field( 'price' ) ) :
    			echo '$' . number_format( ( get_field( 'price' ) ), 0, '.', ',' ) . ' + GST';
    		endif;
    		echo '</div>';
    		echo '<a class="button see-details" href="' . the_permalink() . '">See Details</a>';
    		echo '</div>';
    	}
    }

    Thread Starter powerj

    (@powerj)

    Works a treat! Thanks for all your help

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Genesis Sandbox Featured Posts – Add custom fields’ is closed to new replies.