Wordpress 是否有任何实际的方法将博客文章中的特色图像作为背景图像拉入另一个页面模板上的div中

Wordpress 是否有任何实际的方法将博客文章中的特色图像作为背景图像拉入另一个页面模板上的div中,wordpress,Wordpress,我正在尝试向我的主页模板中添加一个博客帖子网格。我已经从4篇博文中提取了内容,现在我只需要将特色图片作为每个div的背景 这是我当前完成了一半的代码,背景内联样式现在实际上不起作用,我不知道为什么 <?php /* Template Name: Home Content Template*/ ?> <?php get_header(); ?> <div class="row show-grid lrds-hm-grd-wrppr"> <div

我正在尝试向我的主页模板中添加一个博客帖子网格。我已经从4篇博文中提取了内容,现在我只需要将特色图片作为每个div的背景

这是我当前完成了一半的代码,背景内联样式现在实际上不起作用,我不知道为什么

<?php /* Template Name: Home Content Template*/ ?>
<?php get_header(); ?>



<div class="row show-grid lrds-hm-grd-wrppr">
  <div class="span2 lrds-hm-grd-bx lrds-hm-grd-bx-2" data-original-title="" title="" style="background url (<?php echo get_the_post_thumbnail_url($post_id1, 'full'); ?>) center center;  background-size: cover;  background-repeat: no-repeat;">
    <?php
    $post_id1 = 172;
    $queried_post = get_post($post_id1);
    ?>
    <h2><?php echo $queried_post->post_title; ?></h2>
    <?php echo $queried_post->post_content; ?>
  </div>

  <div class="span2 lrds-hm-grd-bx lrds-hm-grd-bx-2" data-original-title="" title="" style="background url (<?php echo get_the_post_thumbnail_url($post_id2, 'full'); ?>) center center;   background-size: cover;   background-repeat: no-repeat;">
    <?php
    $post_id2 = 174;
    $queried_post = get_post($post_id2);
    ?>
    <h2><?php echo $queried_post->post_title; ?></h2>
    <?php echo $queried_post->post_content; ?>
  </div>

  <div class="span3 lrds-hm-grd-bx lrds-hm-grd-bx-3" data-original-title="" title="" style="background url (<?php echo get_the_post_thumbnail_url($post_id3, 'full'); ?>) center center;   background-size: cover;   background-repeat: no-repeat;">
    <?php
    $post_id3 = 176;
    $queried_post = get_post($post_id3);
    ?>
    <h2><?php echo $queried_post->post_title; ?></h2>
    <?php echo $queried_post->post_content; ?>
  </div>

  <div class="span4 lrds-hm-grd-bx lrds-hm-grd-bx-4" data-original-title="" title="" style="background url (<?php echo get_the_post_thumbnail_url($post_id4, 'full'); ?>) center center;   background-size: cover;   background-repeat: no-repeat;">
    <?php
    $post_id4 = 210;
    $queried_post = get_post($post_id4);
    ?>
    <h2><?php echo $queried_post->post_title; ?></h2>
    <?php echo $queried_post->post_content; ?>
  </div>
</div>

  <div class="row">
    <div class="col-md-2">

    </div>

    <div class="col-md-8">

      <?php if(have_posts()) : ?>
         <?php while(have_posts()) : the_post(); ?>
        <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
          <?php the_title('<h2>','</h2>'); ?>
          <?php the_content(); ?>
        </div>

      <?php
          // tags anyone?
          the_tags();

        ?>
         <?php endwhile; ?>

      <?php if (!is_singular()) : ?>
        <div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
        <div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
      <?php endif; ?>

      <?php else : ?>

      <div class="alert alert-info">
        <strong>No content in this loop</strong>
      </div>

      <?php endif; ?>

    </div>

    <div class="col-md-2">

      <?php
       if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('Sidebar')) : //  Sidebar name
      ?>
      <?php
           endif;
      ?>
    </div>

  </div>

<?php get_footer(); ?>


是的,就这样把它传给你所有的朋友

background url (<?php echo wp_get_attachment_url (get_post_thumbnail_id( $post_id($post_id)); ?>)

背景url(这里是一个工作文件的示例,背景图像是从同一页面的特色图像中提取的,而不是一篇单独的博客文章(这是我目前尝试在不同模板上执行的操作)

“代码”

<?php $title = trim(get_field("custom_headline"));
    // check for custom title different from page name
    if ($title == "") {
        $title = Trim(get_the_title()); 
    }        
 ?>
<h2><?php echo $title; ?></h2>
<?php
    global $related_du;
    $rel = $related_du->show( get_the_ID(), true );
    $boxTitle = array();
    $boxContent = array();
    $i = 0;

    // Display the title of each related post
    if( is_array( $rel ) && count( $rel ) > 0 ) {
        foreach ( $rel as $r ) {
            if ( is_object( $r ) ) {
                if ($r->post_status != 'trash') {
                    //echo get_the_title( $r->ID ) . '<br />';
                    //echo get_post($post = $r->ID ) . '<br />';

                    $displayString = "";
                    $content_post = get_post($r->ID);
                    $boxTitle[$i] = $content_post->post_title;
                    $content = $content_post->post_content;
                    $content = apply_filters('the_content', $content);
                    $content = str_replace(']]>', ']]&gt;', $content);

                    $boxContent[$i] =  $content;
                    $i++;


                }
            }
        }
    }
?>



<div class="hero2box" style="background:URL(<?php the_post_thumbnail_url( 'full' ); ?>) center center;  background-size: cover;     background-repeat: no-repeat;">
    <div class="col-sm-1"></div>
    <div class="col-sm-5 tanLeft">
        <h4><?php echo $boxTitle[0] ; ?></h4>
        <hr class="red">
        <?php echo $boxContent[0] ; ?>
    </div>
    <div class="col-sm-5 tanRight">
        <h4><?php echo $boxTitle[1] ; ?></h4>
        <hr class="red">
        <?php echo $boxContent[1] ; ?>
    </div>
    <div class="col-sm-1"></div>
</div>

<div class="row">
    <div class="col-md-2">

    </div>

    <div class="col-md-8">





       <?php while(have_posts()) : the_post(); ?>
        <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

            <?php the_content(); ?>


            <?php //echo get_post_type(); ?>
        </div>

    <?php
            // tags anyone?
            the_tags();

        ?>
       <?php endwhile; ?>

    <?php if (!is_singular()) : ?>
        <div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
        <div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
    <?php endif; ?>

    <?php else : ?>

    <div class="alert alert-info">
      <strong>No content in this loop</strong>
    </div>

    <?php endif; ?>




</div>

<div class="col-md-2">

    <?php
     if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('Sidebar')) : //  Sidebar name
    ?>
    <?php
         endif;
    ?>
</div>


好的,解决了!我必须更简单,因为我有时候很笨

<?php
    $post_id1 = 172;
    $queried_post = get_post($post_id1);
?>
<div class="row show-grid lrds-hm-grd-wrppr">
  <div class="span2 lrds-hm-grd-bx lrds-hm-grd-bx-2"   style="background:url(<?php echo wp_get_attachment_url(get_post_thumbnail_id( $post_id1)); ?>)    ">

    <h2><?php echo $queried_post->post_title; ?></h2>
    <?php echo $queried_post->post_content; ?>
  </div> 


您需要将te post id传递到所需的缩略图url。在完全给予之后,$post_idSo,您的意思是只在此处添加它,它就会显示出来吗?还是我缺少了其他内容…?已经给出了答案,请尝试一下。我认为这就是您的意思。不幸的是,我很早就尝试了,但它根本不起作用。我想知道是否可能没有这是我应该做的其他事情,比如在functions.php或其他东西中?并更改变量名,如post_id1、post_id2。不要保持不变,我仍然无法让它工作。这是一个糟糕的问题。也许我应用了你的答案错误?但看起来不是这样。发生了什么事?
<?php
    $post_id1 = 172;
    $queried_post = get_post($post_id1);
?>
<div class="row show-grid lrds-hm-grd-wrppr">
  <div class="span2 lrds-hm-grd-bx lrds-hm-grd-bx-2"   style="background:url(<?php echo wp_get_attachment_url(get_post_thumbnail_id( $post_id1)); ?>)    ">

    <h2><?php echo $queried_post->post_title; ?></h2>
    <?php echo $queried_post->post_content; ?>
  </div>