I'm using this piece of code I found (http://impnerd.com/wordpress-hack-add-post-images-to-your-homepage) to display the first image uploaded to a post on the homepage next to the excerpts of posts. I'm doing this outside the main loop on the homepage, and have been having problems. When I do rewind_posts() to get the same loop results, it works fine, but when I try to create a different loop, this code snippet breaks down:
$images =& get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . $post->ID );
if ($images)
{
$keys = array_keys($images);
$num = $keys[0];
$firstImageSrc = wp_get_attachment_thumb_url($num);
echo "<li><img src=\"{$firstImageSrc}\" width=\"288\" height=\"216\" alt=\"\" title=\"\" /></li>";
}
I have tried the methods in The_Loop#Multiple_Loops_in_Action in the docs, and they work, meaning I can get normal output after the loop, but my snippet above doesn't work. Any idea if there is a conflicting method call or something that's going on that's stopping it from working? Would appreciate some help, thanks!
To be more specific:
<?php $my_query = new WP_Query('category_name=Daily Photo&showposts=1');
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID; ?>
<p>a</p>
<?php $images =& get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . $post->ID );
if ($images) {
$keys = array_keys($images);
$num = $keys[0];
$firstImageSrc = wp_get_attachment_thumb_url($num);
echo "<li><img src=\"{$firstImageSrc}\" width=\"288\" height=\"216\" alt=\"\" title=\"\" /></li>";} ?>
<?php endwhile; ?>
will output <p>a</p>, but not the <li><img /></li> code I need in the snippet. Whereas if I use rewind_posts();, everything works, and I get the <li><img /></li> code, but I don't want to use the same loop that I had been using previously. I'm using this to display a daily photo in the sidebar, that pulls from the "Daily Photo" category. I will exclude Daily Photos from the main loop, and only want to use them to draw images from in that snippet.
Try removing the if statement for starters. Remove any references to images and see if it outputs your html without the image source. If that's the case then images isn't getting properly assigned and it's never executing the code.
$images =& get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . $post->ID );
This code checks to see if there is an
image uploaded to the gallery.
Now you need to troubleshoot the above statement and see why you're not getting any images.
Also, it looks like there is a plugin to do just this if you don't want to continue messing with the code.
Another thing to keep in mind is that this code is pretty old and may not be compatible with your version of wordpress.
Related
I am using HTML5Blank as a starting theme and it comes with this function which returns 40 chars excerpt:
<?php html5wp_excerpt('html5wp_custom_post'); ?>
My main blog page is more complex, so I am using array to store values into it and echo them where I need them:
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<?php $post_titles[$counter] = get_the_title($post->ID); ?>
<?php $post_excerpts[$counter] = html5wp_excerpt('html5wp_custom_post', $post_id); ?>
<?php $post_permalinks[$counter] = get_the_permalink($post->ID); ?>
<?php $post_thumbs[$counter] = get_the_post_thumbnail($post->ID, '', array('class' => 'img-fluid')); ?>
<?php $counter++; ?>
<?php endwhile; ?>
All other fields work and I can echo them, but I don't have any idea how to make excerpt work because it isn't echoing anywthing with:
<?php echo $post_excerpts[0]; ?>
First of all I notice, that you are using a variable called $post_id, which is not defined as far as I can see. You need to add a $post_id = $post->ID; before or alternatively you can use instead:
<?php $post_excerpts[$counter] = html5wp_excerpt('html5wp_custom_post', $post->ID); ?>
Maybe this already solves your problem. But to make sure, I will take it further.
I took a look at the functions.php of the HTML5-Blank-Theme: https://github.com/html5blank/html5blank/blob/master/src/functions.php
// Create 40 Word Callback for Custom Post Excerpts, call using html5wp_excerpt('html5wp_custom_post');
function html5wp_custom_post($length)
{
return 40;
}
So the function only returns the value of 40. I guess you can simply use the html5wp_excerpt() like this:
html5wp_excerpt(40);
Maybe there is something wrong with the html5wp_custom_post, so you can get rid of it to test this. And I also think, why use an addtional function, if it only returns a number... you can easily set it in the function call.
I don't know if this functions accepts an post ID as a parameter. So maybe it can only be used inside of a single.php page. I can't find a documentation about this, maybe you can do some research and find it out.
Here is another way to achieve it:
You can use the get_the_title() function that will accept the post id. Unfortunately, the get_the_excerpt() does not accept it.
So we first need to get the post object and then apply a filter to get the excerpt of the post. Do this by putting this code inside your while loop:
<?php $current_post = get_post($post->ID); ?>
You now have the current post as an object. In the next line, we apply the filter and save the result to the array at the right index position:
<?php $post_excerpts[$counter] = apply_filters('get_the_excerpt', $current_post->post_excerpt); ?>
I wonder why there are so many php opening and closing tags, so you could make your code more readable:
<?php while ($the_query->have_posts()) : $the_query->the_post();
$current_post = get_post($post->ID);
$post_excerpts[$counter] = apply_filters('get_the_excerpt', $current_post->post_excerpt);
$post_titles[$counter] = get_the_title($post->ID);
$post_permalinks[$counter] = get_the_permalink($post->ID);
$post_thumbs[$counter] = get_the_post_thumbnail($post->ID, '', array('class' => 'img-fluid'));
$counter++;
endwhile; ?>
Just as an additional info, you could also use only the filters, should work with your post object:
$post_titles[$counter] = apply_filters('the_title',$current_post->post_title);
EDIT:
You can trim your excerpt to a certain length with mb_strimwidth (read more: https://www.php.net/manual/en/function.mb-strimwidth.php) :
$current_post = get_post($post->ID);
$trim_excerpt = apply_filters('get_the_excerpt', $current_post->post_excerpt);
$post_excerpts[$counter] = mb_strimwidth($trim_excerpt, 0, 40, '...');
EDIT 2:
Maybe you should check if you are getting your post object. The fact that you are always seeing the same excerpt, maybe means you get the excerpt of the current page (not the post in your query).
$current_id = get_the_id();
$current_post = get_post($current_id);
I have a basic table in a wordpress site that looks like this: https://jsfiddle.net/4fcpdgs8/
Right now the table is hard coded with values found in the html but ideally I would like it to be dynamic based on the values in the MYSQL database. I have seen some solutions that involve php such as:
global $wpdb;
// this adds the prefix which is set by the user upon instillation of wordpress
$table_name = $wpdb->prefix . "your_table_name";
// this will get the data from your table
$retrieve_data = $wpdb->get_results( "SELECT * FROM $table_name" );
?>
<ul>
foreach ($retrieve_data as $retrieved_data){ ?>
<li><?php echo $retrieved_data->column_name;?></li>
<li><?php echo $retrieved_data->another_column_name;?></li>
<li><?php echo $retrieved_data->as_many_columns_as_you_have;?></li>
<?php
}
?>
</ul>
<?php
But one wrinkle is that one column is a url for an html image tag so doing the above just displays the url.
So, I would rather do something that would enable me to just reference the sql results like:
<img src=[mysql_image_column_row_one]>
<img src=[mysql_image_column_row_two]>
Rather than just displaying the sql results in a table. Sidenote: I know very little php and it is quite mysterious to me!
you can use this for images
<?php echo '<img name="myimage" src=' . $retrieved_data->column_name
. ' width="60" height="60" alt="imagedesciription" />'?>;
You must adept the image size or even resize the image.
You can use img tag with an image path inside li tag in foreach loop.
<li><img src='path_to_image_folder/<?php echo $retrieved_data->image_name?>'></li>
I have ACF Plugin installed and I have a gallery filed in my post. I've tried all these docs but still getting the error :
Invalid argument supplied for `foreach()`
this happens because the input of the for each is not an array!
Do you have any clue what's wrong with this?
Do you think if I have to set something while I've defined the custom field?
<?php
$images = get_field('mygall');
$size = 'full'; // (thumbnail, medium, large, full or custom size)
if( $images ): ?>
<ul>
<?php foreach( $images as $image ): ?>
<li>
<?php echo wp_get_attachment_image( $image['ID'], $size ); ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
I think your problem comes from the fact that you are using get_field() instead get_fields(). That's way you don't get an array.
If it still doesn't work check the documentation for get_fields() here. Try to debug it like using only get_fields() and see what is the output. If it is an empty array then it means that you are calling the function out of the loop and it can't get the post id. So do a second test with manually setting the post id like get_fields(123); and check the results. If there are no results then there is something wrong with that post. And if there are results then you can do a final test with checking what will be the result of get_fields(123, 'gallery').
All the above debugging can be wrapped in something like:
echo '<pre>';
print_r( get_fields(123) );
echo '</pre>';
Basically this will give you some idea of what is the structure of the data that you get from this function and how you can manipulate it so to get what you need.
I am using a plugin that outputs data with a simple tag reference
<?php echo $property['building_brochure']; ?>
This outputs the reference number for an uploaded pdf which in this example is 352.
I want to return the actual link for this, the following works
<?php echo wp_get_attachment_url( 352 ); ?>
However, I want the combine these so on the single page template for the entry the relevant PDF is called i.e.
<?php echo wp_get_attachment_url( 'building_brochure' ); ?>
Is there a straightforward way of doing this?
You need to place the function as input into the other function the way Spectre-d has shared in comments.
i.e
<?php echo wp_get_attachment_url( $property['building_brochure'] ); ?>
However if you face issues,like not correct attachment_URL, then it could be Type issue i.e. first function might be outputting string and not integer as expected by attachment_url function in that case I recommend:
<?php
$pdf_id = (int)$property['building_brochure'];
echo wp_get_attachment_url( $pdf_id );
?>
as a newbie in php I am having trouble achieving an as I guess quite simple thing.
I got this code so far (in a php template file):
<?php
//Define my custom post type name in the arguments
$args = array('post_type' => 'reference-document');
?>
<ul>
<li>2013</li>
<li>2014</li>
<li>2015</li>
</ul>
In my custom post-type 'reference-document' I have a list of available tags with the years 2013,2014,2015
Now, actually, my above code is working but I wish to output the li list depending on how many years ever the user might enter as tags in the post-type. (so eventually also 2016,2017…)
So I guess I need to create the list with a php loop or whatever taking the available tags of my post-type as arguments. So far I have tried something like:
<?php
$tags = get_the_tags();
foreach ( $tags as $tag ) {
echo '<li>'.esc_attr($tag->tag_name).'</li>';
}
?>
to get the same output as I had achieved “manually”. But this throws errors and I am quite stuck here. Anyone how can give me the right code?
Please do not forward me to the official standard WP documentations as I tried to understand them but didn’t succeed. I need concrete code. Then I might understand how it is working. Thanks so much in advance!
EDIT
When I add the if statement in my code as:
<?php
$tags = get_the_tags();
if(!empty($tags)) {
foreach ( $tags as $tag ) {
echo '<li>'.esc_attr($tag->tag_name).'</li>';
}
}
?>
in fact there are no more errors but nothing happens also. Which I guess means the 'tags' array is empty. But why? There are tags assigned in my custom post-type. As I said I am really new here so I guess my code contains some semantic illness. Please check!
I succeed at least in part by doing so:
<ul>
<li>All</li>
<?php
//Define the loop based on arguments
$loop = new WP_Query( $args );
//Display the contents
while ( $loop->have_posts() ) : $loop->the_post();
?>
<li><a href="#" data-filter=".<?php
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
echo $tag->slug;
}
}
?>"><?php
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
echo $tag->slug;
}
}
?></a></li>
<?php endwhile;?>
It seems I had a lack of understanding what it means to have tags for a custom post-type since it seems they cannot be retrieved as long as not looping through the posts having them attached. I thought it was possible to say instead: Ok, here we have the XXX post-type let’s check (or output) i.g. for a isotope script, what tags exist for this “category” and display them.
The problem that my solution left is to check which tags are double and then subtract them from the array first.