0

我需要修改此代码以显示仅为用户当前所在的帖子获取自定义字段。

// this needs to be modified
<?php
global $post;
$args = array('category' => 37, 'post_type' => 'post' ); 
$postslist = get_posts( $args ); 
foreach ($postslist as $post) : setup_postdata($post); 
?> 
// end this needs to be modified

<?php if( get_post_meta($post->ID, "Title", true) ): ?>
<?php echo get_post_meta($post->ID, "Title", true); ?></p>
<?php endif; ?>

因为这将被包括在内,所以我不能只使用 get_post_meta。

非常感谢!

4

1 回答 1

1

这会忽略循环,只显示当前页面的自定义字段,注意 get_the_ID():

<?php if( get_post_meta(get_the_ID(), "Title", true) ): ?>
    <p><?php echo get_post_meta(get_the_ID(), "Title", true); ?></p>
<?php endif; ?>

<?php
global $post;
$args = array('category' => 37, 'post_type' => 'post' ); 
$postslist = get_posts( $args ); 
foreach ($postslist as $post){
    setup_postdata($post);
}
?> 

这将从循环中输出所有名为“Title”的自定义字段:

<?php
global $post;
$args = array('category' => 37, 'post_type' => 'post' ); 
$postslist = get_posts( $args ); 
foreach ($postslist as $post){
    setup_postdata($post);

    echo('<p>'.get_post_meta($post->ID, "Title", true).'</p>');
}
?>
于 2013-01-09T00:42:47.180 回答