WordPress的3.5:自己的画廊与包括图像不起作用(Wordpress 3.5: own ga

2019-07-18 02:28发布

我刚刚更新WordPress的3.5,但是这坠毁我的代码一小部分:有一个php文件,它加载特定职位通过AJAX的画廊。

代码如下:

<?php

// Include WordPress
define('WP_USE_THEMES', false);
require('../../../../wp-load.php');

$id = $_POST['id'];

// query post with this identifier
query_posts('meta_key=identifier&meta_value='.$id); 
if (have_posts()) :
while (have_posts()) : the_post();

    // add content
    $content = apply_filters('the_content', get_the_content()); 
    echo '<div class="content-inner">'.$content.'</div>';
endwhile;
endif;
?>

该帖子包含一个[图库]简码。 我已经建立自己的WordPress画廊与此代码:

remove_shortcode('gallery');
add_shortcode('gallery', 'parse_gallery_shortcode');

function parse_gallery_shortcode($atts) {

    global $post;

    extract(shortcode_atts(array(
        'orderby' => 'menu_order ASC, ID ASC',
        'id' => $post->ID,
        'itemtag' => 'dl',
        'icontag' => 'dt',
        'captiontag' => 'dd',
        'columns' => 3,
        'size' => 'full',
        'link' => 'file'
    ), $atts));

    $args = array(
        'post_type' => 'attachment',
        'post_parent' => $id,
        'numberposts' => -1,
        'orderby' => $orderby
        ); 

    $images = get_posts($args);
    print_r($images);
}

这适用于我的网站上的所有其他画廊,但与AJAX加载的。 它已经与WordPress 3.4的工作。

在WordPress的3.5有变化,我已经忽略了?

Answer 1:

我想通了:如果您使用的图像,这已上传到媒体库画廊,画廊简码的样子[gallery ids=1,2,3]是什么意思,是图像仅链接(和不附)到库,所以post_type=attachment不能正常工作。

现在,我使用正则表达式来获得图像的ID:

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


Answer 2:

它现在可以拉动所有的画廊或者甚至一个画廊使用$post->IDget_post_galleries() 每家画廊将包含在图像ID列表对应ids ,以及该列表图像的URL在src 。 画廊对象基本上是短码参数,以便您可以访问那些为好。

if ( $galleries = get_post_galleries( $post->ID, false ) ) {

    $defaults = array (
        'orderby'    => 'menu_order ASC, ID ASC',
        'id'         => $post->ID,
        'itemtag'    => 'dl',
        'icontag'    => 'dt',
        'captiontag' => 'dd',
        'columns'    => 3,
        'size'       => 'full',
        'link'       => 'file',
        'ids'        => "",
        'src'        => array (),
    );

    foreach ( $galleries as $gallery ) {

        // defaults
        $args = wp_parse_args( $gallery, $defaults );

        // image ids
        $args[ 'ids' ] = explode( ',', $args[ 'ids' ] );

        // image urls
        $images = $args[ 'src' ];
    }
}


文章来源: Wordpress 3.5: own gallery with included images doesn't work