3

How can I fetch a div content from a post?

Using the_content() can fetch all the data but instead of that, I need specific content from the div only. So far, all I could do was get the link of the post as the div content.

Here is was I am trying to do:

<?php while (have_posts()) : the_post(); ?>

<li style = "text-align: center; float : left;">

  <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php echo get_the_post_thumbnail($id); ?></a>

  <div class="title">
    <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
  </div>

  <div id = "result">`

    <?php echo "<script> 
      $('#result').load( '".the_permalink()." .ps_post_description' );
    </script>"; ?>

  </div>

</li>

<?php endwhile; ?>

ps_post_description is the class of the div for which I need the content.

1

2 Answers 2

3

Using DOMDocument and DOMXPath you could try this.

<?php while (have_posts()) : the_post();

ob_start();  // run the_content() through the Output Buffer
the_content();
$html = ob_get_clean(); // Store the formatted HTML
$content = new DomDocument(); // Create a new DOMDocument Object to work with our HTML
$content->loadHTML( $html ); // Load the $html into the new object
$finder = new DomXPath( $content );  // Create a new DOMXPath object with our $content object that we will evaluate
$classname = 'ps_post_description'; // The class we are looking for

$item_list = $finder->query("//*[contains(@class, '$classname')]"); // Evaluates our content and will return an object containing the number of items matching our query
?>

<li style = "text-align: center; float : left;">

  <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php echo get_the_post_thumbnail($id); ?></a>

  <div class="title">
    <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
  </div>

  <div id = "result">`
    <?php 
    // echo the value of each item found
    // You might want to format or wrap your $value according to your specification if necessary
    for( $i = 0; $i < $item_list->length; $i++ ){
    $value = $item_list->item( $i )->nodeValue;

      echo $value;

      // if you want the text to be a link to the post, you could use this instead 
      // echo '<a href="' . get_the_permalink() . '">' . $value . '</a>';

    } ?>
  </div>

</li>

<?php endwhile; ?>

UPDATE

Made use of Output Buffering to expand shortcodes before we try to load the HTML in DOMDocument

Note that both solutions (before and after the edit) are working, so errors/warnings should come from elsewhere in your setup.

10
  • thanks for the response. i tried what you suggested but I'm getting a error > Warning: DOMDocument::loadHTML(): Tag canvas invalid in Entity, line: 6 in /home/anantdv/public_html/pantjan/wp-content/plugins/php-code-for-posts/Classes/Shortcode.php(81) : eval()'d code on line 16
    – puneet
    Aug 10, 2016 at 6:51
  • and on line no 16 i wrote $content->loadHTML( $html ); and if i use the_content() instead of get_the_content() i get the whole post as I was getting earlier.
    – puneet
    Aug 10, 2016 at 6:52
  • you can't use the_content() to store in a variable since it echoes its output, that why we use get_the_content(). The warning you get is because you are loading invalid HTML (which is normal since your are loading only part of the page) and it's fine in most cases since DOMDocument::loadHTML() will try to correct the problem. But I think your problem is coming from the use of php-code-for_posts plugin. Did you load the script in that shortcode or do you have shortcodes on the page you are fetching?
    – bynicolas
    Aug 10, 2016 at 14:52
  • Try disabling the plugin that's causing the issue to see if that helps. Also I updated the code to make use of the_content(). you could try that also
    – bynicolas
    Aug 10, 2016 at 14:54
  • Man, you are the life saver. Thanks! the updated code worked for me. Although I'm facing one more issue that is if there are html tags such as anchor or strong then I'm getting some different texts for example for <a href="somelink.html">Click here</a> i'm getting this ** Click here  Â**
    – puneet
    Aug 11, 2016 at 17:07
1

You'd have to get all the content out and then trim it down to that specific div. But why would you want to do that? It's inneficient and cumbersome - extra work to put it into the database, extra work to get it out. Have you considered adding a custom field to use just for that piece of content? (The excerpt sounds like what you're looking for, it's already built in and all you'd have to do to use it is <?php the_excerpt(); ?>)

1
  • thanks for the reply. I am not sure about the the_excerpt() . What I did was I changed the single post template so that I can use page builder for the post as well. Now if I use the_content() it gives me all the content that I created with the page builder along with the page builder code. And I want to show the content of that specific div on home page. I hope you understand what I'm trying to do here. So is there any way to achieve for what I'm trying to do.
    – puneet
    Aug 10, 2016 at 6:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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