我基本上是以某种方式创建一个循环,以便根据它的ID显示单个自定义帖子类型的内容。
所以,我想从ID为3788的自定义帖子类型中获取内容。
此处还有一个功能可以获取精选图片网址。
例如,这是我目前的代码:
<?php $args = array( 'post_type' => 'about', 'posts_per_page' => 1 ); ?>
<?php $loop = new WP_Query( $args ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php global $post; ?>
<?php
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 5600,1000 ), false, '' ); ?>
<div class="section" style="background: url(<?php echo $src[0]; ?>) no-repeat center center fixed; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover; z-index:-1;">
<?php the_content () ?>
<?php endwhile; ?>
答案 0 :(得分:5)
<?php
$ID = 3788;
$args = array('p' => $ID, 'post_type' => 'about');
$loop = new WP_Query($args);
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php global $post; ?>
<?php
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 5600,1000 ), false, '' ); ?>
<div class="section" style="background: url(<?php echo $src[0]; ?>) no-repeat center center fixed; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover; z-index:-1;">
<?php the_content () ?>
<?php endwhile; ?>
答案 1 :(得分:0)
<?php
$ids= array(3788); // for example, You can also pass multiple IDs in array
$args = array('post_type' => 'about','post__in' => $ids);
$loop = get_posts($args);
while ( $loop->have_posts() ) : $loop->the_post();
global $post;
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array(
5600,1000 ), false, '' ); ?>
<div class="section" style="background: url(<?php echo $src[0]; ?>) no-
repeat center center fixed; -webkit-background-size: cover; -moz-background-
size: cover; -o-background-size: cover; background-size: cover; z-index:-1;">
<?php the_content () ?>
<?php endwhile; ?>
答案 2 :(得分:0)
万一其他人偶然发现了这个问题并想要一种简单的方法来实现,get_post_field是您要寻找的功能。假设您有一个ID为420的帖子。如果您只是想获取该帖子的帖子内容,无论您是否处于循环状态,都可以这样做:
<?php echo get_post_field( 'post_content', 420); ?>
在某些情况下,我喜欢这样做,因为它只是一行代码。您可以对任何fields that are included in a post object( post_title,post_excerpt,post_type,post_author,post_parent等)执行此操作。
当然,在您的特定情况下,您本可以完成
<?php echo get_the_content( $post->ID ); ?>
但是对于那些记忆力很差的人,他们忘记了诸如get_the_content(),get_the_title(),get_the_author(),get_the_modified_date()等所有功能的话,您将拥有一个可以完成所有任务的衬里。
答案 3 :(得分:0)
$args = array(
'p' => $id_post,
'post_type' => 'about',
'post_status' => 'any, auto-draft','posts_per_page' => -1);
$posts = get_posts($args);
var_dump($posts);
如果帖子在草稿或垃圾箱中,您将看不到它们,因此请尝试将 post_status 添加到您的查询参数中...我花了一段时间才注意到