【问题标题】:Wordpress 3.5: own gallery with included images doesn't workWordpress 3.5:包含图片的自己的画廊不起作用
【发布时间】:2013-01-11 12:01:00
【问题描述】:

我刚刚更新到 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 中是否有我忽略的变化?

【问题讨论】:

    标签: wordpress wordpress-3.5


    【解决方案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]);
    

    【讨论】:

      【解决方案2】:

      现在可以使用$post-&gt;IDget_post_galleries() 提取所有galleries 甚至单个gallery。每个图库将包含ids 中的图像ID 列表以及src 中的image urls 列表。图库对象基本上是简码参数,因此您也可以访问这些参数。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-12-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-29
        相关资源
        最近更新 更多