3

我刚刚更新到 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;
?>

该帖子包含一个 [gallery] 短代码。我已经使用以下代码构建了自己的 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 中是否有我忽略的变化?

4

2 回答 2

2

我想通了:如果您使用已上传到媒体库的带有图像的画廊,画廊简码看起来像[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]);
于 2013-01-14T10:46:22.027 回答
1

现在可以使用 和 拉出所有画廊甚至单个画廊。每个图库将包含图像 ID 列表以及图像 url列表。图库对象基本上是简码参数,因此您也可以访问这些参数。$post->IDget_post_galleries()idssrc

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' ];
    }
}
于 2016-02-02T02:44:00.430 回答