简体   繁体   中英

How to call Wordpress 3.5 gallery images so they can be re-ordered?

I have a couple of sites which use Wordpress as a CMS. Both have custom rendered galleries using the following code below.

<?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>';
    } 

?>

My problem is that Wordpress 3.5 doesn't seem to be handle previously uploaded images to posts as it used to, and though the code produces a gallery, the images cannot be reordered within the WP admin interface.

I have seen it suggested that attachments no longer work as they did and that there's another way to call the images using code like this:

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

Can someone help explain how this could be applied? The 'solution' of re-creating every gallery from scratch simply isn't an option here.

As i mentioned here , you simply have to expand your if ( $attachments ) clause:

<?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"
    }   
} 

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM