如何在wordpress插件的选项页中获取整个博客的图片


How to get images of whole blog in option page of wordpress plugin?

我必须在插件的选项页面中获取博客的所有图片来源,我正在制作一个插件选项页面,如下所示。。请看一下。。

public function add_plugin_page(){  
                add_options_page('Settings Admin', 'Plugin_options', 'manage_options', 'test-setting-admin', array($this, 'create_admin_page'));
            }

以下代码用于我的插件选项页面

public function create_admin_page(){
        ?>
    <div class="wrap">
    <?php screen_icon(); ?>
    <form action="options.php" method="post" id="<?php echo $plugin_id; ?>_options_form" name="<?php echo $plugin_id; ?>_options_form">
    <?php settings_fields($plugin_id.'_options'); ?>
    <h2>kk Plugin Options &raquo; Settings</h2>
   <table border="1" bordercolor="#FFCC00" style="background-color:#FFFFCC; margin-top:22px"  width="25%" cellpadding="2" cellspacing="1">
       <tbody>
       <tr>
        <td style="padding-left:8px;font-family:Verdana, Geneva, sans-serif;color:#666;"><h3>Blog Id:</h3></td>
        <td><p><?php echo$abc?></p></td>
    </tr>
    <tr>
        <td style="padding-left:8px;font-family:Verdana, Geneva, sans-serif;color:#666;"><h3>API Key:</h3></td>
        <td><input type="text" name="kkpo_quote" value="<?php echo get_option('kkpo_quote'); ?>" /></td>
    </tr>
        </tbody>
</table>
   <div id="mainCHImage" style="position:relative;height:'.$imgHeight.'px;width:'.$width.'px;">
         <img id="paletlyIcon" style="position:absolute;left:'.$imgWidth.'px;top:'.$imgHeight.'px;z-index:9999;cursor:pointer;background:none;border:none;" src="http://Images/favIcon.png" onclick="get_images()">'.   
        <img style="background:none;border:none;"></div>

    </form>
</div>
    <?php
    }

$abc是一个变量,我想要其中的所有图像源。。为此,我写了一个单独的函数

public function get_images(){
    if(is_single() || is_page() || is_home()||!is_admin() ){  
    global $post; 
    global $wpdb;
    $query_images = new WP_Query( $query_images_args );
    $images = array();
         foreach ( $query_images->posts as $image) {      
         $images[]= wp_get_attachment_url( $image->ID );
                 $abc=($images);
                 echo $abc;
                 }
    }

}

但是我得到错误

ReferenceError: get_images is not defined
[Break On This Error]   
get_images();

我在我的结构中添加了该功能,即

add_action('IMages_grab', array(&$this, 'get_images'));
add_filter('IMages_grab', array(&$this, 'get_images'));

我同意@ObmerkKronen的大部分回答,但同意5点。在我看来,这就是问题的核心。

是的,想要一个包含所有帖子的数组是有效的(附件是帖子类型(。10K阵列可能并没有那么大,这取决于它的使用方式。

以下示例显示一个管理通知,其中包含指向媒体库中所有图像的链接。请查阅法典,了解此处使用的函数。应该很容易将其适应插件的选项页面。

add_action( 'admin_notices', 'get_all_images_so_15985067' );
function get_all_images_so_15985067() 
{
    // get all images
    $args = array( 
        'post_type' => 'attachment', 
        'post_mime_type' => 'image',
        'numberposts' => -1,
    );
    $_attachments = get_posts( $args );
    // no images found, do nothing
    if ( empty( $_attachments ) )
        return;
    // build array only with IDs
    $attachments = array();
    foreach ( $_attachments as $key => $val ) {
        $attachments[ $val->ID ] = $_attachments[ $key ];
    }
    // print thumbnail links
    echo '<div class="updated">';
    foreach ( $attachments as $att_id => $attachment ) {
        echo wp_get_attachment_link( 
            $att_id, 
            'thumbnail', 
            false, 
            false, 
            $attachment->post_title 
        ) . "<br>";
    }
    echo '</div>';
}

老兄,你的代码在很多层面上都是错误的!(我只是在这里表达我自己的观点。(

  1. 您有$query_images = new WP_Query( $query_images_args );,但没有定义$query_images_arg(通过定义参数修复(

  2. 你正在构建某种管理菜单,但随后你设置了!is_admin()的条件,它将使所有内容无效(通过决定/定义它的操作位置来修复(

  3. 您有许多语法错误,如echo$abc?(修复为echo $abc(

    3.1.$abc应该是一个数组吗?那么你甚至不能使用echo,而是使用print_r($abc);

  4. 您正在使用settings APi,这是一个序列化数组,然后您调用单个选项,如echo get_option('kkpo_quote');(修复决定使用哪种方法(

  5. 这是最重要的一个,为什么人们会想把整个网站的所有图像都放在一个数组中,并进一步呼应它??你能想象一个拥有10000张图片,甚至只有300张图片的网站会发生什么吗?

现在,您的代码显然是cut&粘贴插件开发。给出一个好的答案意味着你必须更好地解释你的意图,但你可以从修复我在上面写的但不完整的列表开始。。。