我正在尝试创建自定义字段,以按标题获取帖子,并将其显示在我的WP网站的英雄网格中

问题描述 投票:0回答:1

我是WordPress的新手,并且通常进行编码。我正在尝试建立一个新闻网站。现在,我为特色帖子创建了一个网格英雄部分。我希望能够选择将哪些帖子放置在英雄网格中。我认为其中一种方法是通过post_title获取帖子。我已经看了几本教程并尝试使用ACF插件实现该功能,但没有成功。到目前为止,我只能在网格中显示帖子,但我个人不希望按我的喜好显示它们。先进的Tnx。这是代码:

<section class="page-wrap">

    <div class="container-sm">

        <h1><?php  the_title();?></h1>

        <?php get_template_part('includes/section','content');?>
   </div>

   <?php
   $args = array(
       'post_type'=>'post',
       'posts_per_page' => 3
   );
   $_posts = new WP_Query($args);
   ?>
   <?php if($_posts->have_posts()):?>
   <div class="row mt-5">

     <?php while($_posts->have_posts()) : $_posts->the_post();?>

    <?php if(has_post_thumbnail()) {
    $thumbnail_data = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'caursel' );
    $thumbnail_url = $thumbnail_data[0];
}
?>


    <div id="post-<?php the_ID(); ?>"  style="background-image:url('<?php echo $thumbnail_url ?>');width:440px;margin-left:3px;padding:10px; border-radius:10px;display:flex;align:center;" >



        <a href="<?php the_permalink();?>">
        <h3><?php the_title();?></h3>
      </a>
      <h6><?php the_excerpt();?></h6>
    </div>

   <?php endwhile;?>
   </div>
   <?php endif;?>
</section>`

wordpress grid posts
1个回答
0
投票

如果您已经尝试过ACF,并且已经习惯了它,那么我将使用'Relationship'自定义字段。

使用此字段,您可以选择可以选择的帖子(即按类别,标签等),以及可以选择的最小和最大帖子(在您的情况下,可能是一个,因为它是精选帖子)。

一旦在后端设置了此自定义字段,就可以在该字段中添加帖子。

在文本编辑器中,您可以像这样访问ACF:

<?php
  $post_objects = get_field('name_of_acf_relation_field');
  if( $post_objects ) {
     foreach( $post_objects as $single) {
       $ID = $single->ID; ?>
        // write html code here and use your $ID to access the post
        // you can access other custom fields too by using the $ID
        // i.e <?php the_field('field_name', $ID); ?>
      <?php } ?>
  <?php } ?>
© www.soinside.com 2019 - 2024. All rights reserved.