Solve the pageSize problem of multiple widgets under Typecho
in Tutorial with 15 comments
Solve the pageSize problem of multiple widgets under Typecho
in Tutorial with 15 comments

Recently, when developing Pinghsu theme, I encountered a thorny problem, that is, calling the Widget_Contents_Post_Recent If a page calls this for the next two times, the pageSize The parameter value is the maximum of the two values by default.

At present, there is no solution to this problem in online search, so you can only think about it yourself. It doesn't matter, just think about it yourself.

Let me tell you how I solved it.

Then write a function function to call···

Resolution process

In the subject function.php The page creates a new function called getRecentPosts, as shown below

 function getRecentPosts(){ }

Take two parameters for this function. One is used to pass in the current object. Let's call obj as follows

 function getRecentPosts($obj){ }

OK, start to connect to the database and get the cid of the last 10 articles, as follows

 $db = Typecho_Db::get(); $rows = $db->fetchAll($db->select('cid') ->from('table.contents') ->where('type = ? AND status = ?', 'post', 'publish') ->order('created', Typecho_Db::SORT_DESC) ->limit(10));

After obtaining the cid of the article, it is easy to do. Set a cycle and analyze one by one, as follows

 foreach($rows as $row){ $cid = $row['cid']; $apost = $obj->widget(' Widget_Archive@post_ '.$ cid, 'type=post', 'cid='.$ cid); $output = '<li><a href="'.$apost->permalink .'">'. $ apost->title .'</ a></li>'; echo $output; }

Then sort it out, and finally

 function getRecentPosts($obj){ $db = Typecho_Db::get(); $rows = $db->fetchAll($db->select('cid') ->from('table.contents') ->where('type = ? AND status = ?', 'post', 'publish') ->order('created', Typecho_Db::SORT_DESC) ->limit(10)); foreach($rows as $row){ $cid = $row['cid']; $apost = $obj->widget(' Widget_Archive@post_ '.$ cid, 'type=post', 'cid='.$ cid); $output = '<li><a href="'.$apost->permalink .'">'. $ apost->title .'</ a></li>'; echo $output; } }

Then add a piece of code where we need it to execute, as follows

 <? php getRecentPosts($this); ?>

Optimization function

I don't think it's convenient to use this way. Let's add another one pageSize The parameter is used to control the number of recent articles. It is modified as follows

 function getRecentPosts($obj,$pageSize){ Omission··· ->limit($pageSize)); Omission··· }

Well, after so much water, the final code is as follows

 function getRecentPosts($obj,$pageSize){ $db = Typecho_Db::get(); $rows = $db->fetchAll($db->select('cid') ->from('table.contents') ->where('type = ? AND status = ?', 'post', 'publish') ->order('created', Typecho_Db::SORT_DESC) ->limit($pageSize)); foreach($rows as $row){ $cid = $row['cid']; $apost = $obj->widget(' Widget_Archive@post_ '.$ cid, 'type=post', 'cid='.$ cid); $output = '<li><a href="'.$apost->permalink .'">'. $ apost->title .'</ a></li>'; echo $output; } }

The usage is almost the same as above, except that there are more parameters, as follows

 <? php getRecentPosts($this,10); ?>

test

It seems that there is nothing to test Widget_Contents_Post_Recent This function is called under the page of pageSize The problem of taking the maximum value of.

So far, the problem has been solved.

If you encounter more than one Widget Of pageSize This tutorial should help you.

Responses
  1. It's been a long time. The new theme is very good

    Reply
    1. @benzBrake

      Thank you. Welcome to visit 😄

      Reply
  2. lonelypers

    The theme is very beautiful

    Reply
    1. @lonelypers

      thank you, 😜 😜

      Reply
  3. Can you use what you are using in your blog? I like it too much; Can sponsor if necessary

    Reply
    1. @timez

      You'd better wait. Several functions have not been added and logo customization has not yet been done.

      Reply
  4. Just say it here http://timez.me/index.php/archives/12/

    Reply
    1. @timez

      You can use my new theme after I have solved the new theme and opened it.

      Reply
  5. Will the page be too white~tired eyes after watching it for a long time

    Reply
    1. Chakhsu
      @Thirteen

      It's solved. Let's see how it works.

      Reply
      1. @Chakhsu

        The blogger has another problem, which is a bug. After opening the menu on the mobile phone, you cannot close the menu

        Reply
        1. @Thirteen

          In fact, the whole header is implemented by css. I'm still looking for a method for this problem. If it's really impossible, I can directly use js to implement it. 😕

          Reply
  6. The theme is beautiful. How to realize the floating of the first page article

    Reply
    1. @timez

      What emerges?

      Reply