Ads between posts in Wordpress with Genesis Framework - php

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!

Related

WP Sorting custom posts alphabetically by Title

Newbie here...
I have a custom post type of 'equipe' (team in portuguese). I am trying to sort these alphabetically by post title then display the_title so we have a alphabetical list of names.
I've done a search on here and tried a few fixes but Im struggling to get anything other that the standard order.
Any help would be much appreciated!
<?php
$args = array('orderby'=> 'title', 'order' => 'ASC', 'post_type' => 'equipe', 'posts_per_page' => -1, 'post_status' => 'publish' );
$q = new WP_Query($args);
while ( $q->have_posts() ) : $q->the_post();
?>
<h3><?php the_title(); ?></h3>
<?php
endwhile;
wp_reset_query();
?>
<?php
$args = array( 'post_type' => 'equipe', 'posts_per_page'=>5, 'orderby'=>'post_title','order'=>'ASC');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
RESOLVED:
Ok the reason it was enforcing menu_order was because of a setting (F*ing checkbox) within the plugin Post Types Order.
I needed to un-check AUTO SORT
and check Use query ASC / DESC parameter
This then allowed me to adjust the array as follow (and discussed above):
$args = array('orderby' => 'title', 'order'=>'ASC', 'post_type' => 'equipe')
However I did need to add 'order'=>'ASC' into the other pages that sorted by the original query of menu_order.

How can I exclude only latest post in certain category from showing in loop?

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

WordPress: Editing custom post type

I've to do some edits to wordPress theme home page . This is the scenario.
There are 4 posts being showed in home page under news tab now when i check this is using front-page template and in front-page template I've something like this
<?php
wp_reset_query();
$loop = new WP_Query( array( 'post_type' => 'news', 'posts_per_page' => 4 , 'orderby ' => 'date' , 'order' => 'DESC' ) );
// $loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
Now when i try to find out post type for news i dont find it .so if there is no custom post type for this what could be other option for this? please someone help me with it how can i figure this out .

Edit post arg for specific Wordpress category

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!

Trying to get an attachment from a post in a Wordpress loop but getting all, from all posts

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

Categories