这可能是个愚蠢的问题,但我无法让它发挥作用。。
Problem
我不知道如何将前端的图像与数据库中的url链接。
Situation
我创建了一个名为“横幅”的自定义帖子类型。在每一篇帖子上,我都能添加一幅特色图片。这将保存在我的数据库中。此外,在每个post edit页面上,还有一个banner\\u url的输入区域。在那里,我给我的横幅的链接,它应该去后,点击它的前端。我现在可以在我想要的页面上显示我想要的图像了。但现在我需要从数据库中取出url,并将其连接到我的图像中。
Code
<?php
// function to show home page banner using query of banner post type
function wptutsplus_home_page_banner() {
// start by setting up the query
$get_banner = new WP_Query( array(
\'post_type\' => \'banners\',
\'meta_query\' => array(
array(
\'key\' => \'banner_link\',
\'value\' => \'https://www.mypage.com\'
)
)
));
// now check if the query has posts and if so, output their content in a banner-box div
if ( $get_banner->have_posts() ) { ?>
<?php while ( $get_banner->have_posts() ) : $get_banner->the_post(); ?>
<div class="container" align="center"><a href=""><?php echo the_post_thumbnail(); ?></a></div>
<?php endwhile; ?>
<?php }
wp_reset_postdata();
}
?>
Question我的问题是:如何从数据库中获取banner\\u url?我如何才能让它与图像一起工作?
提前感谢!
EDIT PROBLEM SOLVED<感谢Abdul Awal
<?php
// function to show home page banner using query of banner post type
function wptutsplus_home_page_banner() {
// start by setting up the query
$get_banner = new WP_Query( array(
\'post_type\' => \'banners\',
\'meta_query\' => array(
array(
\'key\' => \'banner_link\',
\'value\' => \'https://www.mypage.com\'
)
)
));
// now check if the query has posts and if so, output their content in a banner-box div
if ( $get_banner->have_posts() ) : while ( $get_banner->have_posts() ) : $get_banner->the_post();
$output = \'<div class="container" align="center"><a href="\'.get_post_meta( get_the_ID(), \'banner_link\', true ).\'">\'.get_the_post_thumbnail().\'</a></div>\';
endwhile;
endif;
wp_reset_postdata();
return $output;
}
?>
最合适的回答,由SO网友:Abdul Awal Uzzal 整理而成
是否只需要查询具有banner_link
值设置为https://www.mypage.com
?
如果没有,则可以删除meta_query
退出您的查询。
<?php
// function to show home page banner using query of banner post type
function wptutsplus_home_page_banner() {
// start by setting up the query
$get_banner = new WP_Query( array(
\'post_type\' => \'banners\',
\'meta_query\' => array(
array(
\'key\' => \'banner_link\',
\'value\' => \'https://www.mypage.com\'
)
)
));
// now check if the query has posts and if so, output their content in a banner-box div
if ( $get_banner->have_posts() ) { ?>
<?php while ( $get_banner->have_posts() ) : $get_banner->the_post(); ?>
<div class="container" align="center"><a href="<?php echo get_post_meta( $post->ID, \'banner_link\', true ); ?>"><?php the_post_thumbnail(); ?></a></div>
<?php endwhile; ?>
<?php }
wp_reset_postdata();
}
?>
UPDATE
当您在插件中使用它时。您可以执行以下操作:
<?php
// function to show home page banner using query of banner post type
function wptutsplus_home_page_banner() {
// start by setting up the query
$get_banner = new WP_Query( array(
\'post_type\' => \'banners\',
\'meta_query\' => array(
array(
\'key\' => \'banner_link\',
\'value\' => \'https://www.mypage.com\'
)
)
));
// now check if the query has posts and if so, output their content in a banner-box div
if ( $get_banner->have_posts() ) : while ( $get_banner->have_posts() ) : $get_banner->the_post();
$output = \'<div class="container" align="center"><a href="\'.get_post_meta( $post->ID, \'banner_link\', true ).\'">\'.get_the_post_thumbnail().\'</a></div>\';
endwhile;
endif;
wp_reset_postdata();
return $output;
}
?>
然后用
<?php echo wptutsplus_home_page_banner(); ?>
让我知道它是否有效。