【问题标题】:Sorting custom post type using two taxononmies使用两种分类法对自定义帖子类型进行排序
【发布时间】:2014-07-17 20:20:18
【问题描述】:

我无法弄清楚如何对我的自定义帖子类型进行排序,有 2 个分类法。

分类法 1) 位置(即纽约多伦多)- slug:位置 分类法 2) 贸易(即木匠、电工) - 蛞蝓:贸易

查看多伦多页面时(使用我的分类模板:taxonomy-location.php),我希望能够对多伦多列出的所有交易进行排序。它应该看起来像这样:

——

多伦多(页面标题)

木匠

木匠 1 姓名,木匠 2 姓名,木匠 3 姓名,木匠 4 姓名

电工

电工 1 姓名、电工 2 姓名、电工 3 姓名、电工 4 姓名

——

我正在我的自定义分类模板 (taxonomy-location.php) 中创建查询循环,因此我假设显示的帖子已经在多伦多位置进行了标记,但情况似乎并非如此.

这是我的 taxonomy-location.php 文件中的循环:

<?php
$args=array(
‘trade’ => ‘carpenters’,
'post_type' => 'post_type_name',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo 'Carpenters';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
<?php
endwhile;
}
wp_reset_query();  // Restore global post data stomped by the_post().
?>

如果我在第一个数组中添加位置分类的另一个参数,它可以工作——但我不希望它仅限于多伦多。我希望它从您正在查看的任何位置页面(即纽约、多伦多等)中提取。

<?php
$args=array(
‘trade’ => ‘carpenters’,
'location' => ‘toronto’, // this should be something like this <?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); echo $term->name; ?>
'post_type' => 'post_type_name',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo 'Carpenters';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
<?php
endwhile;
}
wp_reset_query();  // Restore global post data stomped by the_post().
?>

最好的方法是什么?

【问题讨论】:

    标签: php wordpress custom-post-type taxonomy


    【解决方案1】:

    您可以使用get_query_var 获取位置信息,但您必须先注册它才能让 Wordpress 访问。您必须对所有自定义变量执行此操作:

    function add_query_vars_filter( $vars ){
      $vars[] = "location";
      return $vars;
    }
    add_filter( 'query_vars', 'add_query_vars_filter' );
    

    【讨论】:

    • 我不太明白如何使用您在循环中提供的这段代码。你能澄清一下吗?
    • 您可以将该代码添加到您的函数文件中,这将允许您在location 变量上使用get_query_var
    • 一旦将其添加到我的函数文件中,我将如何在循环中使用该变量?
    • $myvariable = get_query_var('location'); 然后您可以在分类查询中使用它
    猜你喜欢
    • 2014-07-21
    • 2014-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多