我正在尝试创建自定义字段以按标题获取帖子并将它们显示在我的 WP 网站的英雄网格中
I am trying to create custom field to fetch posts by title and display them in a hero grid n my WP website
我是 WordPress 的新手,一般来说是编码。我正在尝试创建一个新闻网站。现在,我为特色帖子创建了一个网格英雄部分。
我希望能够 select 哪些帖子被放置在英雄中 grid.I 假设其中一种方法是通过 post_title.I 来获取它们已经看了几个教程
并尝试使用 ACF 插件实现该功能但没有成功。到目前为止,我只能在网格中显示帖子,但它们并没有按照我个人的喜好显示,这是我想要实现的。 Tnx in advnace。这是代码:
<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>`
如果您尝试过 ACF 并且习惯了它,我会选择 'Relationship' 自定义字段。
通过这个字段,您可以选择可以选择的posts(即按类别、标签等),以及可以选择的最小和最大posts(在你的情况下,也许是一个,因为它是一个特色 post)。
一旦你在后端设置了这个自定义字段,你就可以在该字段中添加一个post。
在您的文本编辑器中,您可以像这样访问 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 } ?>
我是 WordPress 的新手,一般来说是编码。我正在尝试创建一个新闻网站。现在,我为特色帖子创建了一个网格英雄部分。 我希望能够 select 哪些帖子被放置在英雄中 grid.I 假设其中一种方法是通过 post_title.I 来获取它们已经看了几个教程 并尝试使用 ACF 插件实现该功能但没有成功。到目前为止,我只能在网格中显示帖子,但它们并没有按照我个人的喜好显示,这是我想要实现的。 Tnx in advnace。这是代码:
<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>`
如果您尝试过 ACF 并且习惯了它,我会选择 'Relationship' 自定义字段。
通过这个字段,您可以选择可以选择的posts(即按类别、标签等),以及可以选择的最小和最大posts(在你的情况下,也许是一个,因为它是一个特色 post)。
一旦你在后端设置了这个自定义字段,你就可以在该字段中添加一个post。
在您的文本编辑器中,您可以像这样访问 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 } ?>