WordPress.org

Ready to get started?Download WordPress

Forums

Contextual Related Posts
Are there any Filters or Action Hooks? (5 posts)

  1. sem101
    Member
    Posted 2 months ago #

    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/

  2. Ajay
    Member
    Plugin Author

    Posted 2 months ago #

    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.

  3. sem101
    Member
    Posted 1 month ago #

    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 );
  4. sem101
    Member
    Posted 1 month ago #

    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 );
  5. Ajay
    Member
    Plugin Author

    Posted 1 month ago #

    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.

Reply

You must log in to post.

About this Plugin

About this Topic