$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How to add an image to a theme page template in code?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How to add an image to a theme page template in code?

matteradmin8PV0评论

So, I'm trying to do an update to a site's theme layout, and I'm not sure what exactly the right function to achieve what I want is. We have a custom theme that uses a page builder, and we can add custom layouts that are added with Advanced Custom Feeds. For greater control over layout, I'm essentially just hand coding the sections we want to add. From what I've seen in the Codex, the function get_attached_file is the closest to what I think I want, but I'm not sure how to get the attachment id from the Media gallery. What I want is some way of pulling any image in the database into a page template and not to manually set the image dimensions and avoiding hard coding image src in the template files. So...what's the better way to do this?

So, I'm trying to do an update to a site's theme layout, and I'm not sure what exactly the right function to achieve what I want is. We have a custom theme that uses a page builder, and we can add custom layouts that are added with Advanced Custom Feeds. For greater control over layout, I'm essentially just hand coding the sections we want to add. From what I've seen in the Codex, the function get_attached_file is the closest to what I think I want, but I'm not sure how to get the attachment id from the Media gallery. What I want is some way of pulling any image in the database into a page template and not to manually set the image dimensions and avoiding hard coding image src in the template files. So...what's the better way to do this?

Share Improve this question asked Feb 11, 2019 at 15:21 nizz0knizz0k 1198 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Haven't tested this, but the key is to use get_posts() with the proper arguments so as to retrieve the ids of the attachments, e.g.

   $args = array(
     'post_type' => 'attachment',
     'numberposts' => -1,
     'post_status' => null,
     'post_parent' => $post->ID
  );

  $attachments = get_posts( $args );
     if ( $attachments ) {
        foreach ( $attachments as $attachment ) {
           echo '<li>';
           the_attachment_link( $attachment->ID, true );
           echo '<p>';
           echo apply_filters( 'the_title', $attachment->post_title );
           echo '</p></li>';
          }
     }

See https://codex.wordpress/Function_Reference/get_attachment_link and https://codex.wordpress/Function_Reference/the_attachment_link.

As far as the image dimensions go, you probably want CSS for that, unless you just want to display the image as is.

Post a comment

comment list (0)

  1. No comments so far