显示循环的所有值而不是要发送的特定值的数组

时间:2021-01-24 作者:Doug Higson

我正在尝试制作一个插件,允许用户添加定制的仪表板小部件,这些小部件具有单独的提要。我正在使用高级自定义字段和自定义Post类型UI插件。用户输入feed名称(feed\\u name)和feed的URL(feed\\u URL)当我运行代码时,用户可以创建任意多个仪表板小部件,但所有的feed都返回到所有小部件中(feed\\u名称工作正常),所以我希望每个小部件都只有来自相应feed\\u URL的feed。

 /** START The News Feed Dashboard Widget */

add_action( \'wp_dashboard_setup\', \'dfdw_feed_dashboard_add_widgets\' );


function dfdw_feed_dashboard_add_widgets(){

  $args = array( \'post_type\' => \'dashboard_feed\',
                  \'numberposts\' => \'-1\'
                );

  $loop = new WP_Query( $args );
  while ( $loop->have_posts() ) : $loop->the_post();
      the_ID();
  

 $feed_name = get_field(\'feed_name\' );

    wp_add_dashboard_widget( $feed_name, __( $feed_name ), \'my_cool_widget\' );

  endwhile;
 
  }


  function my_cool_widget() {

    $args = array( \'post_type\' => \'dashboard_feed\',
    \'numberposts\' => \'-1\'
  );

$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();

      
  
 $feed_url = get_field(\'feed_url\' );  

  $feed = array(
        array(
            \'url\'          => $feed_url,
            \'items\'        =>1,
            \'show_summary\' => 1,
            \'show_author\'  => 0,
            \'show_date\'    => 1,
          

        ),

    );
  
   
  ob_start(); // start output buffering
    wp_dashboard_primary_output( $feed_name, $feed );
    $buffer = ob_get_clean(); // get the buffer without printing the content

    // add the target attribute to the a-tag:
    $result = str_replace("<a class=\'rsswidget\'",
                          "<a class=\'rsswidget\' target=\'_blank\'", $buffer);
    echo $result;
 
  endwhile;
};

1 个回复
最合适的回答,由SO网友:Doug Higson 整理而成

Figured it out:

/** START The News Feed Dashboard Widget */

add_action(\'wp_dashboard_setup\', \'dfdw_feed_dashboard_add_widgets\');

function dfdw_feed_dashboard_add_widgets()
{

    $args = array(\'post_type\' => \'dashboard_feed\',
        \'numberposts\' => \'-1\',
    );

    $loop = new WP_Query($args);
    while ($loop->have_posts()): $loop->the_post();
        $feed_id = get_post()->ID;

        $feed_name = get_field(\'feed_name\');

        wp_add_dashboard_widget($feed_name, $feed_name, \'my_cool_widget\', null, $feed_id);

    endwhile;

}

function my_cool_widget($post, $callback_args)
{
  $post_id = $callback_args[\'args\'];
  $feed_url = get_field(\'feed_url\', $post_id);
  $feed_name = get_field(\'feed_name\', $post_id);

  $feed = array(
      array(
          \'url\' => $feed_url,
          \'items\' => 5,
          \'show_summary\' => 1,
          \'show_author\' => 0,
          \'show_date\' => 1,

      ),

  );
  ob_start(); // start output buffering
  wp_dashboard_primary_output($feed_name, $feed);
  $buffer = ob_get_clean(); // get the buffer without printing the content

  // add the target attribute to the a-tag:
  $result = str_replace("<a class=\'rsswidget\'",
      "<a class=\'rsswidget\' target=\'_blank\'", $buffer);
  echo $result;
}