How to put php created content into targeted specific div? - php

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' );

Related

How to write more efficient fallback for empty wp_query in Wordpress

I have a WooCommerce store where I want to display a featured image & heading of one of the following (in order):
Featured Product
If no featured product, then sticky post
If no sticky post, then most recent post
But I also want to write efficient code. How do I simplify this and remove redundant PHP and HTML?
/* START FEATURED PRODUCT QUERY */
$args = array(
'posts_per_page' => 1,
'post_type' => 'product',
'meta_query' => array(
'key' => '_featured',
'value' => 'yes'
),
$query = new WP_Query( $args );
if( $query->have_posts() ) {
while( $query->have_posts() ) {
$query->the_post(); ?>
<a href="<?php the_permalink(); ?>" id="featured-blog-post">
<?php the_post_thumbnail('full');
the_title('<h2>', '<span>»</span></h2>' );
the_excerpt(); ?>
</a> <?php
} // end while
wp_reset_postdata();
} else {
/* START FALLBACK POST QUERY */
$args = array(
'posts_per_page' => 1,
'post__in' => get_option( 'sticky_posts'),
'ignore_sticky_posts' => 1
);
$query = new WP_Query( $args );
while( $query->have_posts() ) {
$query->the_post(); ?>
<a href="<?php the_permalink(); ?>" id="featured-blog-post">
<?php the_post_thumbnail('full');
the_title('<h2>', '<span>»</span></h2>' );
the_excerpt(); ?>
</a> <?php
} // end while
wp_reset_postdata();
}
The second WP_Query has the exact same HTML output, just different $args
I’m writing a similar query and I’m not sure you can make the query much more efficient in Wordpress than you already have. The only thing I did differently was to make the output of the posts a function so that it calls the same code. This’ll make it easier to update.
Also since you’re only querying one meta field in the first query I switched to a simple custom field query.
// Function to output posts
function output_posts( $query ){
while( $query->have_posts() ) {
$query->the_post();
echo '<a href="' . get_permalink() '" id="featured-blog-post">';
the_post_thumbnail( 'full' );
the_title( '<h2>', '<span>»</span></h2>' );
the_excerpt();
echo '</a>';
}
wp_reset_postdata();
}
// Featured query
$args = array(
'posts_per_page' => 1,
'post_type' => 'product',
'meta_key' => '_featured',
'meta_value' => 'yes',
);
$featured = new WP_Query( $args );
// If featured has posts
if( $featured->have_posts() ) {
// Output
output_posts( $featured );
// Else fallback
} else {
// Fallback query
$args = array(
'posts_per_page' => 1,
'post__in' => get_option( 'sticky_posts'),
'ignore_sticky_posts' => 1,
);
$fallback = new WP_Query( $args );
// If fallback has posts
if ( $fallback->have_posts() ){
// Output
output_posts( $fallback );
}
}

how to loop in single attachment posts in custom post type in wordpress?

please help me with this annoying issue as i'm junior in wordpress , i have a different slider in my project and i made a custom post type to make attachment posts on in to make every single post as a slider .. the problem is how to loop inside CPT to retrieve every single post when i want is to display on front-end i tried alot and searched on net about it but the only code that i found and i used it made every single post to override the first post .. this is my code
<?php
$query = new WP_Query( array(
'post_type' => 'owlgalleryslider',
'posts_per_page'=> 3,
'fields' => 'ids'
));
$image_query = new WP_Query(array(
'post_type' => 'attachment',
'post_status' => 'puplish',
'post_mime_type' => 'image',
'posts_per_page' => 3,
'post_parent' => $query->id,
'order' => 'DESC'
));
?>
<?php if( have_posts( )) : ?>
<?php while ($image_query->have_posts()) : $image_query->the_post( ); ?>
<div class="item owl-slide"> <img src="<?php echo wp_get_attachment_url( get_the_ID() ); ?>" /> </div>
<?php endwhile; wp_reset_query( );?>
<? endif;?>
If you want to create a slider, set the featured image for every slider post and use that image in your WP_Query loop.
<?php
$query = new WP_Query( array(
'post_type' => 'owlgalleryslider',
'posts_per_page'=> 3,
));
?>
<?php if( have_posts( )) :
while ($query->have_posts()) : $query->the_post();
$image = wp_get_attachment_url( get_post_thumbnail_id() );
?>
<div class="item owl-slide"> <img src="<?php echo $image; ?>" />
</div>
<?php endwhile; wp_reset_query( );
endif;
?>

Content loaded from different queries mixed up / wrong content displayed

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

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