如何在WordPress的每篇文章底部添加自定义内容
wordpress可以设置自定义字段,方便扩展功能,wordpress利用巧妙的数据库表设计达到这一目的,posts表存放文章,页面和附件等,与之对应的postmeta表用来存储自定义的字段,采用post_id,key,value这样的设计来存放自定义字段的值。
get_post_meta函数用法:get_post_meta($post_id, $key, $single);该函数有3个基本参数:$post_id —— 所检索数据的文章的ID,使用 $post->ID 来获取文章的ID。
$key —— 要检索的自定义字段名称$single —— 这是一个布尔值,如果设置为 true ,将直接以字符串的形式返回字段的值;一个自定义字段可以填写多个值,如果设置为 false,将返回一个数组 array 来显示这多个值。
此函数定义在wordpress的post.php中:
wordpress写文章过程中添加创建的相册,如何给相册里的每张图片单...
'id' ).com/zhidao/pic/item/023b5bb5c9ea15ce7ed3d214bf003af33a87b2e9.jpg" target="_blank" title="点击查看大图" class="ikqb_img_alink">add_filter(' ''orderby' shortcode_atts( array( '( ;figcaption' ;, '=> $html5 ;); , ' '; $post = gallery' , 'html5''link' intval( $atts['=> ' get_post(); $html5 = 'dd' exclude'columns' if ://a图片信息实际仍存储在posts表中;? ',不过可以通过其他字段实现啊 如下图;),  .php文件里的gallery_shortcode函数来重构gallery的输出 在functions.php中添加代码如下;function :  ,在alt text字段里填上该图片的外链 接着;0.hiphotos.baidu.com/zhidao/wh%3D600%2C800/sign=72292ad8e9fe9925cb596156049872e7/023b5bb5c9ea15ce7ed3d214bf003af33a87b2e9,需要参考wp-includes/media; my_post_gallery( 'itemtag' =>.jpg" esrc="http;'thumbnail';  ://a;  ! empty( $atts['include'] ) ) { $_attachments = get_posts( array( 'include' => $atts['include'], 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $atts['order'], 'orderby' => $atts['orderby'] ) ); $attachments = array(); foreach ( $_attachments as $key => $val ) { $attachments[$val->ID] = $_attachments[$key]; } } elseif ( ! empty( $atts['exclude'] ) ) { $attachments = get_children( array( 'post_parent' => $id, 'exclude' => $atts['exclude'], 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $atts['order'], 'orderby' => $atts['orderby'] ) ); } else { $attachments = get_children( array( 'post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $atts['order'], 'orderby' => $atts['orderby'] ) ); } if ( empty( $attachments ) ) { return ''; } if ( is_feed() ) { $output = "\n"; foreach ( $attachments as $att_id => $attachment ) { $output .= wp_get_attachment_link( $att_id, $atts['size'], true ) . "\n"; } return $output; } $itemtag = tag_escape( $atts['itemtag'] ); $captiontag = tag_escape( $atts['captiontag'] ); $icontag = tag_escape( $atts['icontag'] ); $valid_tags = wp_kses_allowed_html( 'post' ); if ( ! isset( $valid_tags[ $itemtag ] ) ) { $itemtag = 'dl'; } if ( ! isset( $valid_tags[ $captiontag ] ) ) { $captiontag = 'dd'; } if ( ! isset( $valid_tags[ $icontag ] ) ) { $icontag = 'dt'; } $columns = intval( $atts['columns'] ); $itemwidth = $columns > 0 ? floor(100/$columns) : 100; $float = is_rtl() ? 'right' : 'left'; $selector = "gallery-{$instance}"; $gallery_style = ''; /** * Filters whether to print default gallery styles. * * @since 3.1.0 * * @param bool $print Whether to print default gallery styles. * Defaults to false if the theme supports HTML5 galleries. * Otherwise, defaults to true. */ if ( apply_filters( 'use_default_gallery_style', ! $html5 ) ) { $gallery_style = " #{$selector} { margin: auto; } #{$selector} .gallery-item { float: {$float}; margin-top: 10px; text-align: center; width: {$itemwidth}%; } #{$selector} img { border: 2px solid #cfcfcf; } #{$selector} .gallery-caption { margin-left: 0; } /* see gallery_shortcode() in wp-includes/media.php */ \n\t\t"; } $size_class = sanitize_html_class( $atts['size'] ); $gallery_div = ""; /** * Filters the default gallery shortcode CSS styles. * * @since 2.5.0 * * @param string $gallery_style Default CSS styles and opening HTML div container * for the gallery shortcode output. */ $output = apply_filters( 'gallery_style', $gallery_style . $gallery_div ); $i = 0; foreach ( $attachments as $id => $attachment ) { $attr = ( trim( $attachment->post_excerpt ) ) ? array( 'aria-describedby' => "$selector-$id" ) : ''; if ( ! empty( $atts['link'] ) && 'file' === $atts['link'] ) { $image_output = wp_get_attachment_link( $id, $atts['size'], false, false, false, $attr ); } elseif ( ! empty( $atts['link'] ) && 'none' === $atts['link'] ) { $attr['alt'] = get_the_title($post->ID); $image_output = wp_get_attachment_image( $id, $atts['size'], false, $attr ); $custom_link = trim( strip_tags( get_post_meta( $id, '_wp_attachment_image_alt', true ) ) ); $image_output = sprintf( '%s', $custom_link, $attr['alt'], $image_output ); } else { $image_output = wp_get_attachment_link( $id, $atts['size'], true, false, false, $attr ); } $image_meta = wp_get_attachment_metadata( $id ); $orientation = ''; if ( isset( $image_meta['height'], $image_meta['width'] ) ) { $orientation = ( $image_meta['height'] > $image_meta['width'] ) ? 'portrait' : 'landscape'; } $output .= ""; $output .= " $image_output "; if ( $captiontag && trim($attachment->post_excerpt) ) { $output .= " <{$captiontag} class='wp-caption-text gallery-...
wordpress模板里的post
single :文章,通常用于判断函数,如is_single();page:页面,通常用于判断函数,如is_page();post:稿件,也就是文章,通常指数据库里的东西,常做为参数被调用;category:分类,通常用于判断分类,如is_cagegory();需要区分三者在意义上的分别,在英语里post有张贴、公告、投稿等等的意义,指的是一个动作,或一个简短通知。
而single强调单一的、独特的某个特定的东西,它是具体有所指的,而page指的页面是有一定稳定性的,有布局的,我想大致类似报纸的某个特定版面的意思。
在默认情况下,wordpress是通过single.php来表示post的,用page.php来表示页面。
这样为了保持语法命名上的一致性,文章在code中都做single,而页面就是page。
post更多和数据库关联。
post存入数据库,但没有发表出来,只是post ,不是single,因为它没经过single.php的处理。
post 通常按日期排序,有即时性,page就静态的多,按字母排的。
如何设置WordPress文章特色图像
1.添加熊掌号ID声明 2.添加关注功能代码(强烈推荐) wordpress中一般是footer.php中。
如果你想要自定义一些样式熟悉也可以选择添加下面类型的代码: cambrian.render('head')二、结构化改造(划重点) 1.添加canonical标签 要求href的内容为MIP页或H5页对应的PC页地址;如果没有PC页,则填写当前页面地址。
看到这别慌,是不是不知道地址页怎么填了,因为wordpress都是发表文章自动生成地址,我们总不能发表一个改一个,其实只要把以下代码放入你的header.php页面就好了,可以自动获取文章地址。
request));if($current_url)echo '' . "\n" ;?>2.添加JSON_LD数据(难点到了) 代码如下:放入你的function.php中 //获取文章/页面摘要function fanly_excerpt($len=220){ if ( is_single() || is_page() ){ global $post; if ($post->post_excerpt) { $excerpt = $post->post_excerpt; } else { if(preg_match('/(.*)/iU',trim(strip_tags($post->post_content,"")),$result)){ $post_content = $result['1']; } else { $post_content_r = explode("\n",trim(strip_tags($post->post_content))); $post_content = $post_content_r['0']; } $excerpt = preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,0}'.'((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*#s','$1',$post_content); } return str_replace(array("\r\n", "\r", "\n"), "", $excerpt); }} //优先获取文章中的三张图,否则依次获取自定义图片/特色缩略图/文章首图 last update 2017/11/23function fanly_post_imgs(){ global $post; $content = $post->post_content; preg_match_all('//', $content, $strResult, PREG_PATTERN_ORDER); $n = count($strResult[1]); if($n >= 3){ $src = $strResult[1][0].'","'.$strResult[1][1].'","'.$strResult[1][2]; }else{ if( $values = get_post_custom_values("thumb") ) { //输出自定义域图片地址 $values = get_post_custom_values("thumb"); $src = $values [0]; } elseif( has_post_thumbnail() ){ //如果有特色缩略图,则输出缩略图地址 $thumbnail_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),'full'); $src = $thumbnail_src [0]; } else { //文章中获取 if($n > 0){ // 提取首图 $src = $strResult[1][0]; } } } return $src;}下面的代码加入到header.php中,代码还加了一个判断,是不是单页,所以只会在文章中输出信息。
{"@context": "https://ziyuan.baidu.com/contexts/cambrian.jsonld","@id": "'.get_the_permalink().'", "appid": "这里请填写熊掌号ID","title": "'.get_the_title().'","images": ["'.fanly_post_imgs().'"],"description": "'.fanly_excerpt().'","pubDate": "'.get_the_time('Y-m-d\TH:i:s').'"}';}?>Amisser迷失者博客(牧某人) 这是我博客写的,觉得不详细 就去百度搜索 迷失者博客
寻求wordpress 给已发布文章批量添加tag
可以在当前皮肤的functions.php里自建一个函数;function naruco_mod_tags(){$post_ids = array(1,2,3,4,5,6,7,8,9,10);foreach( $post_ids as $k => $post_id ){wp_set_post_tags( $post_id, array('tag1','tag2','tag3') );}}手动输入你的日志ID串;如果你是批量修改,那么你需要从库里读取出ID串;然后附加到HOOKS上,执行一次再行删除这个函数就可以了;如:add_action('edit_post','naruco_mod_tags'); //后台更新一次任意一篇日志就可以实现效果。
如何用代码调用wordpress里的文章显示浏览数
实现显示文章显示浏览数的效果,可以用两种方式,一是插件,二是用代码插件这个?0?2WP-Postviews?0?2?0?2 关于这个插件的详细使用,下次详细说明,今晚就细说用代码来实现这种效果。
网上有好多关于这个用代码实现这种效果的方法文章,但是好多都不太管用,可能是是针对某个特定的wordpress版本,在这里我找到了一个比较实用的方法,而且也是符合seotaoke这个模板的,那就是下面的方法:第一步,在网站的后台里找到外观编辑,这个文件?0?2functions.php?0?2,在这个文件的 ?>前面添加如下一些代码:/* 访问计数 */function record_visitors(){if (is_singular()){global $post;$post_ID = $post->ID;if($post_ID){$post_views = (int)get_post_meta($post_ID, 'views', true);if(!update_post_meta($post_ID, 'views', ($post_views+1))){add_post_meta($post_ID, 'views', 1, true);}}}}add_action('wp_head', 'record_visitors');/// 函数名称:post_views/// 函数作用:取得文章的阅读次数function post_views($before = '(点击 ', $after = ' 次)', $echo = 1){global $post;$post_ID = $post->ID;$views = (int)get_post_meta($post_ID, 'views', true);if ($echo) echo $before, number_format($views), $after;else return $views;}第二步,在你的文章里,有显示浏览次数的地方添加这样的代码:这时你到首页文章里查看即可见到效果!尊重别人的劳动成果,在这里与大家分享!
转载请注明出处51数据库 » wordpress add_post_m