ASCII码 ASCII码

wordpress常用标签调用

发布于:2022-02-15 11:17:59  栏目:技术文档

wordpress模板文件

  • index.php 首页
  • header.php 头部
  • footer.php 尾部
  • archive.php 列表页
  • search.php 搜索页
  • single.php 详情页
  • tag 标签页
  • sidebar.php 侧边栏
  • functions.php 可以自定义函数
  • tdk.php 网站标题描述

常用标签

  1. <?php get_header(); ?> 头部调用

  2. <?php get_footer(); ?> 底部调用

  3. <?php get_footer(); ?> 侧边栏调用

  4. <link href="<?php bloginfo('template_url'); ?>/sy/css.css" type="text/css"> css路径,图片路径和js路径通用

  5. <a href="<?php the_permalink(); ?>"> 文章详情跳转

  6. <img src="<?php echo catch_that_image() ?>" alt="<?php the_title(); ?>"> 调用图片标签

  7. <?php the_title(); ?> 文章标题

8.<?php the_tags(‘’); ? 标签调用

9.<?php echo mb_strimwidth(strip_tags(apply_filters(‘the_content’, $post->post_content)), 0, 60, ‘……’); ?> 文章内容调用并限制字符

10.<?php echo date(“Y-m-d”);?> <?php echo date(“Y-m-d H-i”);?> 时间标签

11.<?php if (have_posts()):while(have_posts()) :the_post(); ?><?php the_content(); ?><?php endwhile; ?><?php endif; ?>文章内容

12.<?php bloginfo(‘name’); ?> 网站域名

13.<?php single_tag_title(); ?> tag标题

14.<?php single_cat_title(); ?> 分类页标题

index首页循环

调用id为1的文章 开始 <?php $the_query = new Wp_Query(array( ‘cat’ => 1, ‘orderby’ => ‘id’, ‘order’ => ‘DESC’, ‘posts_per_page’ => 4, ‘paged’ => get_query_var(‘paged’, 1) )); ?> <?php if ($the_query->have_posts()) : ?> <?php while ($the_query->have_posts()) : $the_query->the_post(); ?>

主体内容

<?php endwhile; ?> <?php wp_reset_postdata(); ?> <?php endif; ?>调用id为1的文章 结束

栏目分类调用

<?php $terms = get_terms(‘category’, ‘orderby=name&hide_empty=0’); $count = count($terms); if ($count > 0) { foreach ($terms as $key => $term) { if ($key <= 4) { echo ‘<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-84"><a class="elementor-item" href="' . get_term_link($term, $term->slug) . '" >‘ . $term->name . ‘</a></li>‘; } } } ?>

搜索页

(1)循环查询规则

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>开始

//循环主体

<?php endwhile; ?><?php endif; ?>结束

搜索框:

<form id="search_form" method="get" target="_search" action="<?php bloginfo('url'); ?>/" accept-charset="utf-8"> <input type="hidden" name="type" value="blog"> <input id="keyword" type="text" name="s" class="logoRDownC_input" value="<?php the_search_query(); ?>" placeholder="请输入搜索关键词"> <input type="submit" class="logoRDownC_input2" value="搜索"> </form>


function.php 页面自定义函数限制标题字符

<?phpfunction excerpttitle($max_length) { $title_str = get_the_title(); if(mb_strlen($title_str,’utf-8’) > $max_length) { $title_str = mb_substr($title_str,0,$max_length,’utf-8’).’…’; } return $title_str;}?>

调用方法:<a href="<?php the_permalink(); ?>"><?php echo excerpttitle(8); ?> </a>

调用80个tag标签:

  1. <?php
  2. if (isset($_GET['showall'])) :
  3. $args = array('hide_empty' => 0);
  4. else :
  5. $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
  6. $per_page = 80;
  7. $offset = ($page - 1) * $per_page;
  8. $args = array('number' => $per_page, 'offset' => $offset, 'hide_empty' => 0);
  9. endif;
  10. $taxonomy = 'post_tag';
  11. $tax_terms = get_terms($taxonomy, $args);
  12. echo '';
  13. foreach ($tax_terms as $tax_term) {
  14. echo '' . '<li style="float:left"><a style="color:#fff;" href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf(
  15. __("View all posts in %s"),
  16. $tax_term->name
  17. ) . '" ' . '>' . $tax_term->name . '</a>&nbsp;&nbsp;&nbsp;</li>';
  18. }
  19. echo ''; ?><br><br>

分类页循环:

  1. <?php
  2. $the_query = new Wp_Query(array(
  3. 'cat' => $cat,
  4. 'orderby' => 'id',
  5. 'order' => 'DESC',
  6. 'paged' => get_query_var('paged', 1)
  7. )); ?>
  8. <?php if ($the_query->have_posts()) : ?>
  9. <?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
  10. 主体内容
  11. <?php endwhile; ?>
  12. <?php wp_reset_postdata(); ?>
  13. <?php endif; ?>
  14. 分页按钮:
  15. <div id="aid" style="text-align: center;margin:19px;">
  16. <?php echo paginate_links(array(
  17. 'total' => $the_query->max_num_pages
  18. )) ?>
  19. </div>

TAG循环:

  1. <?php
  2. if (is_tag()) {
  3. $tagName = single_tag_title('', false);
  4. $tagObject = get_term_by('name', $tagName, 'post_tag');
  5. $tagID = $tagObject->term_id;
  6. } ?>
  7. <?php
  8. $the_query = new Wp_Query(array(
  9. 'tag_id' => $tagID,
  10. 'orderby' => 'id',
  11. 'order' => 'DESC',
  12. 'posts_per_page' => -1
  13. )); ?>
  14. <?php if ($the_query->have_posts()) : ?>
  15. <?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
  16. 主体内容
  17. <?php endwhile; ?>
  18. <?php wp_reset_postdata(); ?>
  19. <?php endif; ?>
相关推荐
阅读 +