• Hi, thank you for the great plugin. I am trying to customize without overriding your core code…

    I have a PHP conditional that outputs an icon depending on how a post is tagged, for example: if post is tagged “dog” then show dog icon; if post is tagged “cat” then show cat icon, etc.

    Is there a way I can hook into your plugin and insert my PHP function?

    Example code I would like to insert:

    <ul>
    <?php echo my_conditional(); ?><li>A post about Dogs</li>
    <?php echo my_conditional(); ?><li>A post about Cats</li>
    <?php echo my_conditional(); ?><li>A post about Birds</li>
    </ul>

    https://wordpress.org/plugins/contextual-related-posts/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Ajay

    (@ajay)

    Hi, you can filter “crp_before_list_item” to add the conditional. (Line 96)

    You’ll need a function similar to this:

    function hook_crp_before_list_item( $before_list_item, $result ) {
    
    }
    add_filter( "crp_before_list_item", "hook_crp_before_list_item", 2);

    $before_list_item is what you set in the Settings page i.e. <li> and the $result is the post object for the current post.

    Thread Starter sem101

    (@sem101)

    Thanks Ajay, I almost have it work. I tried this code quickly!

    Problems:

    1. $output displays above content
    2. how get $title ?
    3. It says “No related posts found” (see #1)

    Can you look at my code please?

    function my_function() {
      echo ':-)';
    }
    function hook_crp_before_list_item( $before_list_item, $result, $output = null ) {
      $before_list_item = my_function();
    	$output .= $before_list_item;
    	$output .= '<li><a href="' . get_permalink( $result->ID ) . '">' . $result->ID . '</a></li>';
    	echo $output;
    }
    add_filter( 'crp_before_list_item', 'hook_crp_before_list_item', 2, 2 );

    Thread Starter sem101

    (@sem101)

    I think I gots it:

    function my_function() {
      return ':-)';
    }
    function hook_crp_before_list_item( $before_list_item, $result, $output = null ) {
      $output .= my_function();
    	$output .= $before_list_item;
    	//$output .= '<li><a href="' . get_permalink( $result->ID ) . '">' . get_the_title( $result->ID ) . '</a></li>';
    	return $output;
    }
    add_filter( 'crp_before_list_item', 'hook_crp_before_list_item', 2, 2 );
    Plugin Author Ajay

    (@ajay)

    function my_function() {
      return ':-)';
    }
    function hook_crp_before_list_item( $before_list_item, $result ) {
      $output = my_function();
    	$output .= $before_list_item;
    	return $output;
    }
    add_filter( 'crp_before_list_item', 'hook_crp_before_list_item', 2, 2 );

    This should work.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Are there any Filters or Action Hooks?’ is closed to new replies.