WordPress:获取嵌套短代码中自定义 post 类型的 ID

WordPress: Get ID of custom post type within nested shortcodes

我目前无法从嵌套简码中获取自定义 post 类型的 ID。我不确定这是否可行。

描述我的场景的更多信息:

我遇到的问题是我试图从 Textarea 短代码中获取 "Activity ID",但我想不出这样做的方法。

请通过此 link 查看图表以更好地描述我的情况:

https://www.dropbox.com/s/a9bgyjdq9m92qpg/WP-Support---Get-ID-within-Nested-Shortcodes.png?dl=0

Activity 简码代码:

function display_activity($atts, $content = null){

        $a = shortcode_atts( array(
            'id' => null,
        ), $atts ) ;

        if (!is_null($a['id'])){

            $activityId = intval($a['id']);

            if (is_int($activityId) && $activityId > 0){

                $activityObj = get_post($activityId);
                $theTitle = sanitize_post_field( 'post_title', $activityObj->post_title, $activityId, 'display' );
                $theContent = apply_filters('the_content', $activityObj->post_content);

                $html = "<div id='sbusuws-activity-" . $activityId . "' class='sbusuws-activity'>";

                $html .= "<h3 class='sbusuws-activity-title'>" . $theTitle . "</h3>";

                $html .= "<div class='sbusuws-activity-content'>";
                $html .= do_shortcode($theContent);
                $html .= "</div>";

                $html .= "</div>";

                return $html;

            }

        }

    }

文本区域简码:

function display_textarea($atts){

$activityId = ""; // <--- This is the problem
    $textareaId = $activityId . "-sbusuws-activity-textarea";
    $html = "<textarea id='" . $textareaId . "' rows='5' cols='20'>";

    return $html;

}

The idea was to make the textarea shortcode as simple as possible, ie: [textarea]

对于此问题的任何帮助,我将不胜感激。

谢谢!

在你的 display_activity 方法中试试这个,如果它适合你..

$theContent = str_replace('[textarea]','[textarea id="'.$activityId.'"]', $theContent); 

$html .= "<div class='sbusuws-activity-content'>";
$html .= do_shortcode($theContent);
$html .= "</div>";