dongye9228 2016-10-19 15:52
浏览 107
已采纳

Wordpress get_posts()类别参数问题

I'm showing a grid of posts with get_posts() function and trying to control category parameter value from a custom field.

my code works if category parameter gets value from echo function for example:

$args = array( 
'numberposts' => 6,
'category'         => '<?php echo "25";?>'
,);

but it doesn't when I do something like

$catid = get_field( "id_of_product_category" );
$args = array( 
'numberposts' => 6,
'category'         => '<?php echo $catid;?>'
,);

at the same time I'am able to

echo $catid;

on the page and it does show correct value set in custom field.

Why I can't get it to work to change category parameter then?

  • 写回答

1条回答 默认 最新

  • dongpanbo4727 2016-10-19 16:09
    关注

    I just realized that your get_field() is missing a second parameter. If you don't specify the specific id of the post that has a field with that name, it will most likely default to the current post's value if it exists. So you basically need to loop through your get_posts() to get the current post that would have the value you are seeking, then do the functionality you are looking for. Exposing more of your could would be helpful.

    // Get your custom field from ACF based on field name and the ID of the post
    $catid = get_field( "id_of_product_category", $post->ID );
    
    $args = array( 
    'numberposts' => 6,
    'category'    => $catid
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝