I have this in my loop:
<?php
$args=array(
'cat' => $select_blog_category,
'orderby' => 'date',
'order' => 'DESC',
'paged' => $paged
);
$temp = $wp_query;
$wp_query = new WP_Query( $args );
// The Loop
while ( $wp_query->have_posts() ) : $wp_query->the_post();
$featured_image_array = wp_get_attachment_image_src( get_post_thumbnail_id(), 'blog-square' );
$featured_image = $featured_image_array[0];
?>
I want this loop to display a specific category. I thought I could do this by changing:
'cat' => $select_blog_category
to this (my post category):
'cat' => 'upcoming-2'
But it doesn't work. I love everything else about how this code is displaying my posts, just want to filter it, thanks!
I also need to add another loop on the same page with a different category. Can I have two loops on the same page? So far every attempt I have tried at copying the code to a new section has resulted in a black white page, which tells me I'm doing something wrong.
I only come here as a last resort, not because I want someone to do my work, so I really appreciate the help, thanks!
Related
wp_get_attachment_image() gets images only for 2 out of 5 posts.
I am new to WordPress, I am making theme where main page only shows images (attachments) from posts. Posts are all in the same manner - title, paragraph, and gallery. Each of them have category assigned to it.
Tried to fix the problem in many ways but none seem to be working. I've dumped every post and it seems that
$postyMarkiQq = new WP_Query( array( 'category_name' => 'marki' ) );
$posty = $postyMarkiQq->posts;
gets information properly.
I've tried using get_attached_media('', $post->ID) but it returns same results.
I've tried putting the main part of the code to function.
<?php
$postyBrandQ = new WP_Query( array( 'category_name' => 'brand' ) );
$posty = $postyBrandQ->posts;
foreach($posty as $post) {
echo $post->ID;
?>
<div class="category">
<?php
echo get_the_title( $post->ID );
$args = array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
);
$attachments = get_children( $args );
foreach ( $attachments as $attachment ) {
echo wp_get_attachment_image($attachment->ID);
}
wp_reset_postdata();
?>
</div>
<?php
}
?>
I don't have any idea why wouldn't this work.
Would really appreciate help.
Thanks!
You probably shouldn't be querying by post parent. Attachments are only given post parents based on the page you are currently viewing when you upload the item to the media library. If you click media, and upload images, the images won't have a post parent, even if you go to a post (or page) later on, and set that image as the featured image.
So, I don't know the exact context of your question, but perhaps get_post_thumbnail_id( $post->ID ) will be the answer you are looking for. This will return the attachment ID of the featured image, then you can use wp_get_attachment_image_src() (pass in the attachment ID probably), which will return you an array. Print the array, in there somewhere you will find the URL.
I am trying to set up a blog that inserts an ad between each post. Here's a wire frame to help explain what I am trying to do. (wireframe) Ads are outlined in red. I am using Genesis framework.
I tried using custom post types to create an "Ad" post type but the problem is I can't prevent the ads from duplicating on the same page. I use the
Here's my loop that I am using. I run the genesis_after_entry hook to add the loop.
<?php $args = array( 'post_type' => 'ads', 'posts_per_page' => 1, 'orderby' => 'rand' );
$do_not_duplicate = array();
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$do_not_duplicate[] = $post->ID;
the_content();
endwhile; ?>
Something's obviously wrong because it's still duplicating. Any help would be greatly appreciated!
I have two loops and queries.
At the top of page I have this code. It only shows the latest post from category named "featured":
<?php
$latest_featured_post = new WP_Query ( array ( 'category_name' => 'featured', 'posts_per_page' => 1 ) );
while ($latest_featured_post->have_posts()) : $latest_featured_post->the_post();
?>
Now I want to exclude that post from the other, main, loop on the same page, because I don't want it to show twice. I tried to achieve that by catching the ID of a latest post in a "featured" category and passing it to the 'post__not_in' argument but I did something wrong. This is my code
<?php
$category_id = get_cat_ID('Događaji');
$exlude_latest_featured_post = array($latest_featured_post->ID);
$args = array(
'category__not_in' => array($category_id),
'post__not_in' => $exlude_latest_featured_post,
);
query_posts( $args );
while (have_posts()) : the_post(); ?>
<?php get_template_part('loop/content'); ?>
I tried to manually pass ID of the post ('post__not_in' => array(1337) for example) and it works. Which means that I made mistake with catching the "featured" latest post ID.
I was searching Google for the answer but I didn't find anything helpful. Hope someone here has time and right answer
Thanks
You can capture the featured post id withing the 1st loop via get_the_id function, then use it in the later loop:
<?php
$latest_featured_post = new WP_Query ( array ( 'category_name' => 'featured', 'posts_per_page' => 1 ) );
while ($latest_featured_post->have_posts()) :
$latest_featured_post->the_post();
$featuredID = get_the_id();
?>
Your latter loop:
$category_id = get_cat_ID('Događaji');
$exlude_latest_featured_post = array($featuredID);
I'm trying to query posts in wordpress, so far so good. However, I would like to show a post with a specific tag 'info' in front of all (even in front of sticky).
Now the query of the theme is like this:
$sticky = get_option( 'sticky_posts' );
rsort( $sticky );
if(of_get_option('sticky-posts') == 'show_sticky'){
query_posts (array( 'post__in' => $sticky, 'order' => $order_posts, 'ignore_sticky_posts' => 1, 'paged' => $paged ) );
}
And after that the loop starts:
//BEGIN LOOP
//=====================================================?>
<article id="articlehold">
<?php
if(have_posts()) : while ( have_posts() ) : the_post();
Now I've done some editing in the query to get the post with the 'info' tag in front, but no luck. Now I'm not sure where the best place is to edit this, in the loop or before the loop?
How could I do this?
First of all - this is a question mainly about the loop™. I'm also having problems with the attachment thing, but that is somehow secondary since there are snippets around there I think could be useful.
Ok, so this is want I want to do in a front page:
get 7 posts from custom post type 'portfolio'
only with the first one, get an specific attachment (in any possible way, be it filename, order in the media manager... whatever is the best, more clean way)
with the other 6, just get the_post_thumbnail() and little more.
Now, I have this:
<?php
$args = array (
'post_type' => 'portfolio',
'order' => 'DESC',
'posts_per_page' => 7
);
$query = new WP_Query( $args );
$first_post = true; /* we will take the latest work first */
?>
<?php if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<?php
if ($first_post):
$first_post = false;
?>
<div> /* This is the div for the first item */
<?php /* let's take the first attachment */
$args = array(
'post_type' => 'attachment',
'numberposts' => 1,
'order' => 'DESC'
);
$attachments = get_posts( $args );
if ( $attachments ) :
foreach ( $attachments as $attachment ) :
echo wp_get_attachment_image( $attachment->ID, 'full' );
endforeach;
endif;
?>
</div>
<?php else: ?>
/* Do something with the other 6 posts and their post_thumbnail */
<?php endif ?>
<?php endwhile; ?>
And now for the questions:
First and foremost: if I set 'numberposts' to all (-1) when trying to recover the attachment, I get ALL attachments from ALL 'portfolio' posts. Shouldn't I be interacting only with the current post (the_post())? I can't quite grasp the concept of the loop here, this is the main question.
That code won't get me the first attachment, even if it's placed in the first place in that post's media manager.
Should I go for secondary or nested loops? I've read and re-read the codex and other tutorials but still can't wrap my head around it.
Many thanks!
used this code :
$attachments = get_posts( array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'exclude' => get_post_thumbnail_id()
) );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
$class = "post-attachment mime-" . sanitize_title( $attachment->post_mime_type );
$thumbimg = wp_get_attachment_link( $attachment->ID, 'thumbnail-size', true );
echo $thumbimg;
}
}
User Shakti Patel gave me the key to the answer, but didn't really answer my question about the loop so here it goes:
The problem was with get_posts. It actually runs a parallel query to the main one without taking into account the current step of the loop. So we have to ask it for the attachments of the current post, which since we're in the loop is stored within $post->ID. So knowing that, we have to request the first attachment for the current post like this:
$args = array(
'post_type' => 'attachment',
'posts_per_page' => 1,
'post_parent' => $post->ID,
'exclude' => get_post_thumbnail_id()
);
$attachments = get_posts( $args );
That way we specify what is the post from which we'll get the first attachment, and while we're on it, exclude the post thumbnail.
I don't know if this is the best way since we're already on a loop and we shouldn't need a new query, shouldn't we be able to retrieve that attachment without the get_posts bit?
Anyway, for more info on get_posts (read while not half asleep): http://codex.wordpress.org/Template_Tags/get_posts