简体   繁体   中英

How to use shortcode in php code to get value via shortcode for php code?

I'm trying to get postid via shortcode to get parent category of that post. Here is my code

I tried this code for now but not get any luck, I don't know what is going wrong. Please consider my request I'm very new to PHP and WordPress.

<?php
    $category_detail=get_the_category('[field parent-id]');//$post->ID
    foreach($category_detail as $cd){
        echo $cd->cat_name;
    }
?> 

I want to print my post id like this

$category_detail=get_the_category('4');//$post->ID

I can't use PHP code to get post id as my other format is make with shortcodes so please help me in this. Thanks (Sorry for poor English)

Update : I tried this code also but no luck

<?php
    $id = do_shortcode('[field parent-id]');

    $category_detail=get_the_category($id);//$post->ID
    foreach($category_detail as $cd){
        echo $cd->cat_name;
    }
?> 

The problem is this shortcode is trying to get the parent-id, but the parent ID of what? You need to tell It, so try this (untested)

global $post;
$parent = $post->post_parent;
$id = do_shortcode('[field '.$parent.']');
$category_detail=get_the_category($id);//$post->ID
foreach($category_detail as $cd){
  echo $cd->cat_name;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM