Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

On the dashboard under add pages, I have 3 section of blockquotes, under text

<blockquote id="section1">first</blockquote>

<blockquote id="section2">second</blockquote>

 <blockquote id="section3">third</blockquote>

So, when I try output them in the index.php , I do

<?php while(have_posts() ): the_post() ?>
    <div class="entry-content">
            <div id="first">.. </div>
            <div id="second"> ..</div>
            <div id="third"> .. </div>

        </div>

<?php endwhile;?>   

How do I output the blockquote id=section1 into div id=first, blockquote id=section2 into div id=second, blockquote id=section3 into div id=third?

do we only have the_content() function? Can we break the content into pierce we want?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

Post content is stored as a single block of data in the *_posts table in the post_content column. It isn't divided up into sections, so what you want is tricky at best.

What the the_content functions does is take that block of data from the database, runs one or more filters on it that alter the data and prints the result to the screen. You could in principle create and use one of these filters to break up your content.

First, if you are hard-coding blockquotes into the post body, why not just avoid the hassle and write in your <div>s? I don't see the point of creating <blockquote id="section1">first</blockquote> only to convert it to <div id="first">.. </div>

But if you must, this should get you pretty close...

global $replace_counter;
$replace_counter = 0;
function replace_bquotes($match) {
  if (!empty($match)) {
    global $replace_counter;
    $replace_counter++;
    return '<div id="div-'.$replace_counter.'">'.$match[1].'</div>';
  }
}
function grab_blockquotes($content) {
  $pattern = '/<blockquote[^>]*>(.+)<\/blockquote>/';
  $content = preg_replace_callback($pattern,'replace_bquotes',$content);
  return $content;
}
add_filter('the_content','grab_blockquotes',1);

It occurs to me that you are trying to use blockquotes in a way that seems like you are treating them as shortcodes. I would suggest actually using a shortcode.

function div_maker($atts,$content) {
  $id = '';
  if (isset($atts['id'])) {
    $id = 'id="'.esc_attr($atts['id']).'"';
  }
  return '<div '.$id.'>'.$content.'</div>';
}
add_shortcode('dm','div_maker');

Now you can create your <div>s with [dm id="first"]First Content[/dm]. That is less typing, and should be much more robust. Parsing HTML with regex, as in the first block of code, is not simple and is easy to get wrong.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.