如何在最短和最长持续时间内使用自定义字段值过滤帖子?

时间:2014-02-01 作者:Mohammad Mursaleen

我正在写一个视频博客。在头版我有一个排序方式选项。您可以在以下链接中尝试该选项

http://cricketclips.net/

无法确定如何使最短和最长的视频选项正常工作。在db中,我有一个名为“complete”的自定义字段,其值以小数为单位(即4.23、0.26、1.28等,其中4.23=4分23秒)

最短视频;我想按照上述十进制值的升序显示视频。

对于最长的视频:希望以降序方式显示视频。

在此方面的任何帮助都将不胜感激。

1 个回复
SO网友:Mohammad Mursaleen

我使用以下代码在前端实现过滤器;

//placed this code in index.php after <?php get_header(); ?>
<?php

    $search = isset( $_GET[\'s\'] ) ? $_GET[\'s\']: null;

    if( $search ){
        $user_filtre .= \'s=\' . $_GET[\'s\'] . \'&\';
    }

    $filtre = isset( $_GET[\'filtre\'] ) ? $_GET[\'filtre\']: \'date\';
    $cat = isset( $_GET[\'cat\'] ) ? $_GET[\'cat\']: 0;

    $user_filtre = \'filtre=\' . $filtre . \'&\';

    $display = isset( $_GET[\'display\'] ) ? $_GET[\'display\']: get_option( get_theme_name() . \'_type_display\' );

    $user_display = \'display=\' . $display . \'&\';



    if( ($filtre != null) && ($cat >= 0) ) :

        $args = array(
            \'posts_per_page\'    => get_option( get_theme_name() . \'_nb_videos_listing\' ),
            \'post_type\'         => \'post\',
            \'paged\'             => $paged,
        );

        $tri = null;

        switch( $filtre ){

                case \'date\' :

                $args = array(
                    \'orderby\'           => \'date\',
                    \'order\'             => \'DESC\',
                    \'posts_per_page\'    => get_option( get_theme_name() . \'_nb_videos_listing\' ),
                    \'paged\'             => $paged,
                    \'cat\'               => $cat
                );
                break; 

                case \'views\' :

                $args = array(
                    \'meta_key\'          => \'post_views_count\',
                    \'orderby\'           => \'meta_value_num\',
                    \'order\'             => \'DESC\',
                    \'posts_per_page\'    => get_option( get_theme_name() . \'_nb_videos_listing\' ),
                    \'paged\'             => $paged,
                    \'cat\'               => $cat
                );
                break;

 //for longest videos
                case \'duree\' :

                $args = array(
                    \'meta_key\'          => \'complete\', // custom feild storing time in decimal
                    \'orderby\'           => \'meta_value_num\',
                    \'order\'             => \'DESC\',
                    \'posts_per_page\'    => get_option( get_theme_name() . \'_nb_videos_listing\' ),
                    \'paged\'             => $paged,
                    \'cat\'               => $cat
                );
                break;
    //for longest videos            
                case \'short\' :

                $args = array(
                    \'meta_key\'          => \'complete\', // custom feild storing time in decimal
                    \'orderby\'           => \'meta_value_num\',
                    \'order\'             => \'ASC\',
                    \'posts_per_page\'    => get_option( get_theme_name() . \'_nb_videos_listing\' ),
                    \'paged\'             => $paged,
                    \'cat\'               => $cat
                );
                break;

                case \'random\' :

                $args = array(
                    \'orderby\'           => \'rand\',
                    \'posts_per_page\'    => get_option( get_theme_name() . \'_nb_videos_listing\' ),
                    \'paged\'             => $paged,
                    \'cat\'               => $cat
                );
                break;
            default:

                $args = array(
                    \'posts_per_page\'    => get_option( get_theme_name() . \'_nb_videos_listing\' ),
                    \'post_type\'         => \'post\',
                    \'paged\'             => $paged
                );
                break;
        }

        $tri = /*\'\' . __(\'sorted by\', get_theme_name()) . \'\' . */corres_tv( $filtre );

        $term = $wp_query->queried_object;
        $wp_query = new WP_Query( $args );

?>
并使用以下行在前端显示筛选器列表

<ul class="filtre-list">               
<!-- for latest videos ------->     <li <?php echo ($filtre == \'date\') ? \'class="selected"\' : \'\'; ?>><a href="?<?php echo $user_display;?>filtre=date"><?php echo __(\'lastest\', get_theme_name());?></a></li>
<!-- for Most viewed videos -->     <li <?php echo ($filtre == \'views\') ? \'class="selected"\' : \'\'; ?>><a href="?<?php echo $user_display;?>filtre=views"><?php echo __(\'most viewed\', get_theme_name());?></a></li>
<!-- for longest videos ------>     <li <?php echo ($filtre == \'duree\') ? \'class="selected"\' : \'\'; ?>><a href="?<?php echo $user_display;?>filtre=duree"><?php echo __(\'longest\', get_theme_name());?></a></li>
<!-- for shortest videos ----->     <li <?php echo ($filtre == \'short\') ? \'class="selected"\' : \'\'; ?>><a href="?<?php echo $user_display;?>filtre=short"><?php echo __(\'shortest\', get_theme_name());?></a></li>
<!-- for Random videos ------->     <li <?php echo ($filtre == \'random\') ? \'class="selected"\' : \'\'; ?>><a href="?<?php echo $user_display;?>filtre=random"><?php echo __(\'random\', get_theme_name());?></a></li>
</ul><!-- .filtre-list -->
瞧!它起作用了!

结束

相关推荐

Too many actions/filters!

这里是wordpress的新成员。动作/过滤器的概念本身并不难理解。令我不知所措的是大量可用的操作和过滤器。当我阅读教程/指南时,他们会说“只需将此功能添加到wp\\U head操作或after\\U setup\\u主题”。如果没有这些教程,我究竟如何知道将该函数与该操作挂钩?作为一个初学者,我怎么会知道什么是合适的操作?有没有关于如何导航的建议?谢谢

如何在最短和最长持续时间内使用自定义字段值过滤帖子? - 小码农CODE - 行之有效找到问题解决它

如何在最短和最长持续时间内使用自定义字段值过滤帖子?

时间:2014-02-01 作者:Mohammad Mursaleen

我正在写一个视频博客。在头版我有一个排序方式选项。您可以在以下链接中尝试该选项

http://cricketclips.net/

无法确定如何使最短和最长的视频选项正常工作。在db中,我有一个名为“complete”的自定义字段,其值以小数为单位(即4.23、0.26、1.28等,其中4.23=4分23秒)

最短视频;我想按照上述十进制值的升序显示视频。

对于最长的视频:希望以降序方式显示视频。

在此方面的任何帮助都将不胜感激。

1 个回复
SO网友:Mohammad Mursaleen

我使用以下代码在前端实现过滤器;

//placed this code in index.php after <?php get_header(); ?>
<?php

    $search = isset( $_GET[\'s\'] ) ? $_GET[\'s\']: null;

    if( $search ){
        $user_filtre .= \'s=\' . $_GET[\'s\'] . \'&\';
    }

    $filtre = isset( $_GET[\'filtre\'] ) ? $_GET[\'filtre\']: \'date\';
    $cat = isset( $_GET[\'cat\'] ) ? $_GET[\'cat\']: 0;

    $user_filtre = \'filtre=\' . $filtre . \'&\';

    $display = isset( $_GET[\'display\'] ) ? $_GET[\'display\']: get_option( get_theme_name() . \'_type_display\' );

    $user_display = \'display=\' . $display . \'&\';



    if( ($filtre != null) && ($cat >= 0) ) :

        $args = array(
            \'posts_per_page\'    => get_option( get_theme_name() . \'_nb_videos_listing\' ),
            \'post_type\'         => \'post\',
            \'paged\'             => $paged,
        );

        $tri = null;

        switch( $filtre ){

                case \'date\' :

                $args = array(
                    \'orderby\'           => \'date\',
                    \'order\'             => \'DESC\',
                    \'posts_per_page\'    => get_option( get_theme_name() . \'_nb_videos_listing\' ),
                    \'paged\'             => $paged,
                    \'cat\'               => $cat
                );
                break; 

                case \'views\' :

                $args = array(
                    \'meta_key\'          => \'post_views_count\',
                    \'orderby\'           => \'meta_value_num\',
                    \'order\'             => \'DESC\',
                    \'posts_per_page\'    => get_option( get_theme_name() . \'_nb_videos_listing\' ),
                    \'paged\'             => $paged,
                    \'cat\'               => $cat
                );
                break;

 //for longest videos
                case \'duree\' :

                $args = array(
                    \'meta_key\'          => \'complete\', // custom feild storing time in decimal
                    \'orderby\'           => \'meta_value_num\',
                    \'order\'             => \'DESC\',
                    \'posts_per_page\'    => get_option( get_theme_name() . \'_nb_videos_listing\' ),
                    \'paged\'             => $paged,
                    \'cat\'               => $cat
                );
                break;
    //for longest videos            
                case \'short\' :

                $args = array(
                    \'meta_key\'          => \'complete\', // custom feild storing time in decimal
                    \'orderby\'           => \'meta_value_num\',
                    \'order\'             => \'ASC\',
                    \'posts_per_page\'    => get_option( get_theme_name() . \'_nb_videos_listing\' ),
                    \'paged\'             => $paged,
                    \'cat\'               => $cat
                );
                break;

                case \'random\' :

                $args = array(
                    \'orderby\'           => \'rand\',
                    \'posts_per_page\'    => get_option( get_theme_name() . \'_nb_videos_listing\' ),
                    \'paged\'             => $paged,
                    \'cat\'               => $cat
                );
                break;
            default:

                $args = array(
                    \'posts_per_page\'    => get_option( get_theme_name() . \'_nb_videos_listing\' ),
                    \'post_type\'         => \'post\',
                    \'paged\'             => $paged
                );
                break;
        }

        $tri = /*\'\' . __(\'sorted by\', get_theme_name()) . \'\' . */corres_tv( $filtre );

        $term = $wp_query->queried_object;
        $wp_query = new WP_Query( $args );

?>
并使用以下行在前端显示筛选器列表

<ul class="filtre-list">               
<!-- for latest videos ------->     <li <?php echo ($filtre == \'date\') ? \'class="selected"\' : \'\'; ?>><a href="?<?php echo $user_display;?>filtre=date"><?php echo __(\'lastest\', get_theme_name());?></a></li>
<!-- for Most viewed videos -->     <li <?php echo ($filtre == \'views\') ? \'class="selected"\' : \'\'; ?>><a href="?<?php echo $user_display;?>filtre=views"><?php echo __(\'most viewed\', get_theme_name());?></a></li>
<!-- for longest videos ------>     <li <?php echo ($filtre == \'duree\') ? \'class="selected"\' : \'\'; ?>><a href="?<?php echo $user_display;?>filtre=duree"><?php echo __(\'longest\', get_theme_name());?></a></li>
<!-- for shortest videos ----->     <li <?php echo ($filtre == \'short\') ? \'class="selected"\' : \'\'; ?>><a href="?<?php echo $user_display;?>filtre=short"><?php echo __(\'shortest\', get_theme_name());?></a></li>
<!-- for Random videos ------->     <li <?php echo ($filtre == \'random\') ? \'class="selected"\' : \'\'; ?>><a href="?<?php echo $user_display;?>filtre=random"><?php echo __(\'random\', get_theme_name());?></a></li>
</ul><!-- .filtre-list -->
瞧!它起作用了!

相关推荐

添加到数组并通过do_action/Apply_Filters传递它

作为练习,我正在使用PHPclass to add meta boxes 我在GitHub上找到了。我只是复制了代码,现在我正在玩它来理解它。其工作原理如下:包含该类的文件包含在init中。在该文件内部,但在类外部,有一个空数组$meta_boxes 已初始化之后,使用apply_filters. 我猜是apply_filters 使用而不是do_action 因为后者不返回任何内容——$meta_boxes = apply_filters( \'cmb_meta_boxes\', $meta_boxes