WordPress图像Gallerys的自定义HTML,仅输出函数

时间:2016-02-04 00:34:02

标签: php wordpress

我从一个示例中获取了以下代码并进行了调整,因此wordpress中的标准库将作为Flexslider和Carousel输出。我可以输出其中一个就好了,但我也为Carousel添加了额外的*输出,现在只打印出Carousel。任何有关如何将整个事情输出的帮助将不胜感激

add_filter('post_gallery', 'my_post_gallery', 10, 2);
function my_post_gallery($output, $attr) {
global $post;

if (isset($attr['orderby'])) {
    $attr['orderby'] = sanitize_sql_orderby($attr['orderby']);
    if (!$attr['orderby'])
        unset($attr['orderby']);
}

extract(shortcode_atts(array(
    'order' => 'ASC',
    'orderby' => 'menu_order ID',
    'id' => $post->ID,
    'itemtag' => 'dl',
    'icontag' => 'dt',
    'captiontag' => 'dd',
    'columns' => 3,
    'size' => 'thumbnail',
    'include' => '',
    'exclude' => ''
), $attr));

$id = intval($id);
if ('RAND' == $order) $orderby = 'none';

if (!empty($include)) {
    $include = preg_replace('/[^0-9,]+/', '', $include);
    $_attachments = get_posts(array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby));

    $attachments = array();
    foreach ($_attachments as $key => $val) {
        $attachments[$val->ID] = $_attachments[$key];
    }
}

if (empty($attachments)) return '';

// Here's your actual output, you may customize it to your need
$output = "<div class=\"wordpress-gallery\">\n";
$output = "<div id=\"sliding\" class=\"flexslider flexslider--post-content\">\n";
//$output .= "<div class=\"preloader\"></div>\n";
$output .= "<ul class=\"slides flexslider__slides\">\n";

// Now you loop through each attachment
foreach ($attachments as $id => $attachment) {
    // Fetch the thumbnail (or full image, it's up to you)
//      $img = wp_get_attachment_image_src($id, 'medium');
//        $imgThumbnail = wp_get_attachment_image_src($id, 'thumbnail');
        $img = wp_get_attachment_image_src($id, 'full');

    $output .= "<li class=\"slide flexslider__slide cover\">\n";
    $output .= "<img src=\"{$img[0]}\" width=\"{$img[1]}\" height=\"{$img[2]}\" alt=\"\" />\n";
    $output .= "</li>\n";
}
$output .= "</ul>\n";
$output .= "</div>\n";
$output = "<div id=\"carousel\" class=\"flexslider flexslider--post-content-carousel\">\n";
$output .= "<ul class=\"slides flexslider__slides\">\n";
foreach ($attachments as $id => $attachment) {
    $imgThumbnail = wp_get_attachment_image_src($id, 'thumbnail');

    $output .= "<li >\n";
    $output .= "<img src=\"{$imgThumbnail[0]}\" alt=\"\" />\n";
    $output .= "</li>\n";
}
$output .= "</ul>\n";
$output .= "</div>\n";
$output .= "</div>\n";



return $output;

}

到目前为止输出的html(它不会输出#sliding div,只输出#carousel):

<div id="carousel" class="flexslider flexslider--post-content-carousel">
    <div class="flex-viewport" style="overflow: hidden; position: relative;">
        <ul class="slides flexslider__slides" style="width: 1400%; transition-duration: 0s; transform: translate3d(0px, 0px, 0px);">
            <li class="flex-active-slide" style="width: 210px; float: left; display: block;">
                <img src="//localhost:3002/test-site/wp-content/uploads/2016/01/test-image-300x300.jpg" alt="" draggable="false">
            </li>
            <li style="width: 210px; float: left; display: block;">
                <img src="//localhost:3002/test-site/wp-content/uploads/2016/01/test-image-1-300x300.jpg" alt="" draggable="false">
            </li>
            <li style="width: 210px; float: left; display: block;">
                <img src="//localhost:3002/test-site/wp-content/uploads/2016/01/test-image-2-300x300.jpg" alt="" draggable="false">
            </li>
            <li style="width: 210px; float: left; display: block;">
                <img src="//localhost:3002/test-site/wp-content/uploads/2016/01/test-image-3-300x300.jpg" alt="" draggable="false">
            </li>
            <li style="width: 210px; float: left; display: block;">
                <img src="//localhost:3002/test-site/wp-content/uploads/2016/01/test-image-4-300x300.jpg" alt="" draggable="false">
            </li>
            <li style="width: 210px; float: left; display: block;">
                <img src="//localhost:3002/test-site/wp-content/uploads/2016/01/test-image-5-300x300.jpg" alt="" draggable="false">
            </li>
            <li style="width: 210px; float: left; display: block;">
                <img src="//localhost:3002/test-site/wp-content/uploads/2016/01/test-image-6-300x300.jpg" alt="" draggable="false">
            </li>
        </ul>
    </div>
    <ul class="flex-direction-nav">
        <li class="flex-nav-prev"><a class="flex-prev flex-disabled" href="#" tabindex="-1">Previous</a></li>
        <li class="flex-nav-next"><a class="flex-next" href="#">Next</a></li>
    </ul>
</div>

2 个答案:

答案 0 :(得分:2)

我有一些错误,我可以看到。

// Here's your actual output, you may customize it to your need
$output = "<div class=\"wordpress-gallery\">\n";
$output = "<div id=\"sliding\" class=\"flexslider flexslider--post-content\">\n";

如果您希望附加数据,则需要在第二个.作业中将=等于此.=之前的句号$output。无论何时你附加但不在变量名后加.=,你基本上都会重新分配一个新值而不是添加它。

同时更改此行:

$output = "<div id=\"carousel\" class=\"flexslider flexslider--post-content-carousel\">\n";

为:

$output .= "<div id=\"carousel\" class=\"flexslider flexslider--post-content-carousel\">\n";

问题在于您基本上是使用新输出重置变量而不是将数据附加到变量。

希望这会对你有所帮助。

请务必仔细检查您的代码并仔细检查变量分配,以确保您要附加而不是重置。

答案 1 :(得分:1)

您的代码中有两个错误,分为以下几行:

Key

您正在重置$ output变量两次,因此之前写入的内容会丢失,因此您应该将它们替换为:

public static void swap(int[][] a, int i0, int j0, int i1, int j1) {
    int temp = a[i0][j0];
    a[i0][j0] = a[i1][j1];
    a[i1][j1] = temp;
  }
public static void main(String args[]){
    int max = 0, min = 0, tmpI = 0, tmpJ = 0, tmpI1 = 0, tmpJ1 = 0;
    int[][] a = {{10,9,20} , {2,10,10}};
    for (int i = 0; i < a.length; i++) {
        for (int j = 0; j < a[i].length; j++) { 
            if (min > a[i][j]) {
                min = a[i][j];
                tmpI1 = i; tmpJ1 = j;
            }
            if (max < a[i][j]) {
                max = a[i][j];
                tmpI = i; tmpJ = j;
            }
        }
    }
    System.out.println(Arrays.deepToString(a));
    swap(a,tmpI1,tmpJ1,tmpI,tmpJ);
    System.out.println(Arrays.deepToString(a));
}