循环通过CMB2附件的帖子

发布于 2025-02-06 13:13:29 字数 707 浏览 3 评论 0 原文

我已经设置了两种不同的自定义帖子类型 - 一种用于类别,另一种用于帖子。我已经将帖子附加到类别上的 cmb2 plugin 。我现在想在每个类别页面上显示所有附加/相关的帖子。我可以从数组中显示不同的ID,但不能显示帖子内容。

试图获取附件:

$attached_users = get_post_meta( get_the_ID(), 'pr2_cmb2_attached_posts', true );

foreach ( $attached_users as $user ) {
    $employee = get_post( $user );
}

试图显示所附帖子中的内容

<?php while ( have_posts() ) : the_post(); ?>
    <?php echo get_the_title($employee);?>
    <?php echo get_the_post_thumbnail($employee);?>
<?php endwhile; // end of the loop. ?>

I've set up two different custom post types – one for categories and one for posts. I've attached the posts to the categories with the CMB2 plugin. I now want to display all the attached/related posts on each category page. I'm able to display the different ID's from the array, but not the post content.

Trying to get attached posts:

$attached_users = get_post_meta( get_the_ID(), 'pr2_cmb2_attached_posts', true );

foreach ( $attached_users as $user ) {
    $employee = get_post( $user );
}

Trying to display the content from the attached posts

<?php while ( have_posts() ) : the_post(); ?>
    <?php echo get_the_title($employee);?>
    <?php echo get_the_post_thumbnail($employee);?>
<?php endwhile; // end of the loop. ?>

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

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

发布评论

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

评论(1

奈何桥上唱咆哮 2025-02-13 13:13:29

您使用的插件可为您提供一系列ID。
使用此ID可以使用 get_post($ id)获得获取邮政对象的可能性。

该函数为您提供一个帖子对象:

但是,如果您只想使用 get_the_title()之类的函数,则只需要发布ID,而不是整个帖子对象。

好吧,您已经获得了所需的所有数据,并且不需要获取邮政对象。
这些插件为您提供了一系列的发布ID。

// get array of post ids
$attached_users = get_post_meta( get_the_ID(), 'pr2_cmb2_attached_posts', true );

// loop through posts using post ids
foreach ( $attached_users as $user ) {
    echo get_the_title( $user );
    echo get_the_post_thumbnail( $user, 'thumbnail' );
}

The plugin you are using gives you an array of ids.
Using this ids gives you the possibility to get the post object by using get_post($id).

The function gives you a post object: https://developer.wordpress.org/reference/functions/get_post/

But if you just want to use functions like get_the_title() you only need the post id, not the whole post object.

Well, you already got all the data you need and do not need to get the post object.
The plugins gives you an array of post ids.

// get array of post ids
$attached_users = get_post_meta( get_the_ID(), 'pr2_cmb2_attached_posts', true );

// loop through posts using post ids
foreach ( $attached_users as $user ) {
    echo get_the_title( $user );
    echo get_the_post_thumbnail( $user, 'thumbnail' );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文