0

Complete Code

<?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' => 5,
            'post_status' => null,
            'post_parent' => $post->ID,
               'order' => 'ASC',
        ) );
        if ( $attachments ) {
            foreach ( $attachments as $attachment ) {
                ?>
          <?php echo wp_get_attachment_image( $attachment->ID, 'full' ); ?>
                  <?php echo apply_filters( 'the_title', $attachment->post_title ); ?>
<?php
$parent = wp_get_post_parent_id( $attachment->ID );?>
<a href="<?php esc_url( the_permalink( $parent) ) ?><?php echo $page ?>/">
<?php the_title ?>
</a>
 <?php  } } ?>
    </div>

<?php the_content() ?>
</div>

<?php  wp_link_pages( array( 'before' => '<div class="page-link"><span>' . 'Page:' . '</span>', 'after' => '</div>' ) ); ?>
        <?php $post_anicast_idurl = get_post_meta($post->ID, '_my_url_anicast', true);
 if (!empty($post_anicast_idurl)) { ?>    
<iframe width="100%" height="120" src="<?php echo $post_anicast_idurl; ?>" frameborder="0"></iframe>
<?php }?>  
        <div id="area_avatar_autor">             
          <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('Editar','',''); ?>
        </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(); ?>

I'm trying to add in the attachment loop url the wp_link_pages function but I did not succeed, my current url:

enter image description here

I've been trying for some time but I can not add wp_link_pages to the url attachment loop, I'd like it to look like this:

enter image description here

6
  • Where are you executing wp_link_pages()?
    – Cedon
    Commented Mar 17, 2017 at 15:37
  • Yes <?php wp_link_pages( array( 'before' => '<div class="page-links">' . __( 'Pages:', 'themeone' ), 'after' => '</div>' ) ); ?>
    – user112214
    Commented Mar 17, 2017 at 15:38
  • Can you update the code block above with it? My suspicion is you're running it outside The Loop.
    – Cedon
    Commented Mar 17, 2017 at 15:39
  • Ok, so you're looking to get the permalink of the attachment's parent post to be in the wrapping <a> element?
    – Cedon
    Commented Mar 17, 2017 at 21:22
  • Yes, I do not know how to do it
    – user112214
    Commented Mar 17, 2017 at 22:10

1 Answer 1

0

In order to get the parent post, you would use the wp_get_post_parent_id() function.

So in your foreach loop, you would want to add something like:

$parent = wp_get_post_parent_id( $attachment->ID );
<a href="<?php esc_url( the_permalink( $parent ) ) ?>">
   // Rest of your HTML
</a>
19
  • Did not return the Attachment
    – user112214
    Commented Mar 17, 2017 at 16:30
  • Code update: pastebin.com/QYrcGEgr I want to insert the wp_link_pages function in the attachment url
    – user112214
    Commented Mar 17, 2017 at 17:02
  • Part of the problem is resolved. I am dividing each image of the post with the page break function each image is on a page, I would like the parent post url to appear in the attachment loop using the wp_link_pages function that is in the post. You can get the URL of the parent post and show the number of the post with the wp_link_pages function. Same as this image, I edited it in the browser to show how I want it to be.
    – user112214
    Commented Mar 18, 2017 at 0:34
  • Example: Image Url 01: http://localhost/wordpress/blog/post-teste/ Image Url 02: http://localhost/wordpress/blog/post-teste/2
    – user112214
    Commented Mar 18, 2017 at 0:35
  • I can't see those images in a comment
    – Cedon
    Commented Mar 18, 2017 at 0:43

Your Answer

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