Support » Plugin: Custom Related Posts » feature request: custom field support

  • Resolved jul3

    (@jul3)


    hello,
    this plugin is (nearly) exactly what i’ve been looking for.
    but my posts use custom fields for additional information, and i would need to display them too. do you think thats possible to integrate in the plugin?
    thanks, julia

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Brecht

    (@brechtvds)

    Hi Julia,

    You can use the crp_output_list_item plugin hook to alter the output of the related posts lists. Something like this can be used:

    function crp_custom_output( $output, $post_id, $relation ) {
    // Alter $output using the $relation from this $post_id.
    return $output;
    }
    add_filter( 'crp_output_list_item', 'crp_custom_output', 10, 3 );

    Kind regards,
    Brecht

    Thread Starter jul3

    (@jul3)

    hello brecht,

    thanks for your quick reply. but i’m afraid i dont know how to use this. i assume it goes in my (child-theme) functions.php, but what part would i need to replace to make it work?

    thanks,
    julia

    Plugin Author Brecht

    (@brechtvds)

    Yes, you’d add that in your functions.php file. You’d then check the $relation variable for the ID of the related post and get the custom fields you need. Whatever you return will be shown on the screen.

    Brecht

    Thread Starter jul3

    (@jul3)

    hello brecht,

    my php abilities are just “copy, paste & alter”, but not write anything myself. i searched the wp codex section, but still am not any wiser.

    so i still don’t know how to:
    “check the $relation variable for the ID of the related post and get the custom fields you need”.

    so this seems not to be a plugin-related question, just my lack of understanding of php. i understand that you can’t give support for that.

    but as i want to learn: if it would be easy for you to give me a hint, or a code example, i wouldn’t mind 😉

    and: “Whatever you return will be shown on the screen.” that would work with both, shortcode or widget, right?

    thanks a lot,
    julia

    • This reply was modified 3 years, 10 months ago by jul3.
    Plugin Author Brecht

    (@brechtvds)

    You could use something like this:

    function crp_custom_output( $output, $post_id, $relation ) {
    	// Alter $output using the $relation from this $post_id.
    	if ( isset( $relation['id'] ) ) {
    		$custom_field = get_post_meta( $relation['id'], 'your-custom-field', true );
    
    		if ( $custom_field ) {
    			$output = '<li><a href="' . $relation['permalink'] . '">' . $relation['title'] . ' - ' . $custom_field . '</a></li>';
    		}
    	}
    	return $output;
    }
    add_filter( 'crp_output_list_item', 'crp_custom_output', 10, 3 );

    One issue might be that the $relation[‘id’] is only set for relations created since our last update.

    This will change the output for both the shortcode and the widget.

    Thread Starter jul3

    (@jul3)

    hello brecht,
    thanks again for your quick reply! had no time to test yet, but will report back!
    julia

    Thread Starter jul3

    (@jul3)

    hello brecht,

    that works perfectly, thanks a lot!!!

    julia

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘feature request: custom field support’ is closed to new replies.