0

我有几个使用 Wordpress 作为 CMS 的网站。两者都有使用下面的代码自定义呈现的画廊。

<?php
$the_cat = get_the_category();
$category_name = $the_cat[0]->cat_name;
$category_description = $the_cat[0]->category_description;
$category_link = get_category_link( $the_cat[0]->cat_ID );
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );

echo '<div class="titleblock-2"><p>';
echo customTitle(50);
echo '</p></div>';

$attachments = get_posts(array
    (
    'post_type' => 'attachment',
    'numberposts' => -1,
    'post_status' => 'published',
    'post_parent' => $post->ID,
    'orderby' => 'menu_order',
    'order' => ''
    )
);

if ( $attachments ) {
    echo '<div id="singlegallery">';
    foreach ( $attachments as $attachment ) {
        $attachment_page = get_attachment_link( $attachment->ID ); 
        echo '<div class="big-thumb"><div class="gallery-icon"><a href="';
        echo wp_get_attachment_url( $attachment->ID );
        echo '">';

        $myimage = preg_replace( '/(width|height)=\"\d*\"\s/', "", wp_get_attachment_image($attachment->ID, medium) ); echo $myimage; 

        echo '</a></div></div>';
        }
    echo '</div>';
    } 

?>

我的问题是 Wordpress 3.5 似乎没有像以前那样处理以前上传到帖子的图像,尽管代码生成了一个画廊,但图像无法在 WP 管理界面中重新排序。

我已经看到它表明附件不再像以前那样工作,并且还有另一种方法可以使用如下代码调用图像:

$post_content = $post->post_content;
preg_match('/\[gallery.*ids=.(.*).\]/', $post_content, $ids);
$array_id = explode(",", $ids[1]);

有人可以帮助解释如何应用吗?从头开始重新创建每个画廊的“解决方案”在这里根本不是一个选择。

4

1 回答 1

0

正如我在这里提到的,您只需扩展您的if ( $attachments )子句:

<?php
$the_cat = get_the_category();
$category_name = $the_cat[0]->cat_name;
$category_description = $the_cat[0]->category_description;
$category_link = get_category_link( $the_cat[0]->cat_ID );
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );

echo '<div class="titleblock-2"><p>';
echo customTitle(50);
echo '</p></div>';

$attachments = get_posts(array
    (
    'post_type' => 'attachment',
    'numberposts' => -1,
    'post_status' => 'published',
    'post_parent' => $post->ID,
    'orderby' => 'menu_order',
    'order' => ''
    )
);

if ( $attachments ) {
    echo '<div id="singlegallery">';
    foreach ( $attachments as $attachment ) {
        $attachment_page = get_attachment_link( $attachment->ID ); 
        echo '<div class="big-thumb"><div class="gallery-icon"><a href="';
        echo wp_get_attachment_url( $attachment->ID );
        echo '">';

        $myimage = preg_replace( '/(width|height)=\"\d*\"\s/', "", wp_get_attachment_image($attachment->ID, medium) ); echo $myimage; 

        echo '</a></div></div>';
        }
    echo '</div>';
} else {   
    $post_content = $post->post_content;
    preg_match('/\[gallery.*ids=.(.*).\]/', $post_content, $ids);
    $attachment_ids = explode(",", $ids[1]);
    foreach ($attachment_ids as $attachment_id) {

        // here you can use your old code for printing images, but use "$attachment_id" instead of "$attachment->ID"
    }   
} 

?>
于 2013-01-16T06:33:48.257 回答