【问题标题】:get_the_content not working within shortcodeget_the_content 在短代码中不起作用
【发布时间】:2014-09-18 22:23:13
【问题描述】:

我为 wordpress 编写的简码存在问题。我正在尝试使用get_the_content() 函数,但不是提取我创建的自定义帖子类型的内容,而是提取简码所在页面的内容。其他功能都可以正常工作,例如get_the_title() 和 get_the_post_thumbnail()。我在函数中传递了 ID,它适用于其他一切,但不是 get_the_content

get_the_content($testimonial_item->ID);

短代码包含分页和其他元素都可以正常工作,只是这一个功能让我很伤心。完整代码如下,任何帮助将不胜感激。

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

extract( shortcode_atts( array(
    'per_page' => 6
), $atts ) );

/* pagination parameters */

// check what page we are on
if ( isset ($_GET['testimonial-page'] ) ) $page = $_GET['testimonial-page']; else $page = 1;

// default number of pages
$total_pages = 1;

// portfolio offset. Used in the get_posts() query to show only portfolio for the current page
$offset = $per_page * ($page-1);

// setup the portfolio args for retrieving the total number of portfolio items
$testimonial_count_args = array(
    'post_type' => 'ncweb-testimonials',
    'posts_per_page' => -1
);
$testimonial_count = count(get_posts($testimonial_count_args));

// calculate the total number of pages
$total_pages = ceil($testimonial_count/$per_page);

/* end pagination parameters */

// main image query
$testimonial_args = array(
    'post_type' => 'ncweb-testimonials',
    'numberposts' => $per_page,
    'offset' => $offset
);

$testimonial_items = get_posts($testimonial_args);

// start our output buffer
ob_start();

if($testimonial_items) :

    /*** main portfolio loop ***/

        $counter = 1;
        echo '<div class="testimonial-items" id="testimonial-items">';

        foreach($testimonial_items as $testimonial_item) :

        $testimonial_company = get_post_meta($testimonial_item->ID, 'ncweb_testimonial_company', true);
        $testimonial_client = get_post_meta($testimonial_item->ID, 'ncweb_testimonial_client_name', true);

        echo '<aside class="testimonial-list-item row">';
        echo '<div class="col-xs-12 testimonial-list-item-info">';
        echo '<div class="testimonial-image">'. get_the_post_thumbnail($testimonial_item->ID) .'</div>';
        echo '<div class="testimonial-client"><span class="testimonial-client-name">'. $testimonial_client .'</span><br/><span class="testimonial-company">'. $testimonial_company .'</span></div>';
        echo '</div>'; //end of testimonial-list-item-info
        echo '<div class="col-xs-12 testimonial-item-content">'. get_the_content($testimonial_item->ID); .'</div>';
        echo '</aside>';

        $counter++;

        endforeach;
        echo '</div>';

        /*** display pagination ***/

        // pagination base
        echo '<div id="testimonial-pagination">';

            $base = get_permalink(get_the_ID()) . '%_%';

            echo paginate_links( array(
                'base' => $base,
                'format' => '?testimonial-page=%#%',
                'prev_text' => __('Previous', 'ncweb'),
                'next_text' => __('Next', 'ncweb'),
                'total' => $total_pages,
                'current' => $page,
                'end_size' => 1,
                'mid_size' => 5
            ));

        echo '</div>';

    /*** end pagination display ***/


else :
    echo '<p>' . __('No testimonial items found.', 'ncweb') . '</p>';
endif; // end if($images)

return ob_get_clean();

}
add_shortcode( 'testimonial-items', 'ncweb_display_testimonial_items' );

【问题讨论】:

    标签: php wordpress shortcode


    【解决方案1】:

    这是因为get_the_content() 函数是一个包装器,它的唯一参数是Read More 文本和stripteaser 参数。

    你真正想要的是。

    $post= get_post($testimonial_item->ID);
    $testimonial_items = $post->content ;
    $testimonial_items = apply_filters('the_content', $testimonial_items);
    

    然后你就会得到你想要的。

    【讨论】:

    • 这段代码是正确的,但你应该将 $testimonial_items 作为 apply_filters() 的第二个参数而不是 $content 传递
    • 应该是 $post->post_content,而不是 $post->content
    【解决方案2】:

    在内容中使用短代码时,您可以尝试此代码以获得完美的输出。

    <?php 
      ob_start();
      the_content();
      $content_output = ob_get_clean();
      echo $content_output;
    ?>

    【讨论】:

      【解决方案3】:

      使用简码输出 the_content 的最简洁方式是:

      function my_sc_content() {
          ob_start();
              $my_content = the_content();
              echo $my_content;
              $output_string = ob_get_contents();
          ob_end_clean();
          return $output_string;
      }
      
      add_shortcode('my-content','my_sc_content');
      

      【讨论】:

        猜你喜欢
        • 2014-06-04
        • 1970-01-01
        • 2015-05-10
        • 1970-01-01
        • 1970-01-01
        • 2023-01-12
        • 2013-02-06
        • 1970-01-01
        相关资源
        最近更新 更多