Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

循环打印文章中自定义栏目 #30

Open
deepthan opened this issue Mar 8, 2018 · 0 comments
Open

循环打印文章中自定义栏目 #30

deepthan opened this issue Mar 8, 2018 · 0 comments

Comments

@deepthan
Copy link
Owner

deepthan commented Mar 8, 2018

  • 原始需求

在后台可以添加或删除logo。

image

需求解剖:
logo数量不一定,需要把它放在文章的自定义栏目中以一个关键词承载并在代码中打印出来。

  • 那怎么循环打印呢?
  1. 在后台的新建一篇文章,分类为轮播logo,名字也取为轮播logo(名字随便取都可以)文章自定义栏目中定义一个image名称,它的值分别是三个图片的地址。

image

  1. 代码这样写
   <?php
   $my_query = getMy_query(1,"轮播logo");
   if( $my_query->have_posts() ) {
     while ($my_query->have_posts()) : $my_query->the_post();
       $image = get_post_meta ( $post->ID, 'image', false );
       for ($x=0; $x<sizeof($image); $x++) {;?>
    	 <img src="<?php echo $image[$x];?>" alt="">
      <?php } 
      endwhile; wp_reset_query(); 
    }else{ ?>
		<p> 暂无数据 </p>
    <?php } ?>	

$my_query = getMy_query(1,"轮播logo") 从分类为轮播logo的分类中查找一篇文章
if( $my_query->have_posts() )如果查询到有文章的话
while ($my_query->have_posts()) : $my_query->the_post() 当有文章则调用文章 $image = get_post_meta ( $post->ID, 'image', false ) 取名字为image的自定义栏目, false表示以数组方式展示,true表示只取第一个
for ($x=0; $x<sizeof($image); $x++) {;?> 循环打印image里面的内容,sizeof()函数返回image数组的长度
<img src="<?php echo $image[$x];?>" alt=""> 打印出url

打开浏览器就可以看到图片渲染出来啦。

@deepthan deepthan changed the title 循环文章自定义栏目并打印出来 循环文章中自定义栏目并打印出来 Mar 8, 2018
@deepthan deepthan changed the title 循环文章中自定义栏目并打印出来 循环打印文章中自定义栏目 Mar 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant