I was trying to make a template to show all used image in wordpress blog, and link to the post which has the image attached, like a gallery or portfolio page, but I can not get the link nor title to the post single page.
I've made this far:
<?php
$post_parent = get_post($post->ID, ARRAY_A);
$parent = $post_parent['post_parent'];
$paged = ( get_query_var( 'paged' ) ) ? get_query_var('paged') : 1;
$attachments = get_children( array( 'post_parent' => $post_id, 'post_type' => 'attachment', 'orderby' => 'menu_order ASC, ID', 'order' => 'DESC', 'paged' => $paged, 'posts_per_page' =>9,) );
?>
And the foreach:
<?php foreach($attachments as $id => $attachment) :
$img_title = $attachment->post_title;
$img_caption = $attachment->post_excerpt;
$img_description = $attachment->post_content;
?>
So I looked up the codex and found how to get the img url like this:
<?php if ( have_posts() ) : ?>
<li>
<?php $image_attributes = wp_get_attachment_image_src( $id, full ); // returns an array
if( $image_attributes ) {?>
<img src="<?php echo $image_attributes[0]; ?>" alt="<?php echo $img_alt; ?>" title="<?php echo $img_title; ?>" />
<?php
echo $post->post_parent;
?>
<p><a href="<?php
echo get_post($id)->post_parent; ?>">link</a></p>
<?php } ?>
</li>
<?php endif; ?>
<?php endforeach; ?>
These codes above works great, it shows all images attached in an ul li list.
Then I want to add a link to the parent POST in the loop, using something in the foreach like:
<p>Post link</p>
but it returns a link to nothing or returns a "0", if I "echo $id", it return the attachment id but not the Post ID (Post id is what I want).
Please how to get the parent post ID or simply get the post link and title in the loop above? Help!
Thank you!
Find the answer now.
I use the normal query to get all posts, and that query search for the img tag in content( or its featured image) then show that image in loop, optionally to show excerpt below that image, then this template is done.
I was thinking about getting attachments in media library but it is not the right way, some media are not even attached to any posts so it’s a dead end.
But to query post and make that query select posts with “<img” is somewhat way more better than query attachments, and easy to understand too.
Related
I need to edit my Wordpress child theme (based on Twenty Twenty One) archives.php template so that it grabs the first image added within any Post, not the Featured Image as that is being used by another plugin (and as such has to be different dimensions than what we want here). I've tried a few different solutions but nothing seems to work, I am guessing this may be to do with the fact I am using Gutenberg blocks.
I did however get the following code to work for the posts I had already uploaded (I have set out the most relevant line of code between two spaces within the archiveContainer div), but bizarrely after then creating some new test posts the code is no longer working and is just grabbing the Featured Images again, which is not what we want.
<?php while ( have_posts() ) : ?>
<?php the_post(); ?>
<?php $attachment = array_values( get_children( array( 'post_parent' => $post->ID, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'numberposts' => 1 ) ) ); ?>
<h2 class="archiveTitle" id="post-<?php the_ID(); ?>">
<?php the_title(); ?>
</h2>
<div class="archiveContainer">
<?php if( $attachment ) echo '<img src="' . wp_get_attachment_url($attachment[0]->ID) . '" class="post-attachment" />'; ?>
</div>
<?php get_template_part( 'template-parts/content/content', get_theme_mod( 'display_excerpt_or_full_post', 'excerpt' ) ); ?>
<?php endwhile; ?>
It could well be that I have inadvertently changed or deleted something of course - but, any ideas how I can get this working consistently?
I have a custom image field for all pages with a specific page template (using ACF plugin).
I'm querying for these pages like so:
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'page',
'meta_key' => '_wp_page_template',
'meta_value' => 'services-page.php'
));
Then I'm displaying pages with a foreach loop:
if( $posts ): ?>
<?php foreach( $posts as $post ): setup_postdata( $post );?>
//content goes here
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Now I want to access the custom field to display inside the loop. But, below doesn't work. I'm guessing because ACF fields don't get appended to the post object.
//Does not work
$image = $post -> services_block_image
ACF has the get_field() function, but what can I do to get the field for each of the posts from my original query? Found ACF docs to be rather confusing on this (goes without saying I'm a bit new to PHP).
Within loop use get_field function to get the image.
check below code for your reference.
$image = get_field('services_block_image'); // get the image
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
On my Wordpress website I am filtering through blogposts via custom fields on 2 seperate places. On the one page I pass the values via querystring and then use query posts to display the filter results on that same page, which works well.
Now, my question is, how do I go about if I want to use the same filters, but display the results on a different page.
The link that passes the querystring:
NATIONAL
Query posts
<?php
$location = $_GET['location'];
$catid = $category_link;
$sps = array(
'post_type' => 'post',
'posts_per_page' => -1,
'cat' => $catid,
'order' => DESC,
'orderby' => date,
'meta_key' => 'location',
'meta_value' => $location);
?>
Display results
<?php query_posts($sps); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li>
<?php
if ( has_post_thumbnail())
{
echo '<a href="' . get_permalink($post->ID) . '" class="thumbnail_image_link">';
the_post_thumbnail();
echo '</a>';
}
?>
</li>
<?php endwhile; else: ?>
<p>There are currently no results available</p>
<?php endif; ?>
Is there any way I can modify this code to display on another page?
just change
NATIONAL
lets suppose your page id is 12
NATIONAL
and use same query to retrieve post on this page.....
I am creating a website that integrates a portfolio which uses custom post types, this was done based off of this tutorial.
So far it is exactly what I am looking for and works great except for one small detail. In order to fetch the posts from the new custom post type the author of the tutorial used the query_posts() codex. So the top of my portfolio page looks like this:
<?php
/* Template Name: Portfolio */
get_header();
query_posts('post_type=portfolio&posts_per_page=10');
?>
What I gather is that this declares "get posts from "post type" portfolio and show 10 per page". My problem is that I can't go get content from my portfolio page. It seems that now my portfolio page only fetches the content from the custom post type, and I can't use:
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; // end of the loop. ?>
to get content from the actual page.
This is what I am trying to do, I've replaced:
query_posts('post_type=portfolio&posts_per_page=10');
with:
add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
function add_my_post_types_to_query( $query ) {
if ( is_page( 8 ) && $query->is_main_query() )
$query->set( 'post_type', array( 'portfolio' ) );
return $query;
}
This seems like the right track, but it stills doesn't work. I'm not getting the posts from my custom post type.
Any ideas how I could modify this? I am also still learning so being clear with explanations would be greatly appreciated.
Thank you!
Editing the pre_get_posts will replace the original query and you will not have the content for your page at all. I would only recommend going this approach if you only wanted to display the content of your portfolio post type and not the content of your portfolio page.
For general post queries it is recommended to use WP_Query or get_posts.
http://codex.wordpress.org/Class_Reference/WP_Query
http://codex.wordpress.org/Template_Tags/get_posts
If you use the WP_Query function the wp_reset_postdata() will restore the post data back to the original so you can get the content of your original page.
$args = array(
'posts_per_page' => 10,
'post_type' => 'portfolio',
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
Now you will be able to use the original loop to show the content of your page
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; // end of the loop. ?>
Usually, I stick my query posts in a variable, like so:
$catid = get_cat_ID('My Category Name');
$args = array(
'posts_per_page' => 5,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'category' => $catid
);
$posts_array = get_posts($args);
Then you can loop it like so:
<?php foreach ($posts_array as $post) : setup_postdata($post);?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<?php endforeach; ?>
Finally, to access your page content you can use the variable $post, it's automatically set by wordpress. No need to add any more code than this to access your page content.
<?php foreach( $posts as $post ) : setup_postdata($post); ?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<?php endforeach; ?>
The foreach loop for your page content is a little overkill, and there is a better way to do it (most likely at least), but I haven't been bothered to look into it further yet! It works though!
I have that site : http://ougk.gr and I want on the navigation to have a link which points to the latest post of a specific category (full post with comments etc). How can I achieve that?
There are a few ways to do this. This one uses wp_get_recent_posts(), and prints a basic link:
<nav>
<?php
$args = array( 'numberposts' => '1', 'category' => CAT_ID );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo 'Latest Post';
}
?>
// .. other menu code ..
</nav>
Where CAT_ID is the id of the targeted category. For your situation the simple answer is to insert the link code just after the opening nav tag, as above.
To place the link somewhere else in the nav, you'll have to dive into some of the other functions called in the code you pasted. It might be a good idea to get your hands dirty..
<?php
$args = array(
'numberposts' => '1',
);
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ):
$post_id = $recent['ID'];
$post_url = get_permalink($recent['ID']);
$post_title = $recent['post_title'];
$post_content = $recent['post_content'];
$post_thumbnail = get_the_post_thumbnail($recent['ID']);
endforeach;
?>
You need ot get the title and perma link
<?php
// retrieve one post with an ID of 5
query_posts( 'cat=X&posts_per_page=1&order=DESC' );
// the Loop
while (have_posts()) : the_post();
echo "<a href='<?php the_permalink(); ?>'>";
the_title();
echo "</a>";
endwhile;
?>