在WP查询中输入变量时出现问题

时间:2021-11-08 作者:ukijake

不确定描述这一点的最佳方式,但基本上我定义了一个变量($cat),它根据在调用查询之前定义的数字来选择分类法,但由于该数字是在args之后定义的,所以我不知道如何使其工作。我知道我可以为我想要的每个分类法复制整个查询,但我希望有一种不太重字符的方法来实现这一点。

当前代码:

//cat changer (using a settings page so non-dev colleagues can switch taxonomies on the homepage)
if ( $cat == 1 ) { $sector = get_option(\'b4_settings_homepage_category_1\')[0]; }
if ( $cat == 2 ) { $sector = get_option(\'b4_settings_homepage_category_2\')[0]; }
if ( $cat == 3 ) { $sector = get_option(\'b4_settings_homepage_category_3\')[0]; }
if ( $cat == 4 ) { $sector = get_option(\'b4_settings_homepage_category_4\')[0]; }
if ( $cat == 5 ) { $sector = get_option(\'b4_settings_homepage_category_5\')[0]; }
if ( $cat == 6 ) { $sector = get_option(\'b4_settings_homepage_category_6\')[0]; }
if ( $cat == 8 ) { $sector = get_option(\'b4_settings_homepage_category_7\')[0]; }
if ( $cat == 9 ) { $sector = get_option(\'b4_settings_homepage_category_8\')[0]; }
if ( $cat == 10 ) { $sector = get_option(\'b4_settings_homepage_category_9\')[0]; }
if ( $cat == 11 ) { $sector = get_option(\'b4_settings_homepage_category_10\')[0]; }
if ( $cat == 12 ) { $sector = get_option(\'b4_settings_homepage_category_11\')[0]; }
if ( $cat == 13 ) { $sector = get_option(\'b4_settings_homepage_category_12\')[0]; }
if ( $cat == 14 ) { $sector = get_option(\'b4_settings_homepage_category_13\')[0]; }
//that \'get_option\' outputs as the plain ID of the taxonomy

//args & query
$args_cat = array( 
   \'post_type\' => $post_types,
   \'posts_per_page\' => 3,
   \'fields\' => \'ids\',
   \'post__not_in\' => array( $feat_ids, $latest_ids, $trend_ids ),
   \'tax_query\' => array( 
      \'relation\' => \'AND\',
      array(
         \'taxonomy\' => \'sector\',
         \'field\' => \'id\',
         \'terms\' => $sector
      )
   )
);
$cat_query = new WP_Query( $args_cat );

//choosing the category and hoping to run the query with it (not working)
$cat = 1;
while ( $cat_query->have_posts() ) { 
   $cat_query->the_post();
   get_template_part( \'template-parts/posts/content\', get_post_type() );
   wp_reset_postdata();
}
因此,就像我希望它返回到页面顶部,根据数字选择cat,然后使用该选项运行查询,但我就是搞不清楚。按原样运行会返回“术语”=>;空值。我需要重置或返回还是什么?

非常感谢您的帮助。

顺便说一下,我知道我可以对每个税务/查询这样做:

$sector = get_option(\'b4_settings_homepage_category_1\')[0];
$args_cat = array( 
   \'post_type\' => $post_types,
   \'posts_per_page\' => 3,
   \'fields\' => \'ids\',
   \'post__not_in\' => array( $feat_ids, $latest_ids, $trend_ids ),
   \'tax_query\' => array(
      \'relation\' => \'AND\',
      array( 
         \'taxonomy\' => \'sector\',
         \'field\' => \'id\',
         \'terms\' => $sector
      )
   )
);
$cat_query = new WP_Query( $args_cat );
while ( $cat_query->have_posts() ) {
$cat_query->the_post(); 
etc etc
但如果可以避免的话,我真的不想让这个查询重复很多次。。。

4 个回复
最合适的回答,由SO网友:ukijake 整理而成

将Petar的函数和Tiago的缩写选项语法结合起来,可以:

在模板中:

//once, in header:
$i = 0;

//anytime I want to run the query:
$i++;
$cat_query = prefix_get_wp_query($i);
 while ( $cat_query->have_posts() ) {
  $cat_query->the_post();
  get_template_part( \'template-parts/posts/content\', get_post_type() );
  wp_reset_postdata();
}
在函数中:

function prefix_get_wp_query( $i ) {
 if ( $i > 0 ) { 
  $cat = "b4_settings_homepage_category_".$i;
  $sector = get_option($cat)[0];
 }
 $cat_args = [
  \'post_type\'      => $post_types,
  \'posts_per_page\' => 3,
  \'fields\'         => \'ids\',
  \'post__not_in\'   => [ $feat_ids, $latest_ids, $trend_ids ],
  \'tax_query\'      => [
   \'relation\' => \'AND\',
    [ \'taxonomy\' => \'sector\',
      \'field\'    => \'id\',
      \'terms\'    => $sector
    ]
   ]
  ];
  return new WP_Query( $cat_args );
}
非常感谢!!

SO网友:Petar Petrov

为了减少代码重复,可以引入一个函数。下面的代码示例是您提供的代码的重写版本,但它使用了一个函数。如果要将此用于类别2, 只需使用参数调用函数2.

请注意,您需要声明在函数开头使用的变量。此外,还包括get_option 函数可以返回的不仅仅是一个数组,这将引起一个通知。

$cat_query = prefix_get_wp_query(1);
while ( $cat_query->have_posts() ) {
    $cat_query->the_post();
    get_template_part( \'template-parts/posts/content\', get_post_type() );
    wp_reset_postdata();
}

function prefix_get_wp_query( $cat ) {
    // declare $post_types, $feat_ids, $latest_ids and $trend_ids below this line

    //cat changer (using a settings page so non-dev colleagues can switch taxonomies on the homepage)
    if ( $cat == 1 ) { $sector = get_option(\'b4_settings_homepage_category_1\')[0]; }
    if ( $cat == 2 ) { $sector = get_option(\'b4_settings_homepage_category_2\')[0]; }
    if ( $cat == 3 ) { $sector = get_option(\'b4_settings_homepage_category_3\')[0]; }
    if ( $cat == 4 ) { $sector = get_option(\'b4_settings_homepage_category_4\')[0]; }
    if ( $cat == 5 ) { $sector = get_option(\'b4_settings_homepage_category_5\')[0]; }
    if ( $cat == 6 ) { $sector = get_option(\'b4_settings_homepage_category_6\')[0]; }
    if ( $cat == 8 ) { $sector = get_option(\'b4_settings_homepage_category_7\')[0]; }
    if ( $cat == 9 ) { $sector = get_option(\'b4_settings_homepage_category_8\')[0]; }
    if ( $cat == 10 ) { $sector = get_option(\'b4_settings_homepage_category_9\')[0]; }
    if ( $cat == 11 ) { $sector = get_option(\'b4_settings_homepage_category_10\')[0]; }
    if ( $cat == 12 ) { $sector = get_option(\'b4_settings_homepage_category_11\')[0]; }
    if ( $cat == 13 ) { $sector = get_option(\'b4_settings_homepage_category_12\')[0]; }
    if ( $cat == 14 ) { $sector = get_option(\'b4_settings_homepage_category_13\')[0]; }
    //that \'get_option\' outputs as the plain ID of the taxonomy

    //args & query
    $args_cat = array(
        \'post_type\'      => $post_types,
        \'posts_per_page\' => 3,
        \'fields\'         => \'ids\',
        \'post__not_in\'   => array( $feat_ids, $latest_ids, $trend_ids ),
        \'tax_query\'      => array(
            \'relation\' => \'AND\',
            array(
                \'taxonomy\' => \'sector\',
                \'field\'    => \'id\',
                \'terms\'    => $sector
            )
        )
    );

    return new WP_Query( $args_cat );
}

SO网友:tiago calado

如果第二个代码工作正常,只需执行以下操作

$option = "b4_settings_homepage_category_".$cat;
$sector = get_option($option)[0];
这将使您可以选择cat变量,例如b4\\u settings\\u homepage\\u category\\u 7,它与if statment相同,但更加优雅。此外,您的第一个代码可能不起作用,因为$cat变量应该在if stations之前,而不是之后。

因此,inteire代码应为:

$cat = 7;
$option = "b4_settings_homepage_category_".$cat;
$sector = get_option($option)[0];
$args_cat = array(
   \'post_type\' => $post_types,
   \'posts_per_page\' => 3,
   \'fields\' => \'ids\',
   \'post__not_in\' => array( $feat_ids, $latest_ids, $trend_ids ),
   \'tax_query\' => array(
      \'relation\' => \'AND\',
      array(
         \'taxonomy\' => \'sector\',
         \'field\' => \'id\',
         \'terms\' => $sector
      )
   )
);
$cat_query = new WP_Query( $args_cat );
while ( $cat_query->have_posts() ) {
    $cat_query->the_post();
    // etc etc
}

SO网友:tiago calado

您的正确答案可能会发现一个问题,当$i小于或等于零时,将出现错误,即$扇区是第一次使用的,因此防止这种情况的一种方法是

function prefix_get_wp_query( $i ) {
 if ( $i > 0 ) { 
  $cat = "b4_settings_homepage_category_".$i;
  $sector = get_option($cat)[0];
  $cat_args = [
  \'post_type\'      => $post_types,
  \'posts_per_page\' => 3,
  \'fields\'         => \'ids\',
  \'post__not_in\'   => [ $feat_ids, $latest_ids, $trend_ids ],
  \'tax_query\'      => [
   \'relation\' => \'AND\',
    [ \'taxonomy\' => \'sector\',
      \'field\'    => \'id\',
      \'terms\'    => $sector
    ]
   ]
  ];
  return new WP_Query( $cat_args );
 } else {
  return 0;
 }
}
如果您知道它将始终大于0,则可以简单地删除if

function prefix_get_wp_query( $i ) {
  $cat = "b4_settings_homepage_category_".$i;
  $sector = get_option($cat)[0];
  $cat_args = [
  \'post_type\'      => $post_types,
  \'posts_per_page\' => 3,
  \'fields\'         => \'ids\',
  \'post__not_in\'   => [ $feat_ids, $latest_ids, $trend_ids ],
  \'tax_query\'      => [
   \'relation\' => \'AND\',
    [ \'taxonomy\' => \'sector\',
      \'field\'    => \'id\',
      \'terms\'    => $sector
    ]
   ]
  ];
  return new WP_Query( $cat_args );
}

相关推荐

PHP警告:试图读取bool上的属性“Term_id”

自从从PHP 7.4更新到PHP 8以来,我的error_log 现在,我使用的一个儿童主题中充满了这样的行:【2021 10月17日13:30:38 UTC】PHP警告:尝试读取属性;term\\u id;在bool-in/public\\u html/wp内容/主题/主题名子项/函数。php在线181这是抛出错误的块。它用于修改分页,以便不导航特定类型(链接)的帖子。// modify pagination to exclude post_format of \'link\' on news/blog