自定义库简码在Wordpress中的用法

时间:2018-07-17 05:12:58

标签: php wordpress

我是Wordpress的新手。 我在this thread的帮助下,在博客文章中编写了用于画廊显示的自定义简码。我遵循了Mathielo编写的代码段。

这是我修改后的代码段

add_shortcode('custom_gallery', 'custom_gallery');
function custom_gallery($atts) {
global $post;

if (isset($attr['orderby'])) {
    $attr['orderby'] = sanitize_sql_orderby($attr['orderby']);
    if (!$attr['orderby'])
        unset($attr['orderby']);
}

extract(shortcode_atts(array(
    'order' => 'ASC',
    'orderby' => 'menu_order ID',
    'id' => $post->ID,
    'itemtag' => 'dl',
    'icontag' => 'dt',
    'captiontag' => 'dd',
    'columns' => 3,
    'size' => 'thumbnail',
    'include' => '',
    'exclude' => ''
), $attr));

$id = intval($id);
if ('RAND' == $order) $orderby = 'none';

if (!empty($include)) {
    $include = preg_replace('/[^0-9,]+/', '', $include);
    $_attachments = get_posts(array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby));

    $attachments = array();
    foreach ($_attachments as $key => $val) {
        $attachments[$val->ID] = $_attachments[$key];
    }
}

if (empty($attachments)) return '';

// Here's your actual output, you may customize it to your need
$output = '<div class="blog-media mt-40 mb-40 mb-xs-30">
            <ul class="clearlist content-slider">';


// Now you loop through each attachment
foreach ($attachments as $id => $attachment) {
    // Fetch the thumbnail (or full image, it's up to you)
//      $img = wp_get_attachment_image_src($id, 'medium');
//      $img = wp_get_attachment_image_src($id, 'my-custom-image-size');
    $img = wp_get_attachment_image_src($id, 'full');

    $output .= "<li>\n";
    $output .= "<img src=\"{$img[0]}\" width=\"{$img[1]}\" height=\"{$img[2]}\" alt=\"\" />\n";
    $output .= "</li>\n";
}

$output .= "</ul>\n";
$output .= "</div>\n";

return $output;`

在我的WP编辑器中,我尝试使用以下简短代码:[custom_gallery ids="33,34,35"]。但是图像没有显示出来。我确定我的图像带有正确的ID。

在使用简码时我可能在哪里出错?

有关此问题的任何帮助将不胜感激。

N.B。我在function.php

中编写了上面的简短代码段

1 个答案:

答案 0 :(得分:0)

您的函数中有一些小错误,请改用它。

add_shortcode('custom_gallery', 'custom_gallery_fn');
function custom_gallery_fn($attr)
{
    global $post;

    if (isset($attr['orderby'])) {
        $attr['orderby'] = sanitize_sql_orderby($attr['orderby']);
        if (!$attr['orderby'])
            unset($attr['orderby']);
    }

    extract(shortcode_atts(array(
        'order' => 'ASC',
        'orderby' => 'menu_order ID',
        'id' => $post->ID,
        'itemtag' => 'dl',
        'icontag' => 'dt',
        'captiontag' => 'dd',
        'columns' => 3,
        'size' => 'thumbnail',
        'ids' => '',
        'exclude' => ''
    ), $attr));

    $id = intval($id);
    if ('RAND' == $order) $orderby = 'none';

    if (!empty($ids)) {
        $ids = preg_replace('/[^0-9,]+/', '', $ids);
        $_attachments = get_posts(array('include' => $ids, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby));

        $attachments = array();
        foreach ($_attachments as $key => $val) {
            $attachments[$val->ID] = $_attachments[$key];
        }
    }

    if (empty($attachments)) return '';

    // Here's your actual output, you may customize it to your need
    $output = '<div class="blog-media mt-40 mb-40 mb-xs-30">
                <ul class="clearlist content-slider">';

    // Now you loop through each attachment
    foreach ($attachments as $id => $attachment) {
        // Fetch the thumbnail (or full image, it's up to you)
        // $img = wp_get_attachment_image_src($id, 'medium');
        // $img = wp_get_attachment_image_src($id, 'my-custom-image-size');
        $img = wp_get_attachment_image_src($id, 'full');

        $output .= "<li>\n";
        $output .= "<img src=\"{$img[0]}\" width=\"{$img[1]}\" height=\"{$img[2]}\" alt=\"\" />\n";
        $output .= "</li>\n";
    }

    $output .= "</ul>\n";
    $output .= "</div>\n";

    return $output;
}

请注意,functions参数应为“ $ attr”,而不是“ $ atts”。还需要将“ include”更改为“ ids”。就是这样。