默认文件名
基础调用标签
最新文章调用
分类文章调用
热门文章调用
随机文章调用
TDK功能
WooCommerce最新商品调用
WooCommerce分类商品调用
WooCommerce随机商品调用
头部:header.php
首页:home.php
页脚:footer.php
分类:category.php
文章:single.php
单页:page.php
函数:functions.php
标签:tag.php
侧边栏:sidebar.php
图片:image.php
样式:style.css
后台主题封面:screenshot.png
网站当前语言:<?php language_attributes(); ?>
网站编码:<?php bloginfo( ‘charset’ ); ?>
网站标题:<?php bloginfo(‘name’); ?>
绝对地址:<?php bloginfo(‘url’); ?>
网站副标题:<?php bloginfo(‘description’); ?>
日志或页面标题:<?php wp_title(); ?>
相对地址:<?php bloginfo( ‘stylesheet_url’ ); ?>
Pingback地址:<?php bloginfo(‘pingback_url’); ?>
相对地址:<?php bloginfo(‘template_url’); ?>
头部文件:<?php get_header();?>
底部文件:<?php get_footer();?>
Hook标签(插件):<?php wp_head(); ?>
导航菜单:<?php wp_nav_menu( array( ‘container’ => ”,’menu_class’ => ‘【class】’,’menu_id’ => ‘【div的id】’) ); ?>
侧边栏:<?php get_sidebar();?>
更多按钮链接:<?php get_sidebar();?>
Wordpress版本:<?php bloginfo(‘version’); ?>
Atom地址:<?php bloginfo(‘atom_url’); ?>
RSS2地址:<?php bloginfo(‘rss2_url’); ?>
网站的HTML版本:<?php bloginfo(‘html_type’); ?>
时间标签(年月日时分秒星期):<?php the_time( ‘Y m d H:i:s l ‘ ) ?>
下一篇日志的URL地址:<?php next_post_link(‘%link’) ?>
上一篇日志的URL地址:<?php previous_post_link(‘%link’) ?>
显示载入页面查询:<?php echo get_num_queries(); ?>
显示载入页面的时间:<?php timer_stop(1); ?>
在日志或页面中插入分页:
打印输出信息:<?php _e(’Message’); ?>
显示注册链接:<?php wp_register(); ?>
显示登入/登出链接:<?php wp_loginout(); ?>
默认评论:<?php comments_template();?>
<?php
$query = new WP_Query(array(
'posts_per_page' => 5, // 显示最新5篇(自定义调整)
'post_status' => 'publish'
));
if ( $query->have_posts() ) {?>
<ul>
<?php while ( $query->have_posts() ) {
$query->the_post();?>
<li><a href="<?php get_permalink(); ?>"><?php get_the_title(); ?></a></li>
<?php }?>
</ul>
<?php
wp_reset_postdata();
}
?>
<?php $cat_id = 1; // 分类ID,根据实际情况修改 $args = array( 'cat' => $cat_id, 'posts_per_page' => 5, // 显示最新5篇 'orderby' => 'date', 'order' => 'DESC', ); $query = new WP_Query($args); if ($query->have_posts()) :?> <ul> <?php while ($query->have_posts()) : $query->the_post();?> <li><a href="<?php get_permalink(); ?>"><?php get_the_title(); ?></a></li> <?php endwhile;?> </ul> <?php wp_reset_postdata(); else : echo '暂无文章'; endif; ?>
在 functions.php 中添加计数函数:
function get_post_views($postID){
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
return "0";
}
return $count;
}
function set_post_views($postID) {
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
}else{
$count++;
update_post_meta($postID, $count_key, $count);
}
}
在 single.php 或文章详情页循环里调用<?php echo set_post_views(get_the_ID()); ?>记录浏览量。
最后就可以用下面代码调用浏览量最多的文章了
<?php $args = array( 'meta_key' => 'post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC', 'posts_per_page' => 10 // 调整数量 ); $popular_posts = new WP_Query($args); if($popular_posts->have_posts()) : while($popular_posts->have_posts()) : $popular_posts->the_post(); ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endwhile; wp_reset_postdata(); endif;?>
<?php $args = array( 'posts_per_page' => 5, // 显示5篇随机文章,可自行修改数量 'orderby' => 'rand' ); $random_query = new WP_Query($args); if ($random_query->have_posts()) : echo '<ul>'; while ($random_query->have_posts()) : $random_query->the_post(); echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>'; endwhile; echo '</ul>'; wp_reset_postdata(); endif; ?>
待定
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 8,
'orderby' => 'date',
'order' => 'DESC'
);
$loop = new WP_Query($args);
if ($loop->have_posts()) {
echo '<div class="my-products-list">';
while ($loop->have_posts()) : $loop->the_post();
global $product;
?>
<div class="my-product-item">
<a href="<?php the_permalink(); ?>">
<?php if (has_post_thumbnail()) {
the_post_thumbnail('medium');
} ?>
<h2><?php the_title(); ?></h2>
</a>
<span class="price"><?php echo $product->get_price_html(); ?></span>
</div>
<?php
endwhile;
echo '</div>';
} else {
echo __('暂无商品');
}
wp_reset_postdata();
?>
<?php
$cat_ids = array(12, 15, 18); // 这里填入你的商品分类ID数组
foreach ($cat_ids as $cat_id) {
// 获取分类对象以显示分类名称
$term = get_term($cat_id, 'product_cat');
if (!$term || is_wp_error($term)) continue;
echo '<h2 class="my-cat-title">' . esc_html($term->name) . '</h2>';
$args = array(
'post_type' => 'product',
'posts_per_page' => 8,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $cat_id,
),
),
'orderby' => 'date',
'order' => 'DESC'
);
$loop = new WP_Query($args);
if ($loop->have_posts()) {
echo '<div class="my-cat-products-list">';
while ($loop->have_posts()) : $loop->the_post();
global $product;
?>
<div class="my-cat-product-item">
<a href="<?php the_permalink(); ?>">
<?php if (has_post_thumbnail()) {
the_post_thumbnail('medium');
} ?>
<h3><?php the_title(); ?></h3>
</a>
<span class="price"><?php echo $product->get_price_html(); ?></span>
</div>
<?php
endwhile;
echo '</div>';
} else {
echo '<p>暂无该分类商品</p>';
}
wp_reset_postdata();
}
?>
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 8,
'orderby' => 'rand',
'post_status' => 'publish'
);
$loop = new WP_Query($args);
if ($loop->have_posts()) {
echo '<div class="my-random-products">';
while ($loop->have_posts()) : $loop->the_post();
global $product;
?>
<div class="custom-product-item">
<a href="<?php the_permalink(); ?>">
<?php if (has_post_thumbnail()) {
the_post_thumbnail('medium');
} ?>
<h3><?php the_title(); ?></h3>
<span class="price"><?php echo $product->get_price_html(); ?></span>
</a>
</div>
<?php
endwhile;
echo '</div>';
} else {
echo __('暂无商品');
}
wp_reset_postdata();
?>