programing

Wordpress 기본 갤러리 출력 변경

mailnote 2023. 4. 6. 21:53
반응형

Wordpress 기본 갤러리 출력 변경

Wordpress 갤러리 바로 가기를 사용하려고 하는데 출력을 Foundation Orbit 플러그인에 연결하고 싶습니다(슬라이더를 만들기 위해).출력하는 HTML은 다음과 같습니다.

<div class="slideshow-wrapper">
    <div class="preloader"></div>
    <ul data-orbit>
        <li>
            <img src="img1.png" alt="bla bla bla" />
        </li>
        <li>
            <img src="img2.png" alt="bla bla bla" />
        </li>
        <li>
            <img src="img3.png" alt="bla bla bla" />
        </li>
        <li>
            <img src="img4.png" alt="bla bla bla" />
        </li>
    </ul>
</div>

뭐 좀 넣을 수 있을까요?functions.php(혹은 비슷한 것)을 얻을 수 있을까요?

그렇네.꽤 오래전에 이 코드를 발견하고 그 후로 계속 사용하고 있습니다.WP의 디폴트 갤러리를 원하는 대로 커스터마이즈할 수 있어 좋습니다.

에 대한 필터가 있습니다.post_gallery이를 사용하여 모든 기본 WP 갤러리를 맞춤화할 수 있습니다.다음은 당신이 제공한 템플릿에 맞게 수정한 코드 샘플입니다.나는 가능한 한 그것을 정리했다.

이 함수의 첫 번째 부분은 거의 갤러리 첨부 파일 처리이기 때문에 갤러리 템플릿의 출력을 결정하는 후반부를 변경할 수 있습니다(댓글을 따릅니다).

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=\"slideshow-wrapper\">\n";
    $output .= "<div class=\"preloader\"></div>\n";
    $output .= "<ul data-orbit>\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');
//      $img = wp_get_attachment_image_src($id, 'my-custom-image-size');
        $img = wp_get_attachment_image_src($id, 'full');

        $output .= "<li>\n";
        $output .= "<img src=\"{$img[0]}\" width=\"{$img[1]}\" height=\"{$img[2]}\" alt=\"\" />\n";
        $output .= "</li>\n";
    }

    $output .= "</ul>\n";
    $output .= "</div>\n";

    return $output;
}

그냥 붙여주세요.functions.php파일링 및 수정을 통해 필요에 맞게 변경할 수 있습니다.지금까지 잘 되어 왔기 때문에, 당신에게도 도움이 될 것이라고 생각합니다.

매티엘로 잘 대답했어

다만, 캡션을 포함한 옵션이 필요했기 때문에, 필요한 데이터를 얻을 수 있을 것 같아서, wp_prepare_attachment_for_js() 함수를 사용하도록 코드를 변경했습니다.

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=\"slideshow-wrapper\">\n";
$output .= "<div class=\"preloader\"></div>\n";
$output .= "<ul data-orbit>\n";

// Now you loop through each attachment
foreach ($attachments as $id => $attachment) {
    // Fetch all data related to attachment 
    $img = wp_prepare_attachment_for_js($id);

    // If you want a different size change 'large' to eg. 'medium'
    $url = $img['sizes']['large']['url'];
    $height = $img['sizes']['large']['height'];
    $width = $img['sizes']['large']['width'];
    $alt = $img['alt'];

    // Store the caption
    $caption = $img['caption'];

    $output .= "<li>\n";
    $output .= "<img src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"{$alt}\" />\n";

    // Output the caption if it exists
    if ($caption) { 
        $output .= "<div class=\"orbit-caption\">{$caption}</div>\n";
    }
    $output .= "</li>\n";
}

$output .= "</ul>\n";
$output .= "</div>\n";

return $output;
}

원래의 질문에 답한 것은 알고 있습니다만, 필터의 스니펫에 대해서, 다른 사람에게 도움이 될 수 있도록 하고 싶다고 생각하고 있습니다.Miro Mannino의 'Justified Gallery' jquery 플러그인 http://miromannino.com/projects/justified-gallery/을 Wordpress 3.9의 Wordpress 갤러리와 함께 사용할 수 있도록 했습니다.여기 제가 변경한 코드가 있습니다.(img size light 썸네일은 이미지 치수를 유지하면서 페이지 로딩 시간을 줄이기 위한 커스텀 섬네일입니다.)

// Custom Gallery

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

static $instance = 0;
$instance++;

// We're trusting author input, so let's at least make sure it looks like a valid orderby statement
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'       => 'light-thumb',
    '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];
    }
} elseif ( !empty($exclude) ) {
    $exclude = preg_replace( '/[^0-9,]+/', '', $exclude );
    $attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
} else {
    $attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
}

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

if ( is_feed() ) {
    $output = "\n";
    foreach ( $attachments as $att_id => $attachment )
        $output .= wp_get_attachment_link($att_id, $size, true) . "\n";
    return $output;
}

$itemtag = tag_escape($itemtag);
$captiontag = tag_escape($captiontag);
$columns = intval($columns);
$itemwidth = $columns > 0 ? floor(100/$columns) : 100;
$float = is_rtl() ? 'right' : 'left';

$selector = "gallery-{$instance}";

$output = apply_filters('gallery_style', "
    <style type='text/css'>
        #{$selector} {
            margin: auto;
        }
        #{$selector} .gallery-item {
            float: {$float};
            margin-top: 0px;
            text-align: center;
            width: {$itemwidth}%;           }
        #{$selector} img {
            border: 0;
        }
        #{$selector} .gallery-caption {
            margin-left: 0;
        }
    </style>
    <!-- see gallery_shortcode() in wp-includes/media.php -->


    <div id='$selector' class='gallery galleryid-{$id}'>");
    $output = "<div id=\"mygallery\">\n";




$i = 0;
foreach ( $attachments as $id => $attachment ) {
    $link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);


    $output .= "<{$itemtag} class='gallery-item'>";
    $output .= "
        <{$icontag} class='gallery-icon'>
            $link
        </{$icontag}>";
    if ( $captiontag && trim($attachment->post_excerpt) ) {
        $output .= "
            <{$captiontag} class='gallery-caption'>
            " . wptexturize($attachment->post_excerpt) . "
            </{$captiontag}>";
    }
    $output .= "</{$itemtag}>";
    if ( $columns > 0 && ++$i % $columns == 0 )
        $output .= '<br style="clear: both" />';
}

$output .= "
    <br style='clear: both;' />
    </div>\n";


return $output;

}

그것은 효과가 있다.필터를 공유해 주셔서 감사합니다.그것은 바로 제가 찾고 있던 것입니다.

따라서 img title 또는 img description과 같은 다른 문자열을 출력하려면 이 구조를 사용하십시오.

$140 = $img['140'];

Super answer Mathielo (두 번째 답변)와 zubr foundation뿐만 아니라 이 범용 솔루션에 대한 코멘트입니다.

작은 쪽지!관리 영역에서 비활성화되지 않은 경우 이 필터로 인해 관리 영역에서 갤러리가 표시되지 않습니다.이를 피하기 위해 if 문 안에서 필터 주요 부분을 실행할 수 있습니다.

add_filter('post_gallery', 'my_post_gallery', 10, 2);
function my_post_gallery($output, $attr)
{
    // Disable function in admina area. 
    if (is_admin()) {
        return;
    } else {
      // put main code in here
    }
}

언급URL : https://stackoverflow.com/questions/19802157/change-wordpress-default-gallery-output

반응형