WordPress文章内容必备函数调用详解


使用wordpress开发建站,需要使用各位形式的函数调用组合,本文向大家分享的是对于wordpress文章内容中函数的傻瓜式调用方式,方便小白用户及开发大神,限于篇幅测速网只讲wordpress主题文章中常用的几个重点函数类型。

wordpress文章常用函数基础

123456调用文章标题:<?php the_title(); ?>文章内容:<?php the_content(); ?>文章摘要:<?php the_excerpt(); ?>作者姓名:<?php the_author(); ?>发布时间:<?php the_time(); ?>作者的Gravatar头像:<?php echo get_avatar( get_the_author_email(), 36 ); ?>

注意,以下内容需要添加进Wordpress的循环,到这里,普通的小白用户其实已经够用了,但是针对用wordpress站长们,显然还难以满足,那么请继续阅读本文。

1wordpress调用指定分类文章

在网站模块时常需要运用到哪一分类的文章,我们需要了解一个关键语句:

1<?php query_posts(); ?>

wordpress如何通过分类ID调用文章列表
在WP的循环前面加上下面的代码。

12345678<?php$cat_1 = get_cat_ID('分类一');$cat_2 = get_cat_ID('分类二');$limit = get_option('posts_per_page');$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;query_posts('cat=' . $cat_1 . ',' . $cat_2 . '&showposts=' . $limit=10 . '&paged=' . $paged);$wp_query->is_archive = true; $wp_query->is_home = false;?>

注意:分类一和分类二改成你想要的分类,当然你也可以增加。

123456789<?php$cat_1 = get_cat_ID('分类一');$cat_2 = get_cat_ID('分类二');$cat_3 = get_cat_ID('分类三');$limit = get_option('posts_per_page');$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;query_posts('cat=' . $cat_1 . ',' . $cat_2 . ',' . $cat_3 . '&showposts=' . $limit=10 . '&paged=' . $paged);$wp_query->is_archive = true; $wp_query->is_home = false;?>

上面的代码中,$limit=10表示的是你想调用的最新文章的数量,通过修改这个数字可以改变文章显示数量。

wordpress排除指定分类的文章

在首页,我们想显示分类一、分类二,却不想显示分类三,那么怎么办?
在WP的循环前面加上下面的代码:

1234567<?php$cat_1 = get_cat_ID('分类三');$limit = get_option('posts_per_page');$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;query_posts('cat=-' . $cat_1 . '&showposts=' . $limit=10 . '&paged=' . $paged);$wp_query->is_archive = true; $wp_query->is_home = false;?>

注意到了吗,对了,就是多了个“-”号。

wordpress调用文章的第一幅图

12345678910111213141516在functions.php中加入代码:<?phpfunction catch_that_image() {global $post, $posts;$first_img = '';ob_start();ob_end_clean();$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i',$post->post_content, $matches);$first_img = $matches [1] [0];if(empty($first_img)){ //Defines a default image$first_img = "/images/default.jpg";}return $first_img;}?>

在要调用图片的地方加入代码:

1<img src="<?php%20echo%20catch_that_image()%20?>" />

上一篇:教你在WordPress文章页面添加作者信息模块

下一篇:如何在wordpress主题里禁用浏览器的F12和审查元素功能


Copyright © 2002-2019 测速网 www.inhv.cn 皖ICP备2023010105号
测速城市 测速地区 测速街道 网速测试城市 网速测试地区 网速测试街道
温馨提示:部分文章图片数据来源与网络,仅供参考!版权归原作者所有,如有侵权请联系删除!

热门搜索 城市网站建设 地区网站制作 街道网页设计 大写数字 热点城市 热点地区 热点街道 热点时间 房贷计算器