word press常用的标签调用

 

简述:1、头部导航自定义调用-------------------------------------------------------------------------------------------------------------------<nav> <ul> <?php // 获取WordPress中的主菜单 $main_menu_items = wp_get_nav_menu_items(&#39;main-menu&#39;); // 循环遍历每个菜单项 foreach ($main_menu_items as $item) { // 输出菜单项 echo &#39;<li><a href="&#39; . $item->url . &#39;">&#39; . $item->title . &#39;</a>&#39;; // 检查是否存在子菜单项 $sub_menu_items = wp_get_nav_menu_items(...

详情:

1、头部导航自定义调用-------------------------------------------------------------------------------------------------------------------

<nav>

  <ul>   

    <?php

      // 获取WordPress中的主菜单

      $main_menu_items = wp_get_nav_menu_items('main-menu');      

      // 循环遍历每个菜单项

      foreach ($main_menu_items as $item) {     

         // 输出菜单项

        echo '<li><a href="' . $item->url . '">' . $item->title . '</a>';  

              // 检查是否存在子菜单项

        $sub_menu_items = wp_get_nav_menu_items($item->ID);   

             if ($sub_menu_items) {          

               echo '<ul>';    

                     // 循环遍历子菜单项

                 foreach ($sub_menu_items as $sub_item) {   

                          // 输出子菜单项

                       echo '<li><a href="' . $sub_item->url . '">' . $sub_item->title . '</a></li>';

          }         

          echo '</ul>';

        }       

         echo '</li>';

      }  

    ?>

  </ul>

</nav>


1-1、头部导航自定义调用-------------------------------------------------------------------------------------------------------------------




1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

<?php wp_nav_menu( 

array

&#39;theme_location&#39;  => &#39;&#39; //指定显示的导航名,如果没有设置,则显示第一个 

&#39;menu&#39;            => &#39;header-menu&#39;, 

&#39;container&#39;       => &#39;nav&#39;, //最外层容器标签名 

&#39;container_class&#39; => &#39;primary&#39;, //最外层容器class名 

&#39;container_id&#39;    => &#39;&#39;,//最外层容器id值 

&#39;menu_class&#39;      => &#39;sf-menu&#39;, //ul标签class 

&#39;menu_id&#39;         => &#39;topnav&#39;,//ul标签id 

&#39;echo&#39;            => true,//是否打印,默认是true,如果想将导航的代码作为赋值使用,可设置为false 

&#39;fallback_cb&#39;     => &#39;wp_page_menu&#39;,//备用的导航菜单函数,用于没有在后台设置导航时调用 

&#39;before&#39;          => &#39;&#39;,//显示在导航a标签之前 

&#39;after&#39;           => &#39;&#39;,//显示在导航a标签之后 

&#39;link_before&#39;     => &#39;&#39;,//显示在导航链接名之后 

&#39;link_after&#39;      => &#39;&#39;,//显示在导航链接名之前 

&#39;items_wrap&#39;      => &#39;<ul id="%1$s">%3$s</ul>&#39;, 

&#39;depth&#39;           => 0,////显示的菜单层数,默认0,0是显示所有层 

&#39;walker&#39;          => &#39;&#39;// //调用一个对象定义显示导航菜单 ));  

?>




2、轮播图调用-------------------------------------------------------------------------------------------------------------------

<?php get_header(); ?>

<div id="frame-top-slider-detail"> </div>

        <div id="frame-top-slider" class="smallslider">

            <!-- 大图轮播-->

            <script type="text/javascript">

                $(document).ready(function () {

                    var l=$(&#39;#bigpic&#39;).children().length;

                    var str=&#39;&#39;;

                    for(var i=0; i < l;i++)

                    {

                        if(i==0)

                            str += &#39;<li  class="on" ></li>&#39;;

                        else

                            str += &#39;<li class=" "></li>&#39;;

                    }

                    $(&#39;#pager&#39;).html(str);

                    var mosaic = XMosaic(&#39;bigpic&#39;, { pager: &#39;pager&#39;, delay: 10000, countX: 10, countY: 5, how: 9, order: 0 });

                });

            </script>

            <div  id ="bigpic">

            <a style="left: 0px; top: 0px; position: absolute;" target="_blank" href="/13v0to-1.html">

    <img  src=&#39;<?php echo get_option(&#39; wpd_banner1 &#39;); ? alt="wordpress如何进行仿站" >&#39; alt=&#39;1&#39; />

</a><a style="left: 0px; top: 0px; position: absolute;" target="_blank" href="">

    <img  src=&#39;<?php echo get_option(&#39; wpd_banner2 &#39;); ? alt="wordpress如何进行仿站" >&#39; alt=&#39;2&#39; />

</a>          </div>

        </div>

    </div>


3、部分文章图调用-------------------------------------------------------------------------------------------------------------------

div id="frame-top-slider-mask">

        <ul class="ctrls" id="pager">

        </ul>

    </div>

</div>

<div id="frame-main-body">

    <div id="frame-main-wrapper" class="yui3-g">

        <div id="frame-main" class="yui3-u">

            <div id="frame-main-left" class="yui3-u-1-2">

                <div class="module module_left">

                    <div class="module-name">

                    <a href="/75d14e-1.html" target="_blank">学院新闻</a>

                    <a style="font-size:13px; margin-left:250px; color:blue;" target="_blank" href="/75d14e-1.html">更多</a>

                    </div>

                    <div class="module-spliter">

                        <div> </div>

                    </div>

                    <div class="module-warpper">

                        <div>

    <div class="row pd5 fb">

        <a target="_blank" title="海洋与环境学院孙军院长一行走访国家海洋信息中心" href="/lqpmfv.html">海洋与环境学院孙军院长一行走访国家海洋信息中心</a>

    </div>

    <li class="row spic tag_description">

        <a target="_blank" href="/lqpmfv.html" title="海洋与环境学院孙军院长一行走访国家海洋信息中心">

            <img width=360    style="max-width:90%" align="left" alt="海洋与环境学院孙军院长一行走访国家海洋信息中心" src="<?php bloginfo(&#39;template_url&#39;); ?>/picture/540_225.jpg">

                    </a>

    </li>

    <div style="clear:both;"></div>

</div>                        <table cellspacing="0" cellpadding="0" class="tag_table">

    <tbody>

         

        <?php

            query_posts(

                //&#39;query_type = post&posts_per_page=8&#39;

                array ( &#39;category_name&#39; => &#39;business&#39;, &#39;posts_per_page&#39; => 8 )

                );

                $i=0; while(have_posts()) : the_post(); $i++; ?>

                <tr>

                <td width="100%">

                    <ul>

                        <li class="tag_title">

                            <a target="_blank" title="<?php the_title(); ?>" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>

                        </li>

                    </ul>

                </td>

                </tr>            

            <?php endwhile; wp_reset_query(); ?> 

         

        </tbody>

</table>                  </div>

                </div>

            </div>

            <div id="frame-main-right" class="yui3-u-1-2">

                <div class="module">

                    <div class="module-name"> <a href="/c6c7e7-1.html" target="_blank">通知公告</a>

                    <a style="font-size:13px; margin-left:250px; color:blue;" target="_blank" href="/c6c7e7-1.html">更多</a>

                    </div>

                    <div class="module-spliter">

                        <div> </div>

                    </div>

                    <div class="module-warpper">

            <table cellspacing="0" cellpadding="0" class="tag_table">

    <tbody>

         

        <?php

        query_posts(

            //&#39;query_type = post&posts_per_page=8&#39;

            array ( &#39;category_name&#39; => &#39;technolo&#39;, &#39;posts_per_page&#39; => 8 )

            );

            $i=0; while(have_posts()) : the_post(); $i++; ?>        

                <tr>

                    <td width="100%">

                        <ul>

                            <li class="tag_title">

                                <a target="_blank" title="<?php the_title(); ?>" href="<?php the_permalink(); ?>"><?php the_time(&#39;Y年n月j日&#39;); ?>  <?php the_title(); ?></a>

                            </li>

                        </ul>

                    </td>

                </tr>

        <?php endwhile; wp_reset_query(); ?> 

         

        </tbody>

</table>                  </div>

                </div>

            </div>

        </div>

        <div id="frame-side" class="yui3-u">

            <div id="placehoder"></div>

            <ul>

                <li><a target="" href="/906368.html">研究成果</a></li><li><a target="" href="/faef50-1.html">学术交流</a></li><li><a target="_blank" href="http://hyxy.tust.edu.cn/shiyanshifan/1shoye.html">实践教学</a></li><li><a target="" href="/ccac59.html">教学成果</a></li><li><a target="" href="/4d3baa-1.html">学生资助</a></li><li><a target="" href="/801d37-1.html">校友相聚</a></li><li><a target="" href="#">学术报告</a></li>         </ul>

        </div>

    </div>

    <div id="frame-main-link" class="yui3-g">

        <div>

            <div id="demo" style="overflow:hidden;height:110px;margin:0 auto;">

                <table align="left" cellpadding="0" cellspace="0" border="0">

                    <tr>

                        <td id="demo1" valign="top">

                        <div>

                        <a style="width:0px;" href="/0c8a9f.html#ppzy" target="_blank"><img  src="<?php bloginfo(&#39;template_url&#39;); ? alt="wordpress如何进行仿站" >/picture/57243dbb36ec49fe8ea8839a947fe310.jpg" /></a><img  src="<?php bloginfo(&#39;template_url&#39;); ? alt="wordpress如何进行仿站" >/picture/869b86dfeae34a31b851fb01cec2b074.jpg" /><a   style="max-width:90%" href="http://hyhjbhjs.cl1.soochong.com" target="_blank"><img  src="<?php bloginfo(&#39;template_url&#39;); ? alt="wordpress如何进行仿站" >/picture/2015-08-27-20-0606158108.jpg" /></a><img  src="<?php bloginfo(&#39;template_url&#39;); ? alt="wordpress如何进行仿站" >/picture/39bfa2a5c9be4d87bb8c50a886d3f3c0.jpg" /></div>

                        </td>

                        <td id="demo2" valign="top"> </td>

                    </tr>

                </table>

            </div>

        </div>

    </div>

</div>

<?php get_footer(); ?>


3、文章分类页面模板标签调用-------------------------------------------------------------------------------------------------------------------

方法一:

<?php

if (have_posts()) {

    while (have_posts()) {

        the_post();

        ?>

          <a href="<?php echo esc_url(get_permalink(get_the_ID())); ?>" class="gonew_item curnew" data-id="">

                            <div class="imgbox">

                                <img src="<?php the_field('litpic'); ?>" alt="<?php the_title(); ?> ">

                            </div>

                            <div class="ctext">

                                <div class="t01 dot"><?php the_title(); ?> </div>

                                <div class="t02 dot">V1Pro</div>

                                <div class="t03 dot">Innovation Meets Limitless Potential</div>

                                <div class="t04"></div>

                            </div>

                            

                            <div class="ctit">Flash</div>

                            

                            <div class="cnew">New</div>

                        </a>

        

        

        <?php

    }

} else {

    echo ' <a  class="gonew_item curnew" data-id="">没有找到相关文章</a>';

}

?>


方法二:

<?php if(have_posts()) { ?>

<?php while(have_posts()) { ?>  

<?php the_post(); ?>  

                        <a href="<?php echo esc_url(get_permalink(get_the_ID())); ?>" class="gonew_item curnew" data-id="">

                            <div class="imgbox">

                                <img src="<?php the_field('litpic'); ?>" alt="<?php the_title(); ?> ">

                            </div>

                            <div class="ctext">

                                <div class="t01 dot"><?php the_title(); ?> </div>

                                <div class="t02 dot">V1Pro</div>

                                <div class="t03 dot">Innovation Meets Limitless Potential</div>

                                <div class="t04"></div>

                            </div>

                            

                            <div class="ctit">Flash</div>

                            

                            <div class="cnew">New</div>

                        </a>

                        

<?php } // end while ?>  

<?php } // end if ?>   



4、获取所有产品分类页面模板标签调用-------------------------------------------------------------------------------------------------------------------                         

           <?php

// WP_Query 获取产品

$args = array(

    'post_type' => 'product',

    'posts_per_page' =>10, // 显示所有产品,可以设置为想要显示的数量

);


$products = new WP_Query($args);


if ($products->have_posts()) :

    while ($products->have_posts()) : $products->the_post();

?>

       

            <div class="item">

                                <a href="<?php the_permalink(); ?>">

                                    <div class="imgbox"><img src="<?php the_post_thumbnail('medium'); ?>" alt="<?php the_title(); ?> "></div>

                                    <div class="t"><?php the_title(); ?> </div>

                                    <span>Learn More</span>

                                </a>

                            </div>

<?php

    endwhile;

    wp_reset_postdata(); // 重置查询

else :

    echo ' <div  class="gonew_item curnew" style="width: 100%;text-align: center;">没有找到相关文章</div>';

endif;

?>  

4-2、获取当前产品分类页面模板标签调用-------------------------------------------------------------------------------------------------------------------     


5、搜索标签标签调用-------------------------------------------------------------------------------------------------------------------     

(1)这个代码可以放到任何位置调用

<form role="search" method="get" class="search-form" action="<?php echo esc_url( home_url( '/' ) ); ?>">

    <label>

        <span class="screen-reader-text"><?php echo _x( 'Search for:', 'label' ); ?></span>

        <input type="search" class="search-field" placeholder="<?php echo esc_attr_x( 'Search …', 'placeholder' ); ?>" value="<?php echo get_search_query(); ?>" name="s" />

    </label>

    <button type="submit" class="search-submit"><?php echo esc_html_x( 'Search', 'submit button' ); ?></button>

</form>

(2)搜索结果一般是search.php这个页面

    <main id="main" class="site-main" role="main">


        <header class="page-header">

            <h1 class="page-title"><?php printf( esc_html__( 'Search Results for: %s', 'your-theme-text-domain' ), '<span>' . get_search_query() . '</span>' ); ?></h1>

        </header>


        <?php if ( have_posts() ) : ?>


            <?php while ( have_posts() ) : the_post(); ?>

                <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

                    <header class="entry-header">

                        <h2 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>

                    </header><!-- .entry-header -->


                    <div class="entry-content">

                        <?php the_excerpt(); ?>

                    </div><!-- .entry-content -->

                </article><!-- #post-<?php the_ID(); ?> -->

            <?php endwhile; ?>


            <?php the_posts_navigation(); ?> <!-- 分页导航 -->


        <?php else : ?>

            <p><?php esc_html_e( 'No results found.', 'your-theme-text-domain' ); ?></p>

        <?php endif; ?>


    </main><!-- #main -->


5、上下页调用-------------------------------------------------------------------------------------------------------------------   


 5.1、总分类文章上下页                           

<a href="<?php echo get_permalink(get_previous_post()->ID); ?>" title="<?php echo get_the_title(get_previous_post()->ID); ?>">

    <div>

        <img src="<?php echo get_theme_file_uri( 'static/images/x_left_h.png' ); ?>">

    </div>

    <div>

        <span>上一篇</span><span><?php echo get_the_title(get_previous_post()->ID); ?></span>

    </div>

</a>


<a href="<?php echo get_permalink(get_next_post()->ID); ?>" title="<?php echo get_the_title(get_next_post()->ID); ?>">

    <div>

        <span>下一篇</span><span><?php echo get_the_title(get_next_post()->ID); ?></span>

    </div>

    <div>

        <img src="<?php echo get_theme_file_uri( 'static/images/x_right_h.png' ); ?>">

    </div>

</a>  

 5.1、单曲分类文章上下页      比如文章子分类是live-streaming

<?php

// 获取当前文章的分类

$categories = get_the_category();

$category_slugs = array();

foreach ($categories as $category) {

    $category_slugs[] = $category->slug;

}


// 检查当前文章是否属于"Live streaming"分类或其子分类

if (in_category('live-streaming') || cat_is_ancestor_of(get_cat_ID('live-streaming'), $categories[0])) {

    // 获取上一篇文章的链接和标题

    $previous_post = get_previous_post(true);

    if (!empty($previous_post)) {

        echo '<a href="' . get_permalink($previous_post->ID) . '" title="' . get_the_title($previous_post->ID) . '">';

        echo '<div><img src="' . get_theme_file_uri('static/images/x_left_h.png') . '"></div>';

        echo '<div><span>上一篇</span><span>' . get_the_title($previous_post->ID) . '</span></div>';

        echo '</a>';

    }else{

        echo '<a >';

        echo '<div><img src="' . get_theme_file_uri('static/images/x_left_h.png') . '"></div>';

        echo '<div><span>上一篇</span><span>没有了。!</span></div>';

        echo '</a>';

    }


    // 获取下一篇文章的链接和标题

    $next_post = get_next_post(true);

    if (!empty($next_post)) {

        echo '<a href="' . get_permalink($next_post->ID) . '" title="' . get_the_title($next_post->ID) . '">';

        echo '<div><span>下一篇</span><span>' . get_the_title($next_post->ID) . '</span></div>';

        echo '<div><img src="' . get_theme_file_uri('static/images/x_right_h.png') . '"></div>';

        echo '</a>';

    }else{

       echo '<a>';

        echo '<div><span>下一篇</span><span> 没有了。</span></div>';

        echo '<div><img src="' . get_theme_file_uri('static/images/x_right_h.png') . '"></div>';

        echo '</a>';

    }

}

?>   

5.3 WooCommerce产品分类上下页  


       <a href="<?php echo get_permalink(get_previous_post()->ID); ?>" title="<?php echo get_the_title(get_previous_post()->ID); ?>">

    <div>

        <img src="<?php echo get_theme_file_uri( 'static/images/x_left_h.png' ); ?>">

    </div>

    <div>

        <span>上一篇</span><span><?php echo get_the_title(get_previous_post()->ID); ?></span>

    </div>

</a>


<a href="<?php echo get_permalink(get_next_post()->ID); ?>" title="<?php echo get_the_title(get_next_post()->ID); ?>">

    <div>

        <span>下一篇</span><span><?php echo get_the_title(get_next_post()->ID); ?></span>

    </div>

    <div>

        <img src="<?php echo get_theme_file_uri( 'static/images/x_right_h.png' ); ?>">

    </div>

</a> 



5.4、 WooCommerce 中的某个产品分类文章上下页  



      

<?php

// 检查当前文章是否属于 WooCommerce 中的 "Custom Signs" 产品分类或其子分类

if (has_term('custom-signs', 'product_cat')) {

    // 获取上一篇文章的链接和标题

    $previous_post = get_previous_post(true);

    if (!empty($previous_post)) {

        echo '<a href="' . get_permalink($previous_post->ID) . '" title="' . get_the_title($previous_post->ID) . '">';

        echo '<div><img src="' . get_theme_file_uri('static/images/x_left_h.png') . '"></div>';

        echo '<div><span>上一篇</span><span>' . get_the_title($previous_post->ID) . '</span></div>';

        echo '</a>';

    }


    // 获取下一篇文章的链接和标题

    $next_post = get_next_post(true);

    if (!empty($next_post)) {

        echo '<a href="' . get_permalink($next_post->ID) . '" title="' . get_the_title($next_post->ID) . '">';

        echo '<div><span>下一篇</span><span>' . get_the_title($next_post->ID) . '</span></div>';

        echo '<div><img src="' . get_theme_file_uri('static/images/x_right_h.png') . '"></div>';

        echo '</a>';

    }

}

?>