如何使用帖子ID显示特定帖子

时间:2017-01-16 06:02:51

标签: php html wordpress post custom-post-type

我正在尝试使用帖子ID显示两个特定帖子。但是,我无法弄清楚用什么代码来调用帖子ID。任何人都可以帮助我吗?

///////////////////////////////

//////////////////////////////

<div class="food-featured-posts">
    <div class="food-featured-posts-first">
        <?php query_posts( 'p=185'); ?>
        <div class="food-wrapper"><?php the_post_thumbnail(); ?>
            <div class="popular-sticker">Popular</div>
        </div>
        <div class="food-featured-posts-info">
            <h2><a class="post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
            <p class="post-excerpt"><?php echo get_the_excerpt(); ?></p>
            <?php get_template_part( 'share-buttons' ); ?>
            <a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
            <?php comments_popup_link ('No Comments', '1 Comment', '% Comments', 'comment-count', 'none'); ?>
        </div>
    </div>
    <div class="food-featured-posts-second">
        <?php query_posts( 'p=173'); ?>
        <div class="food-wrapper"><?php the_post_thumbnail(); ?>
            <div class="popular-sticker">Popular</div>
        </div>
        <div class="food-featured-posts-info">
            <h2><a class="post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
            <p class="post-excerpt"><?php echo get_the_excerpt(); ?></p>
            <?php get_template_part( 'share-buttons' ); ?>
            <a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
            <?php comments_popup_link ('No Comments', '1 Comment', '% Comments', 'comment-count', 'none'); ?>
        </div>
    </div>
</div>
<h1 class="latest-in-food">Latest in food</h1>

更新代码

<?php
$args = array(
    'post_type'  => 'post',
    'meta_key' => '_thumbnail_id',
    'p' => 185/173,
$query = new WP_Query($args);

<div class="food-featured-posts">
<?php while($query->have_posts()) : $query->the_post(); ?>
  <div class="food-featured-posts-first">
      <div class="food-wrapper"><?php the_post_thumbnail(); ?>
          <div class="popular-sticker">Popular</div></div>
      <div class="food-featured-posts-info">
      <h2><a class="post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
            <p class="post-excerpt"><?php echo get_the_excerpt(); ?></p>
            <?php get_template_part( 'share-buttons' ); ?>
            <a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
            <?php comments_popup_link ('No Comments', '1 Comment', '% Comments', 'comment-count', 'none'); ?>
      </div></div>

  <?php endwhile; ?>

<?php while($query->have_posts()) : $query->the_post(); ?>
  <div class="food-featured-posts-second">
      <div class="food-wrapper"><?php the_post_thumbnail(); ?>
          <div class="popular-sticker">Popular</div></div>
      <div class="food-featured-posts-info">
      <h2><a class="post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
            <p class="post-excerpt"><?php echo get_the_excerpt(); ?></p>
            <?php get_template_part( 'share-buttons' ); ?>
            <a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
            <?php comments_popup_link ('No Comments', '1 Comment', '% Comments', 'comment-count', 'none'); ?>
      </div></div>
  <?php endwhile; ?>

</div>
?>

<h1 class="latest-in-food">Latest in food</h1>

2 个答案:

答案 0 :(得分:1)

使用get_post()获取特定帖子

get_post()

示例:

<?php $postData = get_post( $id, $output, $filter ); 

echo "<pre>";
print_r($postData);
?>

https://developer.wordpress.org/reference/functions/get_post/

https://www.tipsandtricks-hq.com/query-or-show-a-specific-post-in-wordpress-php-code-example-44

答案 1 :(得分:0)

    <?php
    $args = array(
    'post_type'  => 'post',       
    'p' => 173,
    $query = new WP_Query($args);

    <div class="food-featured-posts">
    <?php while($query->have_posts()) : $query->the_post(); ?>
    <div class="food-featured-posts-first">
      <div class="food-wrapper"><?php the_post_thumbnail(); ?>
          <div class="popular-sticker">Popular</div></div>
      <div class="food-featured-posts-info">
      <h2><a class="post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
            <p class="post-excerpt"><?php echo get_the_excerpt(); ?></p>
            <?php get_template_part( 'share-buttons' ); ?>
            <a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
            <?php comments_popup_link ('No Comments', '1 Comment', '% Comments', 'comment-count', 'none'); ?>
      </div></div>

    <?php endwhile;
    wp_reset_query();

    $args1 = array(
    'post_type'  => 'post',        
    'p' => 185,
    $anotherQuery = new WP_Query($args1);
    ?>

    <?php while($anotherQuery->have_posts()) : $anotherQuery->the_post(); ?>
    <div class="food-featured-posts-second">
      <div class="food-wrapper"><?php the_post_thumbnail(); ?>
          <div class="popular-sticker">Popular</div></div>
      <div class="food-featured-posts-info">
      <h2><a class="post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
            <p class="post-excerpt"><?php echo get_the_excerpt(); ?></p>
            <?php get_template_part( 'share-buttons' ); ?>
            <a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
            <?php comments_popup_link ('No Comments', '1 Comment', '% Comments', 'comment-count', 'none'); ?>
      </div></div>
    <?php endwhile;
    wp_reset_query();
    ?>

    </div>
    ?>