wordpress 怎么$post
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的面包屑导航
如果是新手, 建议用其他Wordpress 的插件。
例如Yoast SEO 或者 WP SEO 他们都有加面包屑的功能, 如果不想加插件, 可以把以下加进function.php // Breadcrumbsfunction custom_breadcrumbs() { // Settings $separator = '>'; $breadcrums_id = 'breadcrumbs'; $breadcrums_class = 'breadcrumbs'; $home_title = 'Homepage'; // If you have any custom post types with custom taxonomies, put the taxonomy name below (e.g. product_cat) $custom_taxonomy = 'product_cat'; // Get the query & post information global $post,$wp_query; // Do not display on the homepage if ( !is_front_page() ) { // Build the breadcrums echo ''; // Home page echo '' . $home_title . ''; echo ' ' . $separator . ' '; if ( is_archive() && !is_tax() && !is_category() && !is_tag() ) { echo '' . post_type_archive_title($prefix, false) . ''; } else if ( is_archive() && is_tax() && !is_category() && !is_tag() ) { // If post is a custom post type $post_type = get_post_type(); // If it is a custom post type display name and link if($post_type != 'post') { $post_type_object = get_post_type_object($post_type); $post_type_archive = get_post_type_archive_link($post_type); echo '' . $post_type_object->labels->name . ''; echo ' ' . $separator . ' '; } $custom_tax_name = get_queried_object()->name; echo '' . $custom_tax_name . ''; } else if ( is_single() ) { // If post is a custom post type $post_type = get_post_type(); // If it is a custom post type display name and link if($post_type != 'post') { $post_type_object = get_post_type_object($post_type); $post_type_archive = get_post_type_archive_link($post_type); echo '' . $post_type_object->labels->name . ''; echo ' ' . $separator . ' '; } // Get post category info $category = get_the_category(); if(!empty($category)) { // Get last category post is in $last_category = end(array_values($category)); // Get parent any categories and create array $get_cat_parents = rtrim(get_category_parents($last_category->term_id, true, ','),','); $cat_parents = explode(',',$get_cat_parents); // Loop through parent categories and store in variable $cat_display $cat_display = ''; foreach($cat_parents as $parents) { $cat_display .= ''.$parents.''; $cat_display .= ' ' . $separator . ' '; } } // If it's a custom post type within a custom taxonomy $taxonomy_exists = taxonomy_exists($custom_taxonomy); if(empty($last_category) && !empty($custom_taxonomy) && $taxonomy_exists) { $taxonomy_terms = get_the_terms( $post->ID, $custom_taxonomy ); $cat_id = $taxonomy_terms[0]->term_id; $cat_nicename = $taxonomy_terms[0]->slug; $cat_link = get_term_link($taxonomy_terms[0]->term_id, $custom_taxonomy); $cat_name = $taxonomy_terms[0]->name; } // Check if the post is in a category if(!empty($last_category)) { echo $cat_display; echo '' . get_the_title() . ''; // Else if post is in a custom taxonomy } else if(!empty($cat_id)) { echo '' . $cat_name . ''; echo ' ' . $separator . ' '; echo '' . get_the_title() . ''; } else { echo '' . get_the_title() . ''; } } else if ( is_category() ) { // Category page echo '' . single_cat_title('', false) . ''; } else if ( is_page() ) { // Standard page if( $post->post_parent ){ // If child page, get parents $anc = get_post_ancestors( $post->ID ); // Get parents in the right order $anc = array_reverse($anc); // Parent page loop if ( !isset( $parents ) ) $parents = null; foreach ( $anc as $ancestor ) { $parents .= '' . get_the_title($ancestor) . ''; $parents .= ' ' . $separator . ' '; } // Display parent pages echo $parents; // Current page echo ' ' . get_the_title() . ''; } else { // Just display current page if not parents echo ' ' . get_the_title() . ''; } } echo ''; }}
求教wordpress如何调用最新文章?
$limit = get_option('posts_per_page');$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;query_posts('showposts=' . $limit=7 . '&paged=' . $paged);//limit变量控制最新文章数量$wp_query->is_archive = true; $wp_query->is_home = false;?>ID)) : ?>
怎么删除wordpress程序中文章浏览数
可以用两种方式,一是插件,二是用代码插件这个WP-Postviews关于这个插件的详细使用,下次详细说明:seotaoke这个模板的,那就是下面的方法:第一步,在网站的后台里找到外观编辑,这个文件functions.php,在这个文件的 ?>前面添加如下一些代码:/* 访问计数 */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;}第二步,在你的文章里,有显示浏览次数的地方添加这样的代码:这时到首页文章里查看即可见到效果!
wordpress首页如何调用最新文章
第一种方法WordPress最新文章的调用可以使用一行很简单的模板标签wp_get_archvies来实现. 代码如下:(显示10篇最新更新文章)或后面这个代码显示你博客中最新的20篇文章,其中format=custom这里主要用来自定义这份文章列表的显示样式。
具体的参数和使用方法你可以参考官方的使用说明- wp_get_archvies。
(fromat=custom也可以不要,默认以UL列表显示文章标题。
)补充: 通过WP的query_posts()函数也能调用最新文章列表, 虽然代码会比较多一点,但可以更好的控制Loop的显示,比如你可以设置是否显示摘要。
具体的使用方法也可以查看官方的说明。
第二种方法调用最新文章:(直接在想要呈现的位置放上以下代码即可)$limit = get_option('posts_per_page');$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;query_posts('showposts=' . $limit=7 . '&paged=' . $paged);$wp_query->is_archive = true; $wp_query->is_home = false;?>ID)) : ?>
Wordpress 突然间post页面空白,但是数据能传输!
三种方法调取缩略图。
<1>;最方便是使用插件,虽然很多人使用WP-Thumbnails,但我觉得thumbnail-for-excerpts比较适合新手,你只需要进入后台进行激活!然后其他统统不用管了!该插件会自动检测你文章的第一幅图片作为摘要缩略图!不管该图片是不是上传到wordpress文件下,即使是外链图片同样有效。
<2>;自定义字段,比较的麻烦,每次都要手动操作。
具体到“美设之家”(百度)了解。
<3>;如果你想使用代码来实现的话,分两种情况,一种是文章有图片的调取第一张,但如果文章没图片首页会不协调。
另一种情况是,文章有图片自动调取第一张,如果文章没图片,则调取放进去的图片,并随机显示出来。
如果不嫌折腾,跟着操作。
function wpu_thumbnail() { global $post; if ( has_post_thumbnail() ) { $domsxe = simplexml_load_string(get_the_post_thumbnail()); $thumbnailsrc = $domsxe->attributes()->src; echo ''; } else { $content = $post->post_content; preg_match_all('//sim', $content, $strResult, PREG_PATTERN_ORDER); $n = count($strResult[1]); if($n > 0){ echo ''; }else { $random = mt_rand(1, 5); echo ''; }} }代码放到functions.php里,然后在主题模板里新建一个“img”文件夹,然后在img文件夹里再创建一个“thumb”文件夹。
准备好5张随机图片放进刚建好的thumb文件夹里,需要重点提一下的是图片的后缀名必须更改为img1.png,img2.png,img3.png,img4.png,img5.png。
最后在所需要的地方调用出来。
<?php wpu_thumbnail( ); ?>不懂再问
寻求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'); //后台更新一次任意一篇日志就可以实现效果。
转载请注明出处51数据库 » $post wordpress
劲风知傲骨