6

我正在开发一个网络服务(API),我正在获取结果 WP_query() 函数并以 JSON 格式解析它。这将在android应用程序中进一步使用。问题是我通过查询得到的 post_content 是由视觉作曲家组成的,整个内容都是以这样的标签的形式出现的

[VC_ROW][/VC_ROW][VC_COLUMN]some text[/VC_COLUMN] etc.

我想从内容中删除/剥离所有这些短代码,并只从中检索纯文本。是否有任何视觉作曲家功能可以通过它来实现这件事

<?php
require('../../../wp-load.php');
require_once(ABSPATH . 'wp-includes/functions.php');
require_once(ABSPATH . 'wp-includes/shortcodes.php');
header('Content-Type: application/json');

$post_name = $_REQUEST['page'];

if($post_name!=''){
    if($post_name=='services') {

    $args = array(
        'post_parent' => $page['services']['id'],
        'post_type'   => 'page', 
        'post_status' => 'published' 
    ); 
    $posts = get_children($args);
    foreach($posts as $po){
        $services_array[] = array('id'=>$po->ID,'title'=>$po->post_title,'image'=>get_post_meta($po->ID, 'webservice_page_image',true),'description'=>preg_replace("~(?:\[/?)[^/\]]+/?\]~s", '', $po->post_content));
    }

    $post = array(
        'status'=>'ok', 
        'services'=>$services_array
    );
    echo json_encode($post);
}
}
?>
4

7 回答 7

4

I want to remove/strip all these shortcode from the content and retrieve only plain text from it.

对我有用的解决方案:

$content = strip_tags( do_shortcode( $post->post_content ) );

do_shortcode触发所有可视化作曲家简码,从而返回 html+text;

strip_tags删除所有 html 标签并返回纯文本。

于 2020-12-04T21:51:27.747 回答
2

在这里,您可以尝试轻松地在数组中添加一些您需要的短代码,也可以通过以下代码删除所有短代码。

$the_content = '[VC_ROW][VC_COLUMN]some text1[/VC_COLUMN] etc.[/VC_ROW][VC_COLUMN_INNTER width="1/3"][/VC_COLUMN_INNTER]';

$shortcode_tags = array('VC_COLUMN_INNTER');
$values = array_values( $shortcode_tags );
$exclude_codes  = implode( '|', $values );

// strip all shortcodes but keep content
// $the_content = preg_replace("~(?:\[/?)[^/\]]+/?\]~s", '', $the_content);

// strip all shortcodes except $exclude_codes and keep all content
$the_content = preg_replace( "~(?:\[/?)(?!(?:$exclude_codes))[^/\]]+/?\]~s", '', $the_content );
echo $the_content;

您想保留一些不能strip_shortcodes()用于此目的的简码。

于 2016-08-04T10:49:05.077 回答
1

最佳解决方案,解决。
只需将以下代码添加到文件wp-includes/rest-api.php的底部:

/**
 * Modify REST API content for pages to force
 * shortcodes to render since Visual Composer does not
 * do this
 */
add_action( 'rest_api_init', function ()
{
   register_rest_field(
          'page',
          'content',
          array(
                 'get_callback'    => 'compasshb_do_shortcodes',
                 'update_callback' => null,
                 'schema'          => null,
          )
       );
});

function compasshb_do_shortcodes( $object, $field_name, $request )
{
   WPBMap::addAllMappedShortcodes(); // This does all the work

   global $post;
   $post = get_post ($object['id']);
   $output['rendered'] = apply_filters( 'the_content', $post->post_content );

   return $output;
}
于 2019-03-09T09:10:19.180 回答
0

我把它带到某个地方并对其进行了一些更新,以便更好地工作:)。在functions.php中添加这个函数:

/** Function that cuts post excerpt to the number of a word based on previously set global * variable $word_count, which is defined below */

if(!function_exists('kc_excerpt')) {

  function kc_excerpt($excerpt_length = 20) {

    global $word_count, $post;

    $word_count = $excerpt_length;

    $post_excerpt = get_the_excerpt($post) != "" ? get_the_excerpt($post) : strip_tags(do_shortcode(get_the_content($post)));

    $clean_excerpt = strpos($post_excerpt, '...') ? strstr($post_excerpt, '...', true) : $post_excerpt;

    /** add by PR */

    $clean_excerpt = strip_shortcodes(remove_vc_from_excerpt($clean_excerpt));
    /** end PR mod */

    $excerpt_word_array = explode (' ',$clean_excerpt);

    $excerpt_word_array = array_slice ($excerpt_word_array, 0, $word_count);

    $excerpt = implode (' ', $excerpt_word_array).'...'; echo ''.$excerpt.'';

  }
}

然后你正常调用它kc_excerpt(20);,它会返回正常的 post_content/excerpt

于 2019-03-22T12:43:55.437 回答
0

如何从 wp 帖子中删除可视化作曲家:即[vc_row][vc_column width=\"2/3\"][distance][vc_single_image image=\"40530\" img_size=\"large\"][distance][distance][distance][vc_column_text] WP 帖子也可以删除短代码和 html 标签。

while($posts->have_posts()) {
      $postContent = get_the_content();
      //Remove html tags. and short code 
      $postContent = strip_tags( do_shortcode( $postContent ) );
      //Remove visual composer tags [vc_column] etc
      $postContent = preg_replace( "/\[(\/*)?vc_(.*?)\]/", '', $postContent );
}
于 2021-01-13T10:28:00.547 回答
0

从 wordpress api 中查看源代码,我做了一个从内容中删除一些短代码的功能。所以这是结果:

function removeShortcode($content, $shortcodeList){
  preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches);
  $tagnames = array_intersect($shortcodeList, $matches[1]);
  if(empty($tagnames)){
    return $content;
  }
  $pattern = get_shortcode_regex($tagnames);
  preg_match_all("/$pattern/", $content, $matchesTags);
  foreach ($matchesTags[0] as $key => $value) {
    $content = str_replace($value, $matchesTags[5][$key], $content);
  }
  return $content;
}

例子:

$content = "<p>Hi, this is a [example]<b>example</b>[/example]. [end]</p>";
$shortcodesToRemove = ["example", "end"];
echo removeShortcode($content, $shortcodesToRemove);
于 2021-06-19T06:34:17.503 回答
0
foreach($posts as $po){
    $services_array[] = array('id'=>$po->ID,'title'=>$po->post_title, 'description'=>do_shortcode($po->post_content));
}
  • 你应该试试这个。
于 2021-06-25T06:51:34.873 回答