Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Creating a wordpress shortcode

Featured Replies

Hi, hoping someone can help me.

 

I have created a custom post type called "books".

 

I neede a shortcode to display a thumbnail image of the featured image and the name of the post. Both being so you can click them and you will go to the post page.

 

The shortcode must be able to display a specific post according to post id, so something like this:

[listbook id="2456"]

I have tried a few hundred things and am just not managing to do this.

 

The basic html structure would be something like this:

<dl class="book">
  <dt><a href="#">thumbnail</a><dt>
  <dl><a href="#">Book Title</a><dd>
</dl>

Just learning wordpress and PHP and finding this project beyond my abilities!

Your method isn't a good way to go about doing this, it's not really dynamic in that every time you add a book you need to go in update this part of the site.

 

If you have a custom post type, you can use the archive template to loop through every post in this section and pull out the fields you need. If you need a unique template, you can prefix the template name with the name of your custom post type, for example archive-books.php.

 

If you're finding this beyond your ability then you should look at learning a bit more first. You're going to need to deal with creating new templates, writing loops and conditionals in PHP, so if you're not familiar with this you're going to get stuck. There's a course on Treehouse about working with WP templates and creating custom post type templates https://teamtreehouse.com/library/the-wordpress-template-hierarchy, also they have a PHP primer that I would recommend starting before you attempt anything else https://teamtreehouse.com/library/php-for-wordpress.

 

  • Author

Hi JAck. Thank you for the response.

 

I have already what you suggest ... http://www.courtney.co.za/booklevel/a-level/ list of all books for a level students or here http://www.courtney.co.za/booksubjects/biology/ a list of all books relating to the subject biology. The reason I want the shortcode is so on certain pages, say were we discuss the subject A-Level biology, I can place the little thumbnail and book title relating to that particular pages content.

 

Any thoughts?

  • Author

I've not thought this through, but it's just possible you could use this plugin https://wordpress.org/plugins/wp-show-posts/

 

Thank Jane.

 

Although wp show posts does not do quite what I need, I did find Custom Views which is perfect, although I did have to buy the pro version so as to display custom posts and fields.

 

Working example of that I want here: http://www.courtney.co.za/subjects/mathematics-9709/

 

That said, would still love to know how to write the shortcode to achieve this, if anyone know how :-)

Thank Jane.

 

Although wp show posts does not do quite what I need, I did find Custom Views which is perfect, although I did have to buy the pro version so as to display custom posts and fields.

 

Working example of that I want here: http://www.courtney.co.za/subjects/mathematics-9709/

 

That said, would still love to know how to write the shortcode to achieve this, if anyone know how :-)

 

It would look something like this:

<?php function relatedBook($postId = null){ ?>
  <dl class='book'>
    <dt>
      <a href="<?= get_permalink($postId); ?>">
        <img src="<?= get_the_post_thumbnail($postId); ?>">
      </a>
    </dt>
    <dt>
      <a href="<?= get_permalink($postId); ?>">
        <?= get_the_title($postId); ?>
      </a>
    </dt>
  </dl>
<?php } add_shortcode('related-book', 'relatedBook'); ?> 

[related-book postId=120]

 

You would add the function to your functions.php file (after closing the existing open php tag first) the shortcode can be used anywhere. I haven't tested whether this actually works, but, all that it's doing is passing an ID into existing Wordpress functions. A lot of the built in Wordpress functions will allows you to pass in an ID so that you can get content from somewhere else.

  • Jo 90 locked this topic
Guest
This topic is now closed to further replies.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.