0

How can I make a filter to get the image and the caption of the post, I am using the wp_link_pages function to do the internal pagination.

enter image description here

<?php get_header(); ?>                                                                                 
<main id="news_posts"> 
  <?php if (have_posts()) : ?>
  <?php while (have_posts()) : the_post(); ?>
  <!-- Post -->
  <section>
    <div class="post">
      <div class="area_news_post">
        <h1 class="post-title">             
          <?php the_title(); ?> 
        </h1>            
        <div class="news_data"> 
          <span>Date: 
          </span>
          <?php the_time( 'd/m/Y') ?> 
        </div>                         
      </div>       
      <div class="post-body">        
       <div class="images-post">
<?php
        $attachments = get_posts( array(
            'post_type'   => 'attachment',
            'numberposts' => 10,
            'post_status' => null,
            'post_parent' => $post->ID,
               'order' => 'ASC',
        ) );
        if ( $attachments ) {
            foreach ( $attachments as $attachment ) {
              $currentImage;  $count; 
   $currentImage++; 
                ?>          
<?php
$parent = wp_get_post_parent_id( $attachment->ID );?>
<div class="post_current">
<a href="<?php esc_url( the_permalink( $parent) ) ?><?php echo $currentImage ?>/">
<div class="current_page">
<?php echo $currentImage ?>
</div>
   <?php echo wp_get_attachment_image( $attachment->ID, 'large' ); ?>

</a>
</div>
 <?php  } } ?>    </div>
 <div class="image_post">
<div class="caption-image">

</div>
<?php
  preg_match_all("/(<img[^>]*>)/",get_the_content(),$matches,PREG_PATTERN_ORDER);
  for( $i=0; isset($matches[1]) && $i < count($matches[1]); $i++ ) {  
    echo $beforeEachImage . $matches[1][$i] . $afterEachImage; } 
    ?>
            </div>

            <?php if ( wp_link_pages( 'echo=0' ) ) : ?>
            <div class="post-pagination clearfix">
                    <?php
     wp_link_pages( array(
                    'before'         => '<div class="post-numbers">',
                    'after'          => '</div>',
                    'next_or_number' => 'number',
                    'pagelink'       => '<span class="numbers">%</span>'
                ) ); ?>
            </div>
            <?php endif ?>


           <div class="text_image">
<?php $content = get_the_content();$content = preg_replace("/<img[^>]+\>/i", " ", $content); $content = preg_replace("/\[caption.*\[\/caption\]/", '', $content);$content = apply_filters('the_content', $content);$content = str_replace(']]>', ']]>', $content);echo $content;
?>
</div>
</div>
        <div id="area_avatar">             
          <div class="avatar">
            <?php if (function_exists ( 'mt_profile_img' ) ) { $author_id=$post->post_author; mt_profile_img( $author_id, array('size' => 'thumbnail', 'attr' => array( 'alt' => get_the_author(),'title' => get_the_author()   ),'echo' => true ));
}?>
          </div>
          <div class="author_post"> 
            <span>By: 
            </span>
            <?php the_author() ?> 
          </div>
          <div class="views_post">
            <span> Views: 
            </span>
            <?php if(!current_user_can('administrator')){ echo setPostViews(get_the_ID()); } ?>
<?php echo getPostViews(get_the_ID());?>
          </div>
          <div class="share_post_link">
            <span class="label share_bg">Share:
            </span>
            <input type="text" class="share_link" readonly="readonly" value="<?php echo wp_get_shortlink(); ?>">
          </div>
        </div>
        <div id="share_social_tags">
          <div class="social_share">
            <a href="#" title="Facebook"> Facebook
            </a>
            <a href="#" title="Twiiter"> Twiiter
            </a>
            <a href="#" title="Google+"> Google +
            </a>
          </div>
          <div class="tags_post">
            <?php the_tags( ) ?>
          </div>
        </div>
        <div class="post_edit"> 
          <?php edit_post_link('Edit','',''); ?>
        </div>
        <!-- Navigation -->
        <div class="navigation">
          <div class="navigation-previous">
            <?php previous_post_link( '%link',' &laquo; Anterior') ?>
          </div>
          <div class="navigation-next">
            <?php next_post_link('%link', 'Próxima &raquo;') ?>
          </div>
        </div>
        <!-- /Navigation -->
        <div id="disqus_thread">
        </div>
      </div>
      <!-- /Post -->
      <?php endwhile; ?>
      <?php else : ?>
      <!-- Post -->
      <div class="post">
        <div class="post-top">
          <div class="post-bottom">
            <div class="post-title">
              <h2> Not found
              </h2>
            </div>
            <div class="post-entry">
              <p> Nothing found. Come Back
              </p>
            </div>
            <div class="post-info">
            </div>
          </div>
        </div>
      </div>
      <!-- /Post -->
      <?php endif; ?>
      <div class="clear"></div>
      </main>
    </div>
  <!-- /Main -->
  <?php get_sidebar(); ?>
  <?php get_footer(); ?>
  • 1
    It's not very clear what you want to do. – WPExplorer Mar 21 '17 at 18:59
  • I want to separate the text from the image and keep the caption in it – Rikuo Nura Mar 21 '17 at 19:32
  • Example: animeunited.com.br/blog/… – Rikuo Nura Mar 21 '17 at 19:43
  • It is possible to recover the image and the caption of the image by modifying this code ? $content = get_the_content(); $content = preg_replace('/(<)([gallery])(\w+)([^>]*>)/', ”, $content); $content = apply_filters('the_content', $content); $content = str_replace(']]>', ']]&gt;', $content); echo $content; – Rikuo Nura Mar 30 '17 at 17:49
1

If you are trying to remove the text i think the below code is what is adding this

/// this is where your content is being inserted
               <div class="strip_first_image">
    <?php $content = get_the_content();$content = preg_replace("/<img[^>]+\>/i", " ", $content); $content = preg_replace("/\[caption.*\[\/caption\]/", '', $content);$content = apply_filters('the_content', $content);$content = str_replace(']]>', ']]>', $content);echo $content;
    ?>
    </div>

Someone with better code skills might be able to correct me if I am wrong on the below

The get_the_content(); is bringing in your content from the post and it seems to be stripping out the images using preg_replace("/<img[^>]+\>/i", " ", $content);

Then $content = preg_replace("/\[caption.*\[\/caption\]/", '', $content); is removing the captions

After this it is calling a custom hook created and using apply_filters to run this on the content but I don't know what this would be doing.

I hope the above helps you to try remove the text if that was your aim as your title is not enough to go on what you actually are requesting.

Edit for the comment.

Try to add /* */ to that section and see if it only removes the text.

<?php /* $content = get_the_content();$content = preg_replace("/<img[^>]+\>/i", " ", $content); $content = preg_replace("/\[caption.*\[\/caption\]/", '', $content);$content = apply_filters('the_content', $content);$content = str_replace(']]>', ']]>', $content);echo $content;
    */?>
  • I'm trying to separate the text from the images, I wanted to keep the caption in the image – Rikuo Nura Mar 21 '17 at 19:31
  • I have edited my post as I think this does not contain your caption. – Sam Mar 21 '17 at 19:45
  • I'm trying to develop equal to this site animeunited.com.br/blog/… – Rikuo Nura Mar 21 '17 at 20:26
  • Did you try to comment out that section of code and see if it works? – Sam Mar 21 '17 at 20:30
  • I tried but I could not – Rikuo Nura Mar 21 '17 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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