• Resolved 96dpi

    (@96dpi)


    When trying to insert the attachments list into my theme, I get this output:

    <h3>Attachments</h3>
                        <ul>
                            <li>
                                <pre>stdClass Object ( [id] => 190 [fields] => stdClass Object ( [title]
                                    => Test title [caption] =>
                                    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse varius
                                        enim in eros elementum tristique. Duis cursus, mi quis viverra ornare,
                                        eros dolor interdum nulla, ut commodo diam libero vitae erat. Aenean faucibus
                                        nibh et justo cursus id rutrum lorem imperdiet. Nunc ut sem vitae risus
                                        tristique posuere.</p>) [post_id] => 10 )</pre>
                            </li>
                        </ul>

    So I get the correct attachment and all, but something isn’t working like it should.

    BTW. I’m trying to use it on a page, not a post.

    https://www.ads-software.com/plugins/attachments/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter 96dpi

    (@96dpi)

    Oh ok.

    The instructions are way too fuzzy for non-coders like me. I tried to use the code from the instructions under “Pulling Attachments to your Theme”:

    <pre><?php print_r( $attachment ); ?></pre>

    …but this only displays the attributes followed by their value.

    The right way to do this is to follow the instructions under Retrieve Attachment Attributes. E.g. use

    <?php echo $attachments->field( 'title' ); ?>
    <?php echo $attachments->field( 'caption' ); ?>

    etc.

    ??

    Thread Starter 96dpi

    (@96dpi)

    Also, if anyone would like to add their attachments with a shortcode, try this:

    function list_publication_attachments( $atts ) {
    
      ob_start(); ?>
    
      <?php $attachments = new Attachments( 'attachments' ); /* pass the instance name */ ?>
      <?php if( $attachments->exist() ) : ?>
        <div>
          <?php while( $attachment = $attachments->get() ) : ?>
            <?php echo $attachments->image( 'thumbnail' ); ?>
            <h3><a href="<?php echo $attachments->url(); ?>"><?php echo $attachments->field( 'title' ); ?></a></h3>
            <p><?php echo $attachments->field( 'caption' ); ?></p>
          <?php endwhile; ?>
        </div>
      <?php endif; ?>
    
      <?php
        $sc = ob_get_contents();
        ob_end_clean();
        return $sc;
    }
    add_shortcode('publications', 'list_publication_attachments');

    Add it to functions.php or in a file called shortcodes.php and include it in functions.php with

    include('shortcodes.php');

    This will let you add the list of attachments by using [publications] as a shortcode in any WP-post/page.

    Happy coding!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Weird (?) problem’ is closed to new replies.