【问题标题】:Display a specific Text block from all posts显示所有帖子中的特定文本块
【发布时间】:2020-07-20 20:54:07
【问题描述】:

我正在使用 Wordpress 最新版本、子主题和 Visual Composer WPBakery Page Builder。

我正在使用WP_Query 将我的博客文章列到模板 php 文件中,并且在该页面中我需要:

  1. 显示特色图片(有标题) - 完成,
  2. 发布日期 - 完成,
  3. 只是帖子内容中的一个文本块 - 一个特定的行,里面有一个带有文本块的列。这个文本块有一个类名:bblog-text-row。

我只需要回显/显示/显示该文本,但是当我执行 the_content (); 时,它会将 VC 简码连续输出为带有简码和文本的原始文本。我还需要将此文本限制为 250 个字符。

这是我为内容部分准备的代码:

<?php
$query = new WP_Query( array( 'post_type' => 'post' ) );
$posts = $query->posts;
foreach($posts as $post) {
?>
  <p>
   <?php
      $char_limit = 250; //character limit
      $content = $post->post_content; //contents saved in a variable
      echo substr(strip_tags($content), 0, $char_limit);
   ?>
  </p>
<?php
    }
?>

先谢谢你了。

H

【问题讨论】:

    标签: php wordpress visual-composer wpbakery


    【解决方案1】:

    我找到了一个小解决方案,将使用 WPBakery Page Builder 制作的帖子内容放入列出所有帖子的 php 模板文件中 - 就像博客页面一样。可能有更聪明和更好的方法可以做到这一点,但这解决了我的问题。

    <?php
    $the_query = new WP_Query( array( 'post_type' => 'post' ) );
    while ($the_query->have_posts() ) {
        $the_query->the_post(); ?>
    <!-- you may add more post content here
    I just want to refer to the content, not meta content or custom fields -->
     <p class="bowe-post-content-bloc">
      <?php
        $conteudo = get_the_content(); //get ALL content for the post
        $padrao = array('(\[vc(.?)\])', '(\[/vc(.?)\])','(\[booom_baannng_anypostsharebuttons])'); //create a filter
        $reparacao = preg_replace ($padrao, "",$conteudo ); //remove content based on filter
        echo wp_trim_words ( $reparacao, 40, '...'); //display only 40 words
      ?>
     </p>
    <!-- you may add more post content here
    I just want to refer to the content, not meta content or custom fields -->
    <?php
    }
    wp_reset_postdata(); ?>
    

    【讨论】:

      猜你喜欢
      • 2013-11-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-06
      • 2015-08-26
      相关资源
      最近更新 更多