Support » Plugin: Telegram Bot & Channel » wp recent post

  • Resolved gnappo

    (@gnappo)


    Hi i have create the custom plugin as u suggest adding the following code:

    add_action(‘telegram_parse’,’telegramcustom_parse’, 10, 2);

    function telegramcustom_parse( $telegram_user_id, $text ) {
    $plugin_post_id = telegram_getid( $telegram_user_id );

    if ( !$plugin_post_id ) {
    return;
    }

    if ( $text == ‘/latestnews’) {

    ?>

      <?php
      $args = array( ‘numberposts’ => ‘4’ );
      $recent_posts = wp_get_recent_posts($args);
      foreach( $recent_posts as $recent ){
      echo ‘

    • ‘ . $recent[“post_title”].’
    • ‘;
      }
      wp_reset_query();

      ?>

    <?php
    telegram_sendmessage( $telegram_user_id, get_the_title($recent_posts));
    }

    return;
    }

    ?>

    but when i try on telegram he retrive only latest post not 4 latest posts, where is my mistake? is it possible to add the permalink ? or make the entire process internally in your command menu? if yes how?

    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter gnappo

    (@gnappo)

    add_action('telegram_parse','telegramcustom_parse', 10, 2);
    
    function telegramcustom_parse( $telegram_user_id, $text ) {
        $plugin_post_id = telegram_getid( $telegram_user_id );
    
        if ( !$plugin_post_id ) {
            return;
        }
    
        if ( $text == '/latestnews') {
    	    
    	   ?> 
    	   <ul><?php
    	$args = array( 'numberposts' => '4' );
    	$recent_posts = wp_get_recent_posts($args);
    	foreach( $recent_posts as $recent ){
    		echo '<li><a href="' . get_permalink($recent["ID"]) . '">' .   $recent["post_title"].'</a> </li> ';
    	}
    	wp_reset_query();
    
    ?></ul>
    <?php	    
            telegram_sendmessage( $telegram_user_id,  get_the_title($recent_posts));
        }
    
        return;
    }
    
    ?>
    Plugin Author Marco Milesi

    (@milmor)

    Hi,
    you have to save the output in a $variable or you’ll get the last entry only.

    Try this one: [NOT TESTED]

    <?php 
    
    add_action('telegram_parse','telegramcustom_parse', 10, 2);
    
    function telegramcustom_parse( $telegram_user_id, $text ) {
        $plugin_post_id = telegram_getid( $telegram_user_id );
    
        if ( !$plugin_post_id ) {
            return;
        }
    
        if ( $text == '/latestnews') {
    	
    	$args = array( 'numberposts' => '4' );
    	$recent_posts = wp_get_recent_posts($args);
    	$return_message = '';
    	
    	foreach( $recent_posts as $recent ){
    		$return_message .= '['.$recent["post_title"].']('.get_permalink($recent["ID"]).')'.PHP_EOL;
    	}
    	wp_reset_query();
        
           telegram_sendmessage( $telegram_user_id,  $return_message);
        }
    
        return;
    }
    
    ?>
    • This reply was modified 5 years, 8 months ago by Marco Milesi. Reason: formatting
    Thread Starter gnappo

    (@gnappo)

    thanks a lot

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘wp recent post’ is closed to new replies.