dongliaojing0554 2012-03-24 02:18
浏览 49

Wordpress:单击按钮/链接时动态排序内容

I need a little help with some functionality. I am trying to create an Post Page that can be sort dynamically to show what posts the user would like to see

Use Scenario:

In the Post, there are 4 title ei: Title A, Title B, Title C and Title D.

Title A = CARAMOAN A LOOK BACK;

Title B = DESTINATION: PUERTO PRINCESA;

Title C = THE THINGS A FREE ROUND-TRIP TICKET CAN MAKE YOU SEE;

Title D = FIVE THINGS TO REMEMBER WHEN TRAVELING IN THE PHILIPPINES;

Now with this I want to sort by:

Most Comment

Most Viewed

Alphabetical - Ascending Order

which the user can change the page order by clicking on a Button that would look something like this.

enter image description here

  • 写回答

1条回答 默认 最新

  • douhan1860 2012-07-07 00:58
    关注

    The easiest way for that would be using a query string.

    I'll assume that you are looking at a category page.

    First we will add the buttons to the category page

    <a href="?sortby=comment>Sort by Comment</a>
    <a href="?sortby=views>Sort by Views</a>
    <a href="?sortby=alphabet>Alphabetical</a>
    

    This adds a query string to the URL of the page, now on the top of the category page we will add this code:

    <?php
    if (array_key_exists("sortby", $_GET) === true)
    {
        $newQuery = sortIt($_GET['sortby']);
    }
    ?>
    

    After that we will create a function that will sort the posts for us in the functions.php template

    Since we have 3 sort types, we could use a switch case or a if-else statement. I will use if-else here.

    <?php
    function sortIt($sortType)
    {
        global $wp_query;
        $cat_ID = get_query_var('cat');
    
        if (strcmp($sortType, 'comment') == 0 )
        {
            $newQuery = new WP_Query( array( 'orderby' => 'comment_count' , 'cat' => $cat_ID, 'posts_per_page' => '10') );
        }
    
        if (strcmp($sortType, 'views') == 0 )
        {
            $newQuery = new WP_Query( array( 'meta_key' => 'views', 'orderby' => 'meta_value_num', 'order'=> 'DESC', 'cat' => $cat_ID, 'posts_per_page' => '10') );
        }
    
        if (strcmp($sortType, 'alphabetical') == 0 )
        {
            $newQuery = new WP_Query( array( 'orderby' => 'title' , 'cat' => $cat_ID, 'posts_per_page' => '10') );
        }
    
        return $newQuery;
    }
    ?>
    

    Wordpress does not have a native view count, I used an instruction that i read here.

    Since we have all the necessary functions and variables, we need to override the query.

    You will edit the loop so it goes like this:

    <?php if ( $newQuery->have_posts() ) : while ( $newQuery->have_posts() ) : $newQuery->the_post(); ?>
    

    That's it :)

    评论

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本