页面显示短代码未输出

时间:2020-10-19 作者:Muhammad Muazzam

在主题文件夹中的自定义文件中编写以下代码,并通过functions.php 但在第页[product_first_img] 显示短代码文本,而不是输出。

function gal_first_shortcode($atts, $content = null)
{

    extract(shortcode_atts(array(
        \'size\' => \'\'
    ), $atts));

    $image_size = \'medium\';
    if ($size = !\'\') {
        $image_size = $size;
    }

    $images = get_children(
        array(
            \'post_parent\' => get_the_ID(),
            \'post_type\' => \'attachment\',
            \'numberposts\' => 1,
            \'post_mime_type\' => \'image\',
            \'orderby\' => \'menu_order\',
        )
    );

    if ($images) {
        $gallery = \'\';
        foreach ($images as $image) {
            $gallery .= wp_get_attachment_image($image->ID, $image_size);
        }
        $gallery .= \'\';
        return $gallery;
    }
}

add_shortcode(\'product_first_img\', \'gal_first_shortcode\');

1 个回复
SO网友:Saurabh Raut

我在get\\u children()和foreach循环中更改了数组,使用下面的引用url和代码工作正常。请检查您传递的参数,并像示例中所示那样使用echo语句。

参考url:https://developer.wordpress.org/reference/functions/get_children/

让我知道结果或错误

$args = array(
    \'posts_per_page\' => 1,
    \'order\'          => \'ASC\',
    \'post_mime_type\' => \'image\',
    \'post_parent\'    => $post_id,
    \'post_status\'    => null,
    \'post_type\'      => \'attachment\',
);

$attachments = get_children( $args );

if ( $attachments ) {
    foreach ( $attachments as $attachment ) {
        $image_attributes = wp_get_attachment_image_src( $attachment->ID, \'thumbnail\' ) ? wp_get_attachment_image_src( $attachment->ID, \'thumbnail\' ) : wp_get_attachment_image_src( $attachment->ID, \'full\' );
        echo \'<img src="\' . esc_url( wp_get_attachment_thumb_url( $attachment->ID ) ) . \'" class="current" />\';
    }
}

相关推荐

If statement shortcode

因此,我创建了一个短代码来返回一些ACF内容。以下代码起作用:function howitworks() { $page = get_page_by_title(\'shop\'); if (have_rows(\'steps\', $page->ID)) : while (have_rows(\'steps\', $page->ID)) : the_row(); $image = get_sub_fiel