分享

对Oyiso主题的魔改记录

  1. 让首页文章改为时间顺序显示
  2. 让首页的文章支持无限加载

米饭对Oyiso主题进行一些魔改,下面是魔改的记录。(由于米饭对于Html什么的几乎完全不懂,所以大部分工作是通过AI完成的

让首页文章改为时间顺序显示

首页默认是显示热门文章,但是这样就导致了米饭的博客首页永远是那几篇文章,所以想要修改一下。

只需要编辑主题的templates/tpl-home1.php就可以很简单的实现:

  1. 找到注释 // 创建 popular 实例
  2. 进行修改

PHP $popular = new WP_Query( array( 'post_type' => 'post', // 文章类型 'posts_per_page' => 10, // 每页显示的文章数量 'ignore_sticky_posts' => 1, // 排除置顶文章 'paged' => $paged, //'meta_key' => 'post_heat', // 按照浏览量自定义字段排序 // 'orderby' =>'meta_value_num', // 数值类型的排序 // 'orderby' => 'date', // 'order' => 'DESC' // 降序排列 )

 $popular = new WP_Query(
    array(
        'post_type' => 'post', // 文章类型
        'posts_per_page' => 10, // 每页显示的文章数量
        'ignore_sticky_posts' => 1, // 排除置顶文章
        'paged' => $paged,
        //'meta_key' => 'post_heat', // 按照浏览量自定义字段排序
        // 'orderby' =>'meta_value_num', // 数值类型的排序
        // 'orderby' => 'date',
        // 'order' => 'DESC' // 降序排列
    )

把meta_key那一行注释掉即可

让首页的文章支持无限加载

既然已经改成了显示最新文章,所以就想要试试实现无限加载的功能。

  • 修改整个创建实例的代码

PHP<?php // 获取当前页码 $paged = (get_query_var('paged'))? get_query_var('paged') : 1; // 创建 popular 实例 $popular = new WP_Query( array( 'post_type' => 'post', // 文章类型 'posts_per_page' => 10, // 每页显示的文章数量 'ignore_sticky_posts' => 1, // 排除置顶文章 'paged' => $paged, //'meta_key' => 'post_heat', // 按照浏览量自定义字段排序 // 'orderby' =>'meta_value_num', // 数值类型的排序 // 'orderby' => 'date', // 'order' => 'DESC' // 降序排列 ) ); $totalPages = $popular->max_num_pages;

if ($popular->have_posts()) : while ($popular->have_posts()) : $popular->the_post(); $categories = get_the_category(); ?>

&lt;?php
		// 获取当前页码
$paged = (get_query_var(&#39;paged&#39;))? get_query_var(&#39;paged&#39;) : 1;
        // 创建 popular 实例
        $popular = new WP_Query(
    array(
        &#39;post_type&#39; =&gt; &#39;post&#39;, // 文章类型
        &#39;posts_per_page&#39; =&gt; 10, // 每页显示的文章数量
        &#39;ignore_sticky_posts&#39; =&gt; 1, // 排除置顶文章
        &#39;paged&#39; =&gt; $paged,
        //&#39;meta_key&#39; =&gt; &#39;post_heat&#39;, // 按照浏览量自定义字段排序
        // &#39;orderby&#39; =&gt;&#39;meta_value_num&#39;, // 数值类型的排序
        // &#39;orderby&#39; =&gt; &#39;date&#39;,
        // &#39;order&#39; =&gt; &#39;DESC&#39; // 降序排列
    )
);
$totalPages = $popular-&gt;max_num_pages;

        if ($popular-&gt;have_posts()) :
          while ($popular-&gt;have_posts()) : $popular-&gt;the_post();
            $categories = get_the_category();
      ?&gt;
  • 为列表添加id

    • 找到
        这里 为他添加id修改为:
  • 在列表底部添加 加载更多按钮

JavaScript

   加载更多

	   &lt;!-- 加载更多按钮--&gt;
	  &lt;div class=&quot;containerrice&quot; style=&quot;
    display: flex;
    justify-content: center; padding-top: 10px;
&quot;&gt;
    &lt;button class=&quot;show-articles-btn&quot; id=&quot;load-more-posts&quot; class=&quot;btn btn-primary&quot; data-max=&quot;&lt;?php echo $totalPages;?&gt;&quot;&gt;加载更多&lt;/button&gt;

	&lt;style&gt;
	button.show-articles-btn:active {
    transform: translateY(1px);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

button.show-articles-btn:hover {
    background-color: var(--f-button-hover-bg);
    transform: translateY(0px);

}
button.show-articles-btn {
    visibility: visible;
    opacity: 1;
    transform: translateY(0);
    transition: all 0.3s cubic-bezier(0.5, 0, 0, 1);
    background-color: var(--f-button-bg);
    color: var(--f-button-color);
    border: none;
    border-radius: 30px;
    padding: 10px 20px;
    font-size: 16px;
    cursor: pointer;
    display: inline-block;
    text-align: center;
}
	&lt;/style&gt;
  • 添加对应的js逻辑

JavaScript

&lt;script&gt;
	function initPage() {
            const loadMoreButton = document.getElementById(&#39;load-more-posts&#39;);
            const postsContainer = document.getElementById(&#39;popular-posts&#39;);

            if (!loadMoreButton) {
                console.error(&#39;无法获取“显示全部文章”按钮&#39;);
                return;
            }

            if (!postsContainer) {
                console.error(&#39;无法获取文章容器&#39;);
                return;
            }

            // 模拟 pjax 的 navigate 功能
  function customNavigate(url,islastpage) {
	   loadMoreButton.textContent = &quot;加载ing~&quot;;
    loadMoreButton.disabled = true;
    fetch(url)
     .then(response =&gt; response.text())
     .then(data =&gt; {
            const parser = new DOMParser();
            const doc = parser.parseFromString(data, &#39;text/html&#39;);
            const newPostsItems = doc.querySelectorAll(&#39;li.main-reveal&#39;);
            const fragment = document.createDocumentFragment();

            newPostsItems.forEach((item, index) =&gt; {
                const newLi = document.createElement(&#39;li&#39;);
                newLi.classList.add(&#39;main-reveal&#39;);
                newLi.dataset.srId = index + 1;
                newLi.style.visibility = &#39;visible&#39;;
                newLi.style.opacity = &#39;1&#39;;
                newLi.style.transform =&#39;matrix3d(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1)&#39;;
                newLi.style.transition = &#39;all, opacity 1s cubic - bezier(0.5, 0, 0, 1), transform 1s cubic - bezier(0.5, 0, 0, 1)&#39;;

                const aElement = item.querySelector(&#39;a&#39;);
                if (aElement) {
                    newLi.appendChild(aElement.cloneNode(true));
                }
                fragment.appendChild(newLi);
            });

            postsContainer.appendChild(fragment);

            // 对新插入的图片应用懒加载
            const loadingImg = &#39;修改这里为你的加载图片地址&#39;; // 假设这是加载图片的路径
            lazyLoad(loadingImg);
		//console.log(islastpage);
if (islastpage == true) {
                            loadMoreButton.textContent = &quot;已经是最后一页&quot;;
                            loadMoreButton.disabled = true;
                        } else {
							 loadMoreButton.textContent = &quot;加载更多&quot;;
                            loadMoreButton.disabled = false;
                        }

        })
     .catch(error =&gt; {
		 loadMoreButton.textContent = &quot;加载失败啦&quot;;
            console.error(&#39;加载文章时出错:&#39;, error);
            loadMoreButton.disabled = false;
        });
}
	   //结束

            loadMoreButton.addEventListener(&#39;click&#39;, function (event) {
                event.preventDefault();
				    // 从 data - page 属性获取当前页码,初始为 1
    const currentPage = parseInt(loadMoreButton.dataset.page) || 1;
				const maxPage = parseInt(loadMoreButton.dataset.max);
				//console.log(maxPage);
    const newPage = currentPage + 1;
    const newUrl =  `收纳箱/page/${newPage}/`;
				 lastpage = false;

    // 更新 data - page 属性为新的页码
    loadMoreButton.dataset.page = newPage;
				if(newPage&gt;=maxPage){
			//		console.log(&quot;MAX&quot;);
					lastpage = true;
				}
    customNavigate(newUrl,lastpage);

            });
        }

	document.addEventListener(&quot;DOMContentLoaded&quot;, function() {
       console.log(&quot;首次内容已加载&quot;);
        initPage();
    });
	 document.addEventListener(&quot;pjax:ready&quot;, function (event) {
    console.log(&quot;PJAX 内容已加载&quot;);
    initPage();

    // 清理可能重复的逻辑
    document.removeEventListener(&quot;pjax:ready&quot;, arguments.callee);
});

&lt;/script&gt;

注意修改两个部分:

1.修改const newUrl=后面的内容,这里的地址应该是你可以显示全部文章页面的url路径 我的是https://world.ccrice.com/收纳箱/所以写为const newUrl = 收纳箱/page/${newPage}/;

2.修改对新插入的图片应用懒加载部分,把const loadingImg = '修改这里为你的加载图片地址';

  • 编辑文件assets/js/theme.js

    • 找到第2455行左右的注释 //PJAX 修改他的下面一行

JavaScriptconst { Pjax } = window['pjax-api'] window.pjax = new Pjax({ areas: ['#pjax-box, #pjax-box-footer'], link: ':is(a)[href]:not([target],[data-fancybox])', form: ':is(form)[method="get"]', on: { send: function () { // 初始化时的 send 回调逻辑 }, complete: function () { // 初始化时的 complete 回调逻辑 } } })

const { Pjax } = window[&#39;pjax-api&#39;]
window.pjax = new Pjax({
  areas: [&#39;#pjax-box, #pjax-box-footer&#39;],
  link: &#39;:is(a)[href]:not([target],[data-fancybox])&#39;,
  form: &#39;:is(form)[method=&quot;get&quot;]&#39;,
	on: {
        send: function () {
            // 初始化时的 send 回调逻辑
        },
        complete: function () {
            // 初始化时的 complete 回调逻辑
        }
    }
})

是在 new Pjax前面添加了window.pjax

到这里就修改完成啦~

相关分享技术Oyiso主题魔改

最后更新于 2025-09-02

评论

共 1 条评论

  1. 小稚
    厉害啊大佬✧(≖ ◡ ≖✿)

发表评论