Wordpress missing attachment link - php

I have attached a pdf to a post under a custom post type - I've created a link for users to download but all that displays is Missing Attachment. I've checked the admin panel and I've definitely attached it to a post - I can't seem to get it to link to it.
My Code is:
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 10;
$args = array( 'post_type' => 'stores', 'orderby' => 'title', 'order' => 'asc', 'posts_per_page' => $paged );
$success = new WP_Query( $args );
$output = '';
$output .= sprintf( "<table class='stores'>" );
$output .= sprintf( "<tr><th>File Name</th><th>Date added</th><th>Download</th></tr>" );
while( $success->have_posts() ) {
$success->the_post();
$output .= sprintf( "<tr>" );
$output .= sprintf( "<td>%s</td>", get_the_title() );
$output .= sprintf( "<td>%s</td>", get_the_date() );
$output .= sprintf( "<td><a href='%s'>link</a></td>", wp_get_attachment_link() );
$output .= sprintf( "<tr>" );
}
$output .= sprintf( "</tr></table>" );
return $output;

wp_get_attachment_link() requires the ID of your attachment, not the post that the attachment is attached to, so that it can fetch the link. -
wp_get_attachment_link(1234); // Replace 1234 with your relevant ID
To find your relevant ID, log in to your admin area, click Media and the click on the attachment you wish to link to. Finally, check address bar and you'll see something like http://www.mywebsite.com/wp-admin/post.php?post=7814&action=edit - The ID you require is whatever comes after post=.
Other Suggestions
Firstly, I notice in your query your are mixing up your posts_per_page and paged parameters. Essentially you are telling your query to check to see if there is a page set in the query string, else show you page 10. Try this instead -
$args = array(
'post_type' => 'stores',
'orderby' => 'title',
'order' => 'ASC',
'paged' => get_query_var('paged'), // The page to show
'posts_per_page' => 10 // How many posts to show on the page
);
I also notice that you use sprintf() regardless of whether or not it's actually required. This will slow things down, but to go even further, for just one variable I wouldn't use it at all as it's faster not too. Note also that swapping the " quotes for ' quotes will speed things up (as '"' checks for variable references to convert to the String). For you Loop I recommend this code -
$success = new WP_Query( $args );
if( $success->have_posts() ) :
$output.= '<table class="stores">';
$output.= '<tr><th>File Name</th><th>Date added</th><th>Download</th></tr>';
while( $success->have_posts() ) : $success->the_post();
$child_args = array(
'numberposts' => 1,
'order' => 'ASC',
'post_mime_type' => 'pdf',
'post_parent' => get_the_ID(),
'post_status' => null,
'post_type' => 'attachment',
);
$attachments = get_children($child_args);
if(attachments) :
$attachment_ID = $attachment[0]->ID
endif;
$output.= '<tr>';
$output.= '<td>'. get_the_title() .'</td>';
$output.= '<td>'. get_the_date() .'</td>';
$output.= '<td>link</td>';
$output.= '<tr>';
endwhile;
$output.= '</tr></table>';
endif;
return $output;
Finally, if you don't specifically need to return your output you could just output it direct, again speeding things up slightly.

Related

Show last modified posts based on post category in wordpress

As the title says, I have a problem with the output of last modified posts based on current post category. I found solutions based on either the last modified post or the category, but not both together.
As I'm more Illustrator/Designer with knowledge in HTML and CSS, I don't know how to put this together in php. For somebody who's familiar with coding, I'm sure, it's a question of seconds. What I have now is this:
function wpb_lastupdated_posts() {
// Query Arguments
$lastupdated_args = array(
'orderby' => 'modified',
'ignore_sticky_posts' => '1',
'showposts' => 2
);
//Loop to display 5 recently updated posts
$lastupdated_loop = new WP_Query( $lastupdated_args );
$counter = 1;
$string .= '<ul>';
while( $lastupdated_loop->have_posts() && $counter < 5 ) :
$lastupdated_loop->the_post();
$string .= '<li><a href="' . get_permalink( $lastupdated_loop->post->ID
) . '"> ' .get_the_title( $lastupdated_loop->post->ID ) . '</a> ( '.
get_the_modified_date('d.m,') . get_the_modified_time() .' ) </li>';
$counter++;
endwhile;
$string .= '</ul>';
return $string;
wp_reset_postdata();
}
//add a shortcode
add_shortcode('lastupdated-posts', 'wpb_lastupdated_posts');
I added the shortcode in the sidebar and it puts out my last modified posts as expected, but unfortunately it's missing the based-on-category-in-which-the-post-is-php.
Could please somebody help me with this?
Thanks
Update your shortcode to accept a parameter for category id.
On your wp query arguments, Use that id.
function wpb_lastupdated_posts( $atts ) {
// Query Arguments
$lastupdated_args = array(
'orderby' => 'modified',
'ignore_sticky_posts' => '1',
'showposts' => 2,
'cat' => $atts['catid']
);
When calling the shortcode, pass the current category id.
[lastupdated-posts catid="current_cat_id_here"]

How to add category option to shortcode

I'm new to creating wordpress shortcodes and have one just about working the way I want. Missing something specific. Currently I am able to put the following on any page - [children] and it pulls a query of all posts from the custom post type "children" I would like to add the option to add the category id within the shortcode - something like [children category="8"] Here is the code I have so far:
add_shortcode( 'children', 'display_custom_post_type' );
function display_custom_post_type(){
$args = array(
'post_type' => 'children',
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
);
$string = '';
$query = new WP_Query( $args );
if( $query->have_posts() ){
while( $query->have_posts() ){
$query->the_post();
$string .= '<div id="childWrapper"><div id="childImage">' . get_the_post_thumbnail() . '</div><div style="clear: both;"></div><div id="childName">' . get_the_title() . '</div><div style="clear: both;"></div></div>';
}
}
wp_reset_postdata();
return $string;
}
Secondary - is it possible for it to show posts in multiple categories, but only where the posts are in each of the categories. For example showing a list of children, who fall under a category for critical care and surgery needed.
Any help would be greatly appreciated.
You should refer Shortcode function from WordPress Codex. Assuming your taxonomy name is category, I made some changes in your current code it should work as per your requirements.
/**
* [children category="5,7,8"]
*/
add_shortcode( 'children' , 'display_custom_post_type' );
function display_custom_post_type($atts) {
$atts = shortcode_atts( array(
'category' => ''
), $atts );
//If category is multiple: 8,9,3
$categories = explode(',' , $atts['category']);
$args = array(
'post_type' => 'children',
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page'=> -1,
'tax_query' => array( array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => $categories
) )
);
$string = '';
$query = new WP_Query( $args );
if( ! $query->have_posts() ) {
return false;
}
while( $query->have_posts() ){
$query->the_post();
$string .= '<div id="childWrapper"><div id="childImage">' . get_the_post_thumbnail() . '</div><div style="clear: both;"></div><div id="childName">' . get_the_title() . '</div><div style="clear: both;"></div></div>';
}
wp_reset_postdata();
return $string;
}

Display custom posts with shortcode

I've been searching this forum for help for 2 days now but I can't find a solution for what I'm trying to achieve.
I have a custom post type called "product" and inside each post in that post type there are two custom fields called produktnamn(a textfield with product name) and produktbild(a product image). that's it.
Now I need a shortcode to display a particular post in that post type by entering it's slug (which is the same as it's name).
like this:
[product name="myproduct"]
That will render the name and product image inside some HTML tags.
Please help!
My code so far:
//Product image
function cpt_content_func($atts){
extract( shortcode_atts(['name' => null], $atts ) );
$args = [
'name' => $slug,
'post_type' => 'product',
'numberposts' => 1
];
$posts = get_posts( $args );
$post = $post[0];
$title = $post->title;
$id = $post->ID;
get_post_meta('produktbild', $id);
$content = $post[0]->post_title;
return '<h3>'.$content.'</h3>';
}
add_shortcode('product','cpt_content_func');
Solved it. Here's the code!
function display_custom_post_type( $atts ){
// Handle the attributes
$atts = shortcode_atts(
array(
'cat' => ' '
),
$atts, 'products_by_cat' );
// Set default values for the post
$args = array(
'posts_per_page' => -1,
'post_type' => 'product',
'post_status' => 'publish',
'category' => $atts['cat']
);
// Get posts
$posts_array = get_posts( $args );
// Check if posts exist
if( !empty($posts_array) ){
$string = '<div class="row">';
foreach( $posts_array as $post ){
$string .= '<div class="col-sm-3 text-center">';
$string .= '<div class="product-container">';
$string .= '<a href="' . get_field( "order_link", $post->ID ) . '">';
$string .= '<img class="product-img" src="' . get_field( "produktbild", $post->ID ) . '">';
$string .= '</a>';
$string .= '</div>';
$string .= '<p class="product-btn">' . get_field( "produktnamn", $post->ID ) . '</p>';
$string .= '</div>';
}
$string .= '</div>';
} else {
// Didn't find any posts.
}
// Return HTML
return $string;
I agree with James--I'd just use woocommerce.
But you first step has to be learning how to build a shortcode: http://code.tutsplus.com/tutorials/wordpress-shortcodes-the-right-way--wp-17165
Once you've attempted creating your own shortcodes, if things still aren't working, that's the time to ask for help.

orderby menu_order show max 10 pages

trying to put together a wp site for a martial arts centre, and they need a list of their instructors.
i try to get all the children of the instructor page appear in list by their page order, and that works fine, except it only shows the first 10 pages, and there is currently 13 instructor pages...
this is the code i used:
<section id="instruktorer">
<div class="indent">
<?php
$query = new WP_Query( 'pagename=instruktorer' );
$services_id = $query->queried_object->ID;
/* Restore original Post Data */
wp_reset_postdata();
$args = array(
'post_type' => 'page',
'post_parent' => $services_id,
'orderby' => 'menu_order',
'order' => 'ASC',
);
$services_query = new WP_Query( $args );
// The Loop
if ( $services_query->have_posts() ) {
echo '<ul class="instruktorer">';
while ( $services_query->have_posts() ) {
$services_query->the_post();
echo '<li class="clear">';
echo '<a href="' . get_permalink() . '" figure class="instruktorer-thumb">';
the_post_thumbnail('instructor-pic');
echo '</a>';
echo '</figure>';
echo '<div class="caption">' . get_post( get_post_thumbnail_id() )->post_excerpt . '</div>';
echo '</li>';
}
echo '</ul>';
}
/* Restore original Post Data */
wp_reset_postdata();
?>
</div><!-- .indent -->
</section>
Change your query to this:
$args = array(
'posts_per_page' => -1,
'post_type' => 'page',
'post_parent' => $services_id,
'orderby' => 'menu_order',
'order' => 'ASC'
);

Shortcode to list post titles from certain category

i found this great snippet of code on http://codex.wordpress.org/Template_Tags/get_posts and I edited it a little to suit me. It works.
<p>
<?php
$args = array( 'posts_per_page' => 0, 'offset'=> 0, 'category' => 1 );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<?php the_title(); ?>
</br>
<?php endforeach;
wp_reset_postdata();?>
</p>
I would like to transform it into a shortcode with one changeable variable - category id. But, well, since I don't know php, all I came up is:
function tytuly_postow($atts) {
extract(shortcode_atts(array(
'id' => 1,
), $atts));
$args = array( 'posts_per_page' => 0, 'offset'=> 0, 'category' => $id );
$q ="<p>
<?php
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<?php the_title(); ?>
</br>
<?php endforeach;
wp_reset_postdata();?>
</p>";
}
add_shortcode('tytuly','lista_postow');
Of course, it doesn't work. I use Karma theme and added it in shortcodes.php file. Please, help :)
The code you have is certainly on the right track. Here's how I'd do it:
function tytuly_postow( $atts ) {
extract( shortcode_atts( array(
'id' => 1,
), $atts ) );
$posts = get_posts( array(
'posts_per_page' => -1,
'post_status' => 'publish',
'cat' => $id,
) );
$output = '';
if ( $posts ) {
$output .= '<p>';
foreach ( $posts as $post ) {
$output .= apply_filters( 'the_title', $post->post_title ) . '<br />';
}
$output .= '</p>';
}
return $output;
}
add_shortcode( 'tytuly', 'tytuly_postow' );
Offset by default is 0 so you don't need to add it as an argument. I've set posts_per_page to -1. This means get all.
Since you're just listing off titles it may make sense to order them by title. At the moment they'll be ordered by date descending.
I'm setting output to a blank string and returning output at the end. This means no matter what happens the shortcode will always return something, even if that something happens to be an empty string.
Then I'm checking if posts were found. If that's the case I'm adding a paragraph tag before and after the foreach. This was to match your original code.
Finally it loops through each post and uses the_title filter on the post titles then ends each title with a break tag.
By taking a look at the Shortcode API and add_shortcode Function Reference, you can check that the parameters of function add_shortcode are $tag and $func.
So, to start fixing your code, you need to fix it:
//[tutuly]
function tytuly_postow($atts) {
...
}
add_shortcode('tytuly', 'tytuly_postow');
By doing this, you will have a working structure for your shortcode. Now, your second mistake is that your function needs to return a value, thats what your shortcode will be transformed into. To do this, you need to use return.
Edit: Plus, your logic to retrive posts names was wrong, so I fixed it:
So, your fixed code will look like:
//[tutuly id="1"]
function tytuly_postow($atts) {
extract(shortcode_atts(array(
'id' => 1,
), $atts));
$args = array(
'posts_per_page' => -1,
'category' => $id,
'post_status' => 'publish'
);
$myposts = get_posts($args);
if ($myposts) {
$q = '<p>';
foreach ($myposts as $post) {
$q .= apply_filters('the_title', $post->post_title) . '<br />';
}
$q .= '</p>';
} else {
$q = '';
}
return $q;
}
add_shortcode('tytuly','tytuly_postow');
posts_per_page is now set to -1 to retrieve all posts,
post_status is used to retrieve only posts that are published.
If you want to retrieve links for your posts, instead of just the name, you can change this line:
$q .= apply_filters('the_title', $post->post_title) . '<br />';
to
$q .= '<a href=' . get_permalink($post->ID) . '>' . apply_filters('the_title', $post->post_title) . '</a><br />';
Notice that if you don't have enough programming skills, you can always use the awesome GenerateWP Shorcodes Generator.

Categories