在wordpress meta框的前端检索photogallery图像值

发布于 2025-02-03 10:24:31 字数 2290 浏览 4 评论 0原文

摘要

我需要使用自己的html photoGallery,这样做:

  • 建议我使用metabox,所以我发现&安装 metabox.io插件
  • 接下来,我使用其网站&生成了高级图像代码。在functions.php上添加了它,以便在后端显示上传器。
  • 但是在前端,我很难检索图像的值。

functions.php (metabox.io生成的代码):

add_filter( 'rwmb_meta_boxes', 'your_prefix_register_meta_boxes' );

function your_prefix_register_meta_boxes( $meta_boxes ) {
    $prefix = '';

    $meta_boxes[] = [
        'title'   => esc_html__( 'Untitled Field Group', 'online-generator' ),
        'id'      => 'untitled',
        'context' => 'normal',
        'fields'  => [
            [
                'type'             => 'image_advanced',
                'name'             => esc_html__( 'Image Advanced', 'online-generator' ),
                'id'               => $prefix . 'image_advanced_8hswqfsoqai',
                'max_file_uploads' => 40,
            ],
        ],
    ];

    return $meta_boxes;
}

cms

这是CMS的结果。创建了一个新的图像字段。 CMS部分完成:

“在此处输入图像描述”

frontend

然后我从metabox.io&将其放置在index.php 上,但它没有打印什么

<?php
$images = rwmb_meta( $field_id, array( 'size' => 'thumbnail' ) );
foreach ( $images as $image ) {
    echo '<a href="'.$image['full_url'].'"><img src="'.$image['url'].'"></a>';
}
?>

我的问题是

如何在Frontend上显示Metabox.io插件图像的值(在我自己的HTML内部)?我需要如下:

<div class="col-lg-6 img-grids mt-lg-0 mt-5 pl-lg-4">
    <img src="<?php echo get_template_directory_uri().'/assets/images/p1.jpg'; ?>" alt="" class="img-fluid radius-image" />
    <img src="<?php echo get_template_directory_uri().'/assets/images/p2.jpg'; ?>" alt="" class="img-fluid radius-image" />
</div>

注意:我不仅在帖子内容中添加画廊,而必须添加画廊作为元盒子的主要原因,是因为我不需要图库显示,而是要显示。在我自己的HTML代码中的前端检索画廊的个体值。

Summary:

I need to use my own html photogallery so to do this:

  • I was advised to use MetaBox so I found & installed MetaBox.io plugin.
  • Next I generated an Advanced Image code using their website & added it at functions.php for it to show the uploader at Backend.
  • But at Front-end I'm having trouble to retrieve the images' values.

Functions.php (MetaBox.io generated code):

add_filter( 'rwmb_meta_boxes', 'your_prefix_register_meta_boxes' );

function your_prefix_register_meta_boxes( $meta_boxes ) {
    $prefix = '';

    $meta_boxes[] = [
        'title'   => esc_html__( 'Untitled Field Group', 'online-generator' ),
        'id'      => 'untitled',
        'context' => 'normal',
        'fields'  => [
            [
                'type'             => 'image_advanced',
                'name'             => esc_html__( 'Image Advanced', 'online-generator' ),
                'id'               => $prefix . 'image_advanced_8hswqfsoqai',
                'max_file_uploads' => 40,
            ],
        ],
    ];

    return $meta_boxes;
}

CMS:

Here's the result at CMS. A new images field is created. CMS part is done:

enter image description here

Frontend:

Then I grabbed this code from MetaBox.io & placed it at index.php but it prints nothing:

<?php
$images = rwmb_meta( $field_id, array( 'size' => 'thumbnail' ) );
foreach ( $images as $image ) {
    echo '<a href="'.$image['full_url'].'"><img src="'.$image['url'].'"></a>';
}
?>

My question is:

How to display the MetaBox.io plugin images' values at frontend (inside my own HTML)? I need them as follows:

<div class="col-lg-6 img-grids mt-lg-0 mt-5 pl-lg-4">
    <img src="<?php echo get_template_directory_uri().'/assets/images/p1.jpg'; ?>" alt="" class="img-fluid radius-image" />
    <img src="<?php echo get_template_directory_uri().'/assets/images/p2.jpg'; ?>" alt="" class="img-fluid radius-image" />
</div>

Note: the main reason I didn't simply add a gallery inside the post content but had to add a gallery as a Meta Box, is because I don't need the gallery to show as is , but to retrieve the gallery individual values at frontend inside my own html code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

狼性发作 2025-02-10 10:24:31

谢谢您的建议
因此,在这里,

$images = rwmb_meta ($field_id, array ('size' => 'thumbnail'));

我们拥有画廊收到的金额。

根据上述代码,在页面模板文件或循环中使用此代码。

<div class="col-lg-6 img-grids mt-lg-0 mt-5 pl-lg-4">

<?php

$untitled_meta = rwmb_meta('image_advanced_8hswqfsoqai' , '' , get_the_ID());

foreach ($untitled_meta as $image) {
    ?>

    <img src="<?= $image['full_url'] // Or Use url for crop size  ?>" alt="<?= $image['alt'] ?>"
         class="img-fluid radius-image"/>

    <?php
}
?>

rwmb_meta函数具有三个参数。

  1. 根据您的字段ID的第一个值
    代码结构:image_advanced_8hswqfsoqai
  2. ,然后将下一个值留空以接收全部。
  3. 最后一个参数是我在
    get_the_id()我们现在所处的页面ID。

此页面

/72446616?noredirect = 1 在页面上构建META框,然后发布

将此代码放在function.php文件中,您知道它

add_filter( 'rwmb_meta_boxes', 'compliance_students_gallery_meta_boxes' );

function compliance_students_gallery_meta_boxes( $meta_boxes ) {

    $meta_boxes[] = [
        'title'   => esc_html__( 'Untitled Field Group', 'online-generator' ),
        'id'      => 'untitled',
        'context' => 'normal',
        'post_types' => ['post', 'page'],
        'fields'  => [
            [
                'type'             => 'image_advanced',
                'name'             => esc_html__( 'Image Advanced', 'online-generator' ),
                'id'               => 'images_untitled',
                'max_file_uploads' => 40,
            ],
        ],
    ];

    return $meta_boxes;
}

可以在页面上添加一个图像库,并在页面上添加一个图像库

步骤2:在页面模板中显示包含以下模板名称的同一文件:学生当然,这仅适用于您在其他地方问相关问题的您(this page)

<div class="col-lg-6 img-grids mt-lg-0 mt-5 pl-lg-4">

    <?php

    $untitled_meta = rwmb_meta('image_advanced_8hswqfsoqai' , '' , get_the_ID());

    foreach ($untitled_meta as $image) {
        ?>

        <img src="<?= $image['full_url'] // Or Use url for crop size  ?>" alt="<?= $image['alt'] ?>"
             class="img-fluid radius-image"/>

        <?php
    }
    ?>
</div>

< em> 重要说明
如果您想在圆圈或主页或帖子内部显示,则应该是这种情况。

要显示图像量或任何其他液体,必须在WordPress查询中使用它。

用于WordPress查询循环的示例

<?php

$query = new WP_Query($args);
if ($query->have_posts()) {
    // some code here if you want.
    while ($query->have_posts()) {
        $query->the_post();
        ?>
        <div class="col-lg-6 img-grids mt-lg-0 mt-5 pl-lg-4">

            <?php

            $untitled_meta = rwmb_meta('image_advanced_8hswqfsoqai', '', get_the_ID());

            foreach ($untitled_meta as $image) {
                ?>
                <img src="<?= $image['full_url'] // Or Use url for crop size   ?>" alt="<?= $image['alt'] ?>"
                     class="img-fluid radius-image"/>
                <?php
            }
            ?>
        </div>
        <?php
    }
}
?>

thank you for following my advice
So here we are

$images = rwmb_meta ($field_id, array ('size' => 'thumbnail'));

We have the amount that our gallery receives.

According to the above codes, use this code inside the page template file or inside the loop.

<div class="col-lg-6 img-grids mt-lg-0 mt-5 pl-lg-4">

<?php

$untitled_meta = rwmb_meta('image_advanced_8hswqfsoqai' , '' , get_the_ID());

foreach ($untitled_meta as $image) {
    ?>

    <img src="<?= $image['full_url'] // Or Use url for crop size  ?>" alt="<?= $image['alt'] ?>"
         class="img-fluid radius-image"/>

    <?php
}
?>

The rwmb_meta function has three arguments.

  1. The first value of our field ID that is fielded according to your
    code structure: image_advanced_8hswqfsoqai
  2. And leave the next value blank to receive all.
  3. And the last parameter is the page ID or post that I got with the
    get_the_ID() ID of the page we are in now.

To optimize and use the page according to your previous question on this page, I write the following code and please paste it (of course, if you want to work with the page template)

Step 1: Add a function to build the meta box on the page and post

Put this code in the functions.php file as you know it

add_filter( 'rwmb_meta_boxes', 'compliance_students_gallery_meta_boxes' );

function compliance_students_gallery_meta_boxes( $meta_boxes ) {

    $meta_boxes[] = [
        'title'   => esc_html__( 'Untitled Field Group', 'online-generator' ),
        'id'      => 'untitled',
        'context' => 'normal',
        'post_types' => ['post', 'page'],
        'fields'  => [
            [
                'type'             => 'image_advanced',
                'name'             => esc_html__( 'Image Advanced', 'online-generator' ),
                'id'               => 'images_untitled',
                'max_file_uploads' => 40,
            ],
        ],
    ];

    return $meta_boxes;
}

Now you can add an image gallery to both the page and the post

Step 2: Display in the page template the same file that contains this Template Name: students Of course, this is only for you who asked the relevant question elsewhere (this page)

<div class="col-lg-6 img-grids mt-lg-0 mt-5 pl-lg-4">

    <?php

    $untitled_meta = rwmb_meta('image_advanced_8hswqfsoqai' , '' , get_the_ID());

    foreach ($untitled_meta as $image) {
        ?>

        <img src="<?= $image['full_url'] // Or Use url for crop size  ?>" alt="<?= $image['alt'] ?>"
             class="img-fluid radius-image"/>

        <?php
    }
    ?>
</div>

Important note
This should be the case if you want to show inside the circle or the main page or inside the posts.

To display the amount of image or any other liquid, you must use it in the WordPress query.

Example for use in a WordPress query loop

<?php

$query = new WP_Query($args);
if ($query->have_posts()) {
    // some code here if you want.
    while ($query->have_posts()) {
        $query->the_post();
        ?>
        <div class="col-lg-6 img-grids mt-lg-0 mt-5 pl-lg-4">

            <?php

            $untitled_meta = rwmb_meta('image_advanced_8hswqfsoqai', '', get_the_ID());

            foreach ($untitled_meta as $image) {
                ?>
                <img src="<?= $image['full_url'] // Or Use url for crop size   ?>" alt="<?= $image['alt'] ?>"
                     class="img-fluid radius-image"/>
                <?php
            }
            ?>
        </div>
        <?php
    }
}
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文