2024 年 4 月 27 日 我的频道
Wordpress 及其它代码备忘
  • 2021
  • Sunny

更改这篇文章的初衷,就是不想频繁发布重复的关于代码备忘的文章!借此文章,发布在主题开发或者插件开发的规程中发现的代码更新发布在此,一是给自己备忘,二是跟大家一同分享,一同进步,如有更好的代码,别忘了页和我一起分享哟!

wordpress 主题开发的过程中,难免会遇到各种各样的问题,本人一样如此。不过凭着死皮赖脸的性格,终于解决了很多的问题,而造就了这个主题的诞生,不光是 wordpress 的代码,还有主题的 Css,同样也兼顾兼容性问题,一直不断的尝试编写,我们会在自学栏目下的 Html5 & Css 子分类和大家分享,一起学习,下面列举了 Wordpress 的一些常用代码供大家参考,并且记录给自己,如果发现代码不可用,请留言告知。(备注:我可是开启了 Wordpress 的 Bug 模式进行开发测试的),这个贴子会无限期更新,并且置顶!搜索代码请添加至主题的 Function.php

空白的主题是什么也没有的,请添加下面的代码进去,获取最原始支持的功能:

/* 注册菜单 */
register_nav_menus( array(
'primary' => __( 'Primary Menu', 'web2go' ),
'secondary' => __( 'Secondary Menu', 'web2go' )
) );

/* 添加文章类型 */
function add_post_formats() {
add_theme_support( 'post-formats', array( 'gallery', 'quote', 'video', 'audio' ) );
}
add_action( 'after_setup_theme', 'add_post_formats', 20 );

/* 支持特色图像 */
if ( function_exists( 'add_theme_support' ) ) {
add_theme_support( 'post-thumbnails' );
}
if ( function_exists( 'add_image_size' ) ) {
add_image_size( 'customized-post-thumb', 100, 120 );
}

/* 退出登录重定向到主页 */
add_action( 'wp_logout', 'auto_redirect_after_logout' );
function auto_redirect_after_logout() {
wp_redirect( home_url() );
exit();
}

/* WordPress 文章中英文数字间自动添加空格(写入数据库)*/
add_filter( 'wp_insert_post_data', 'fanly_post_data_autospace', 99, 2 );
function fanly_post_data_autospace( $data, $postarr ) {
$data[ 'post_title' ] = preg_replace( '/([\x{4e00}-\x{9fa5}]+)([A-Za-z0-9_]+)/u', '${1} ${2}', $data[ 'post_title' ] );
$data[ 'post_title' ] = preg_replace( '/([A-Za-z0-9_]+)([\x{4e00}-\x{9fa5}]+)/u', '${1} ${2}', $data[ 'post_title' ] );
$data[ 'post_content' ] = preg_replace( '/([\x{4e00}-\x{9fa5}]+)([A-Za-z0-9_]+)/u', '${1} ${2}', $data[ 'post_content' ] );
$data[ 'post_content' ] = preg_replace( '/([A-Za-z0-9_]+)([\x{4e00}-\x{9fa5}]+)/u', '${1} ${2}', $data[ 'post_content' ] );
return $data;
}

/* 搜索代码高亮 */function search_word_replace( $buffer ) {
if ( is_search() ) {
$arr = explode( " ", get_search_query() );
$arr = array_unique( $arr );
foreach ( $arr as $v )
if ( $v )
$buffer = preg_replace( "/(" . $v . ")/i", "<span class='search-keyword'>$1</span>", $buffer );
}
return $buffer;
}

add_filter( "the_title", "search_word_replace", 200 );
add_filter( "the_excerpt", "search_word_replace", 200 );
add_filter( "the_content", "search_word_replace", 200 );

添加文章修改更新日期提示,代码放入 Function.php

// 文章最后更新时间
function wpb_last_updated_date() {
$u_time = get_the_time('U');
$u_modified_time = get_the_modified_time('U');
if ($u_modified_time >= $u_time + 86400) {
echo '<div class="post-last-update">Last Modified On ';
the_modified_time('F jS, Y');
echo ' At ';
the_modified_time();
echo '</div>';
}
}

调用:<?php wpb_last_updated_date();?>

显示站点有多少篇文章:

<?php
$count = wp_count_posts();
$getCount = $count->publish;
echo $getCount;
?>

统计站点有多少会员:

<?php $users = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->users"); echo $users; ?>

2021/11/14 更新

Wordpress 一级分类列表:

<?php wp_list_categories('depth=1&title_li=0&hide_empty=0&orderby=id&parent=0'); ?>

Wordpress 显示一级分类并显示别名

<?php

$args = array(
'orderby' => 'id',
'order' => 'ASC',
'hide_empty' => 0
);
$categories = get_categories( $args );
$cat = get_category( get_query_var( 'cat' ) );

foreach ( $categories as $category ) {
echo '<li><a href="' . get_category_link( $category->term_id ) . '" >' . $category->name . '<br>';
echo '<span>' . $category->slug . '</span>';
echo '</a></li>';
}
?>

Wordpress 标签列表,删除最后一个逗号:

$posttags = get_the_tags();
    if ($posttags) {
       $taglist = "";
       foreach($posttags as $tag) {
           $taglist .=  $tag->name . ', '; 
       }
      echo rtrim($taglist, ", ");
   }

更改默认的 404 转向至主题目录下的 404.php

function redirect_to_theme_404(){
   if( is_404() ){
      global $wp_query;
      $wp_query->set_404();
      status_header( 404 );
      get_template_part( 404 );
      exit();
   }
}
add_action( 'template_redirect', 'redirect_to_theme_404' );

懒人福音,为全站特色图像自动加上链接,独立文章页的特色图像除外:

function custom_featured_image_link($html, $post_id, $post_thumbnail_id, $size, $attr) {
    if (!is_single()) {
        $html = '<a href="' . get_permalink($post_id) . '">' . $html . '</a>';
    }
    return $html;
}
add_filter('post_thumbnail_html', 'custom_featured_image_link', 10, 5);

修复页码跳转的问题:

如果你正在使用 $paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1; 来获取当前页码,那么你可以在表单中使用这个 $paged 变量来构建正确的表单 action。以下是一个更新后的表单代码:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
?>

<form method="get" action="<?php echo esc_url(home_url(add_query_arg('paged', $paged))); ?>">
    <!-- 表单的其余部分 -->
    <!-- ... -->
</form>

这将确保表单在提交时将用户发送回相同的页面。对于处理小于当前页码的情况,你可以使用以下代码:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$target_page = isset($_GET['paged']) ? intval($_GET['paged']) : $paged;

if ($target_page < $paged) {
    $target_page = $paged;
}

更多实用代码不断更新中 ……

WORDPRESS
3459
0

Author Box

名字:Sunny
注册:Jan 30, 2021
简介:技术菜鸟,拷贝、黏贴代码中 ……

Comment Box

0 0 投票数
Article Rating
订阅评论
提醒
guest

0 评论
内联反馈
查看所有评论