首页 前端开发 wordpress

其他函数

tts小陈 发布于 2026-08-04 09:13 更新于 2026-08-04 阅读 4 约 3,361 字 · 阅读 9 分钟
wordpress ·

调用指定分类的标题、链接、别名、文章等

调用指定分类的标题:

<?php echo get_cat_name(1);?>

调用指定分类的链接:

<?php echo get_category_link(1); ?>

调用指定分类的别名:

<?php $cat = get_category(1);echo $cat->slug;?>

调用指定分类的最新文章

<?php query_posts('cat=1&showposts=8'); ?>
<?php while (have_posts()) : the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title();?></a></li>
<?php endwhile; wp_reset_query(); ?>

指定分类从第二篇文章开始调用

其实很简单,就是在文章循环调用代码里加入:

&offset=1

这句代码是指不调前面第一篇文章的意思,offset=2就代表从第三篇文章开始调用

 <?php if (have_posts()) : ?>
<?php query_posts('cat=7&ignore_sticky_posts=1&showposts=5&offset=1'); ?>
<?php while (have_posts()) : the_post(); ?>
 <li>
     <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" target="_blank">
     <?php the_title(); ?></a>
</li>

 <?php endwhile; ?>
<?php endif; wp_reset_query(); ?>

'ignore_sticky_posts' => 1 //作用是在循环中不调取置顶文章

调用最新文章

<?php $post_query = new WP_Query('showposts=10'); 
while ($post_query->have_posts()) : $post_query->the_post(); 
$do_not_duplicate = $post->ID; ?> 
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> 
<?php endwhile;?> 

调用热门文章

<?php
$post_num = 10; // 设置调用条数
$args = array(
'post_password' => '',
'post_status' => 'publish', // 只选公开的文章.
'post__not_in' => array($post->ID),//排除当前文章
'caller_get_posts' => 1, // 排除置顶文章.
'orderby' => 'comment_count', // 依评论数排序.
'posts_per_page' => $post_num
);
$query_posts = new WP_Query();
$query_posts->query($args);
while( $query_posts->have_posts() ) { $query_posts->the_post(); ?>
<li><a href=”<?php the_permalink(); ?>” title=”<?php the_title(); ?>”><?php the_title(); ?></a></li>
<?php } wp_reset_query();?>

排序形式 按发布日期排序 orderby=date 按修改时间排序 orderby=modified 按文章 ID 排序 orderby=ID 按评论最多排序 orderby=comment_count 按标题排序 orderby=title 随机排序 orderby=rand

调用随机文章

<ul>
<?php
global $post;
$postid = $post->ID;
$args = array( 'orderby' => 'rand', 'post__not_in' => array($post->ID), 'showposts' => 10);
$query_posts = new WP_Query();
$query_posts->query($args);
?>
<?php while ($query_posts->have_posts()) : $query_posts->the_post(); ?>
<li><a href=”<?php the_permalink(); ?>” rel=”bookmark” title=”<?php the_title_attribute(); ?>”><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>

调用最新评论

<?php
$comments = get_comments('status=approve&number=5&order=asc');
foreach($comments as $comment) :
$output = '<li>' .get_comment_author().'发表评论说:<a href="' . esc_url( get_comment_link($comment->comment_ID) ) . '">' . $comment->comment_content . '</a></li>';
echo $output;
endforeach;?>

自定义后台顶部菜单

在 HTML 中,每个菜单项都有一个以 "wp-admin-bar-" 开始的 ID。比如工具栏左侧的 WordPress Logo 的 ID 是 "wp-admin-bar-wp-logo"。移除菜单项中 ID 的 "wp-admin-bar-" 部分,那么余下的 "wp-logo" 就是它的 ID。

添加菜单function代码

//管理后台添加按钮
function git_custom_adminbar_menu($wp_admin_bar) {
    if (!is_user_logged_in()) {
        return;
    }
    if (!is_super_admin() || !is_admin_bar_showing()) {
        return;
    }
        $url = home_url();
        $wp_admin_bar->add_menu(array(
        'id' => 'git_shouye',
        'title' => '网站首页', /* 设置链接名 */
        'href' => ''.$url.'',
        'meta' => array(            
            'target' => '_blank'
            )
        ));
}
add_action('admin_bar_menu', 'git_custom_adminbar_menu', 999); //数字999为排序

移除菜单function代码

add_action( 'admin_bar_menu', 'remove_wp_logo', 999 );
function remove_wp_logo( $wp_admin_bar ) {
    $wp_admin_bar->remove_node( 'wp-logo' ); //如果需要移除多个菜单可添加多个
}
💬 留言 共 0 条 · 登录后可留言
还没有留言,来抢沙发~