Content loaded from different queries mixed up / wrong content displayed - php

I have a very strange bug on my wordpress site:
in the header.php i'm loading images like that:
<?php
$args = array(
'post_type' => 'attachment',
'post_status' => 'any',
'tax_query' => array(
array(
'taxonomy' => 'media_category',
'field' => 'name',
'terms' => 'redimages'
)
)
);
$the_query2 = new WP_Query( $args );
if ( $the_query2->have_posts() ) {
while ( $the_query2->have_posts() ) {
$the_query2->the_post();
$postid = get_the_ID();
$image_attributes = wp_get_attachment_image_src( $postid , 'full');
if ( $image_attributes ) : ?>
<img src="<?php echo $image_attributes[0]; ?>" class="backgroundimage"/>
<?php endif;
}
}
wp_reset_postdata();
?>
and inside functions.php i have a custom function to load the content of a page:
function pf_show_page($id) {
$post = get_post($id);
$content = apply_filters('the_content', $post->post_content);
echo $content;
}
now my problem is, that one image from the first query is always displayed, when i call the pf_show_page function, although there are no images in the pages I load.
The strangest thing is that this only happens when the 404.php is opened, but the images show up in the header.php region.
I copied the queries from the other templates to the 404 document and it is still happening.
Maybe there's something wrong outside my template files?
Tried my best to describe my problem. Thank you for reading

Related

How to put php created content into targeted specific div?

Noob here! I'd like to run a simple post-loop on a wordpress homepage. If I write the loop in my functions.php file and then load the homepage, the result of the query is shown at the top of the mainpage.
Now I would like to see the result of the loop in a specific div. The reason for that is, that I would like to use a pagebuilder for most of the sites content first and then see the loop.
TLDR; How can I make the content created by a php function be displayed/loaded into a specific div container? Is it even possible?
I've tried creating the div and putting the shortcode for my function inside of it but wordpress wouldn't even allow me to save the changes.
in functions.php:
function show(){
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'orderby' => 'post_date',
'order' => 'desc',
'posts_per_page' => '30',
'post_status' => 'inherit'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$image = wp_get_attachment_image_src( get_the_ID() );
echo'<div><div class="numbertext">1</div>';
echo $images = wp_get_attachment_image( $query_images->posts->ID, 'full' );
echo'<div class="title">';
the_title();
echo'</div></div>';
endwhile;
};
add_shortcode( 'imgshow', 'show' );
Then in Pagebuilder:
<div id="imgcontainer">[imgshow]<div>
So for each img the query finds, I expect the loop to create some div boxes with the images and their titles in it.
You're on the right track, but a little off on your shortcode syntax. One thing to remember with shortcodes is you always have to return content, as opposed to echoing it. I haven't tested this, but this should work.
function show(){
ob_start(); // should usually start a shortcode with an output buffer
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'orderby' => 'post_date',
'order' => 'desc',
'posts_per_page' => '30',
'post_status' => 'inherit'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$image = wp_get_attachment_image_src( get_the_ID() ); ?>
// You don't have to echo divs you can use html tags in PHP
<div class="numbertext">1</div>
<img src="<?php echo $image; ?>" />
<div class="title">
<?php the_title(); ?>
</div>
</div>
<?php
endwhile;
// put the content in a variable creating from the output buffer
$content = ob_get_clean();
// return the content
return $content;
};
add_shortcode( 'imgshow', 'show' );

Get images from gallery in taxonomy

There seems to be several answers to similar questions but I have not yet found one working for me.
I have a custom post-type called entertainement. entertainement has a taxonomy called ent_categories.
One of the ent_categories is called Event
Each Event has a gallery and I am trying to make a query that will return the latest 10 images that has been added to any of the CPT entertainment with the category Event.
Im hoping to receive a list of urls in an array.
From what I read here something like this should do the trick:
$arg = array(
'post_status' => 'inherit',
'posts_per_page' => -1,
'post_type' => 'attachment',
);
$arg['tax_query'] = array(
array(
'taxonomy' => 'ent_categories',
'field' => 'name',
'terms' => array( 'Event' ),
),
);
$the_query = new WP_Query( $arg );
var_dump($the_query);
The var_dump($the_query); displays a lot of things but no images?
Any tips on this one?
Thank you
EDIT:
I just saw that I can do this:
function pw_show_gallery_image_urls( $content ) {
global $post;
// Only do this on singular items
if( ! is_singular() )
return $content;
// Make sure the post has a gallery in it
if( ! has_shortcode( $post->post_content, 'gallery' ) )
return $content;
// Retrieve all galleries of this post
$galleries = get_post_galleries_images( $post );
$image_list = '<ul>';
// Loop through all galleries found
foreach( $galleries as $gallery ) {
// Loop through each image in each gallery
foreach( $gallery as $image ) {
$image_list .= '<li>' . $image . '</li>';
}
}
$image_list .= '</ul>';
// Append our image list to the content of our post
$content .= $image_list;
return $content;
}
add_filter( 'the_content', 'pw_show_gallery_image_urls' );
This result in that all the gallery images urls get displayed below the images in the gallery.
Maybe this function could be called from a page instead than from functions.php?
You were on the right track, but you're querying for posts of type attachment with a term of the ent_categories taxonomy which only applies to entertainement posts, so there won't be any of them as you'll see if you:
var_dump($the_query->posts);
If you dump all $the_query you'll see lots of things because it's a WP_Query object.
You need to query your entertainement posts: (Be careful because you have a typo in your slug!)
$arg = array(
'posts_per_page' => -1,
'post_type' => 'entertainement',
);
$arg['tax_query'] = array(
array(
'taxonomy' => 'ent_categories',
'field' => 'name',
'terms' => 'Event',
),
);
$the_query = new WP_Query( $arg );
Then you can iterate the posts and get the gallery items like this:
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
if (get_post_gallery()) :
echo get_post_gallery();
print_r(get_post_gallery_images());
endif;
}
/* Restore original Post Data */
wp_reset_postdata();
}
get_post_gallery_images() will get you an array of the URL's of gallery images
get_post_gallery() will get you actual HTML to print the gallery.

Wordpress get attachment image caption

I tried to get attachment meta caption value as mentioned here, but couldn`t get any output. Other meta arrays like [created_timestamp] or [iso] gave their values.
$img_meta = wp_get_attachment_metadata( $id );
echo $img_meta[image_meta][caption];
This issue happens to both [caption] and [title]. Any help is much appreciated.
The caption and title you are looking to get from wp_get_attachment_metadata are not the title and caption you add in WordPress they are meta data from the actual image itself. To get the WordPress data use something like this (assuming $id is the id of your image).
$image = get_post($id);
$image_title = $image->post_title;
$image_caption = $image->post_excerpt;
Since WordPress 4.6.0 there is get_the_post_thumbnail_caption($post) which gets you the caption for the specified post.
put this in your functions.php file:
function show_caption_image($type='title'){
global $post;
$args = array( 'post_type' => 'attachment', 'orderby' => 'menu_order', 'order' => 'ASC', 'post_mime_type' => 'image' ,'post_status' => null, 'numberposts' => null, 'post_parent' => $post->ID );
$attachments = get_posts($args);
if ($attachments) {
foreach ( $attachments as $attachment ) {
$alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);
$image_title = $attachment->post_title;
$caption = $attachment->post_excerpt;
$description = $image->post_content;
}
}
return $type == 'title' ? $image_title : $caption.$description;
}
and below the image in your theme, or wherever you prefer to put it, usually in the single.php file:
<?php if ( has_post_thumbnail() ) :
?>
<span class="image main"><img src="<?php echo get_the_post_thumbnail_url()?>" alt="<?php echo get_the_title()?>" /><i><?php echo show_caption_image();?></i></span>
<?php endif; ?>

Get featured image of a page (no post) in Wordpress

I need to show the featured images of all the pages, not the posts. I have this code:
<?php
if ((is_singular() || is_home()) && current_theme_supports('post-thumbnails')) : echo get_the_post_thumbnail( '12', 'full' ); ?>
<img src="<?php header_image(); ?>" class="header-img" alt="" />
<?php endif;?>
But this only shows one featured image.
Thank you so much!
You can simply use WP_Query to get that,
$loop = new WP_Query( array( 'post_type' => 'page', 'meta_key' => '_thumbnail_id' ) );
Or if you want to do by your way you need to fetch all pages first & than loop over it to get thier feature image,
$args = array(
'post_type' => 'page',
'post_status' => 'publish'
);
$pages = get_pages($args);
foreach($pages as $page) {
echo get_the_post_thumbnail( $page->ID, 'full' );
}

How to exclude posts with no images in a wordpress query_post function?

The query below runs a featured carousel on my site. I want the carousel to only display post that contain an image. I've searched everywhere and cannot find a solution. Please help!
query_posts(array('post_type' => 'ad_listing', 'post_status' => 'publish', 'orderby' => 'rand'));
I ran into this same thing while developing my own theme.
This is how I solved it.
Start by adding the featured image capability in your functions.php.
if (function_exists('add_theme_support')) {
add_theme_support('post-thumbnails');
}
This allows you to select a featured image for each post.
With the featured image function available you can use the following function to detect if a post has a featured image...in loop form:
$args = array(
'post_type' => 'ad_listing'
);
query_posts($args);
if ( have_posts() ) :
while ( have_posts() ) : the_post();
if ( has_post_thumbnail() ) { //For featured images
//For post images/attachments
ob_start();
the_id();
$postID = ob_get_clean();
$args = array(
'numberposts' => 1,
'order'=> 'ASC',
'post_mime_type' => 'image',
'post_parent' => $postID,
'post_status' => null,
'post_type' => 'attachment'
);
$images =& get_children($args);
if ( empty($images) ) {
//What to do without images
}
else {
//What to do with images
}
}
endwhile;
else :
//What happens when no posts are found
endif;
Hope this helps.
You won't be able to do that using query_posts. You'll have to do it in the loop.
Look at get_children() and especially the example of how to Show the first image associated with the post.

Categories