I've inherited a wordpress site and I am having a hard time understanding how posts are being displayed. I want to hide a couple from view (but still be able to give out a URL to view them). I'm not familiar with the way a particular template was coded. The template outputs an image and blurb for each event in a certain category. The meat of code that is spitting this out look like this:
<?php
$args['post_type']='seasonalevents';
$args['posts_per_page']=-1;
$args['orderby'] = 'menu_order';
$activities = new WP_Query( $args );
while ( $activities->have_posts() ) : $activities->the_post();
$image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src($image_id,'thumb_345_154', true);
?>
Is there any way I can exclude post ID's within the code above? Any hints or tips? Feel totally baffled by this. The variables are defined above this code snippet. I can post if needed.
thanks!
The wordpress-y way to do this would be to add an element to the $args array under the three you already have:
$args['post__not_in'] = array(123,456,789);
Where 123, 456, and 789 are the ids of the posts you want to exlude from showing on this page.
So your whole code would look like:
<?php
$args['post_type']='seasonalevents';
$args['posts_per_page']=-1;
$args['orderby'] = 'menu_order';
$args['post__not_in'] = array(123,456,789);
$activities = new WP_Query( $args );
while ( $activities->have_posts() ) : $activities->the_post();
$image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src($image_id,'thumb_345_154', true);
?>
Yes there is!
You can get the current post's ID using http://codex.wordpress.org/Function_Reference/get_the_ID
I recommend you looking into 'the loop' and what that is.
This code snippet should do the job :-)
...
$not_these = array(1, 2, 7 /* array with post id's you got somewhere */);
while ( $activities->have_posts() ) : $activities->the_post();
if(in_array(get_the_ID(), $not_these)) continue;
...
The easiest solution is to unpublish that post from your administration panel.
Or
<?php
// The Loop
while($query->have_posts()):
$query->the_post();
if(get_the_ID()!=YOUR_POST_ID):
?>
<!-- Show Post -->
<?php
endif;
endwhile;
?>
Related
I am having trouble getting the image attachment to show by the title & excerpt on a Wordpress page in a list. I am using a new WP_Query to generate the list, consisting of 3 posts, from certain categories. Inside the loop, I'm trying to get the first image of each post, retrieving their image IDs with a 'get_children' array (the posts do not have featured images, so I can't use the_post_thumbnail). Using console.log, I can see that I am at least getting the image url for the first item in the list, but not for the others. And even with the one I am getting, I can't get it to display using wp_get_attachment_thumb_file(). Here's the code I have so far:
<div class="inner-left">
<ul class="blog-posts-ul">
<?php
global $wp_query;
$wp_query = new WP_Query(array('order' => 'DESC', 'posts_per_page' => 3, 'cat' => '21,23,689,741,1589'));
if (have_posts()) : while (have_posts()) : the_post(); ?>
<li>
<?php $attachment = get_children( array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' => 'image' ) );
if ( $attachment ) {
$attachment = current( $attachment );
$image_url = wp_get_attachment_thumb_url( $attachment->ID );
$attachment_id = attachment_url_to_postid( $image_url );
echo '<script>console.log("thumb: "' . $image_url . ')</script>';
}; ?>
<?php the_title(); ?><?php the_excerpt(); ?>
<?php wp_get_attachment_thumb_file( $attachment->ID ); ?>
</li>
<?php endwhile; endif; ?>
</ul>
<?php // Reset Query
wp_reset_query(); ?>
</div><!-- inner-left -->
The test site is at http://testsite.humortimes.com/ if you want to have a look. Scroll down, it's formatted as 3 squares, below the 'Latest Humor Times Faux News Headlines' section. Right now, it's not formatted very well, I plan on having two columns, there's just one showing.
Any help would be appreciated. Thank you.
Stop using console log and start using var_dump ($image_url) ; that will print whatever you have directly on that element.
I dont quite get why you cant use post thumbnail?
After a lot of frustration, I found this nifty function that does just what I want. It queries the database to find the first image of the post, which I guess is the key I was missing. It also tests for a featured image first, so that's taken care of as well. I put the function in my child function file, and just call it when needed on a page.
If you use it, be aware you need to add $size = 'thumbnail'; because, although her description says it'll return a thumbnail, it was returning full size images. This variable is used throughout the function, though, so you just need to give it a value.
Thanks, Amberweinberg!
I am creating a wp template based on 2013. I want to display a 'page' that has some content from a page and then 6 posts on it. These posts have been selected via the themes options panel using the settings api. So I can get at each one using
$options = get_option( 'osc_theme_options' );
The template i have been using so far is based on a page so i am guessing I need to change the loop in someway.
The loop goes:
<?php /* The loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>...
I want to know how to alter the loop so that it pulls in just the posts that have been selected. At the moment this template/loop is only pulling in the page - which I guess is fair enough.
Would it be possible to create 'another loop' maybe under the first that then brings in the selected posts - if so how?
Many thanks
Yes you can effectively create another loop under the existing loop to display the posts. I am not sure what $options = get_option( 'osc_theme_options' ); returns, i.e an array of Post ID's etc. In order to show the posts you need to do a custom loop:
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() )
{
echo '<ul>';
while ( $the_query->have_posts() )
{
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
}
else
{
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
This is taken from:
https://css-tricks.com/snippets/wordpress/custom-loop-based-on-custom-fields/
Also see the following:
https://digwp.com/2011/05/loops/
http://www.smashingmagazine.com/2013/01/14/using-wp_query-wordpress/
So effectively it all boils down to the $args variable as to which posts you will get. Here is an example of multiple ids
id=2,6,17,38
So if $options = get_option( 'osc_theme_options' ); returns an array of post ids like so:
array(
0=>1
1=>22
2=>27
)
You could probably do something like:
$args = "id=".implode($options,",");
This is the best advice I can give without deeper knowledge of the theme etc.
I'm trying to query my WordPress for a specific post type "team_members" and then store the titles in an array. When I use the following my output just shows 0, 1, 2 (I only have the 3 posts) it doesn't show the post titles.
$my_array=array();
// the query
$args=array('post_type' => 'team_members','order'=>'ASC');
$my_query = new WP_Query($args);
if ($my_query->have_posts()) :
while ($my_query->have_posts()) : $my_query->the_post();
$my_array[]=get_the_title( get_the_ID() );
endwhile;
endif;
Any help or direction would be greatly appreciated.
Thanks
John
Thanks Guys,
Your right it does work, I think it was the way I was trying to use the variable in Visual Composer. When I use the variable in a drop down box I see the titles correctly but when I use it for multiple checkboxes it displays the post ID.
It's a Visual Composer issue.
Thanks Mauro as the_title(); did simplify the code and also worked for me.
Thanks
John
I can't see any problem with your code. Try using get posts and using the post_title property instead.
E.g.
$my_array = array();
// the query
$args=array('post_type' => 'team_members','order'=>'ASC');
$posts = get_posts( $args );
if ( $posts ) {
foreach ( $posts as $post ) {
$my_array[] = $post->post_title;
}
}
// the query
$args=array('post_type' => 'team_members','order'=>'ASC');
$my_query = new WP_Query($args);
if ($my_query->have_posts()) :
while ($my_query->have_posts()) : $my_query->the_post();
$id = $my_query->posts->ID;
$my_array[]=get_the_title( $id );
endwhile;
endif;
try replacing
get_the_title(get_the_ID());
with
the_title();
I am trying to do something very characteristic and i am not that kinda good with PHP.
I am making some site for soccer team. And i want to have one main page called RESULTS. I will have for example 10 rounds(every round have separate page) in full season. I already make fixtures and everything depending on date.
My problem is how to call specific page, for example today is 29.10.2013 and my 3rd round has just over and i fill out all my result on my "3rd round page", and i want that this content be shown on page "RESULTS" also.
Than next week 04.11.2013 its coming my 4th round and i will also fill out everything on my "4th round page", but i just want now that all this be on "RESULTS" page because this is active round now not 3rd round.
I am guessing that i need to make template for my "RESULTS" page but i just need code, function how to call specific ID page depending on date. (29.10.2013 call page id 54; 04.11.2013 call page id 55).
I hope i explained well and hope someone is good in this.
Thanks in advance!
This is probably not the most elegant answer but should work
<?php
switch(date('d.m.Y'))
{
case '29.10.2013':
$THE_PAGE_ID = 1;
break;
case '29.10.2013':
$THE_PAGE_ID = 2;
break;
case '29.10.2013':
$THE_PAGE_ID = 3;
break;
default:
$THE_PAGE_ID = 4; // page if the date does not match
}
$args = array( 'posts_per_page' => -1,
'post_type' => 'page',
'include'=> $THE_PAGE_ID
);
$myposts = get_posts( $args );
foreach ( $myposts as $post ) :
setup_postdata( $post );
?>
<h1><?php the_title(); ?></h1>
<div id="page-content">
<?php the_content(); ?>
</div>
<?php
endforeach;
wp_reset_postdata();
?>
If you want to get the data of a specific page you can use "get_posts"
http://codex.wordpress.org/Template_Tags/get_posts
Inside your page template add something like this
<ul>
<?php
$args = array( 'posts_per_page' => -1,
'post_type' => 'page',
'include'=> $THE_PAGE_ID
);
$myposts = get_posts( $args );
foreach ( $myposts as $post ) :
setup_postdata( $post );
?>
<li>
<?php the_title(); ?>
</li>
<?php
endforeach;
wp_reset_postdata();
?>
</ul>
Or use "get_post":
http://codex.wordpress.org/Function_Reference/get_post
<?php
$thepost = get_post($THE_PAGE_ID, ARRAY_A);
$title = $thepost['post_title'];
echo $title;
?>
Hope that's what you where after.
Thanks a lot for reply, i see that you very good with this. I did like you tell me and with this i got link to page on my "RESULTS" page.
So is it possible to little bit change code that will be checking date. For example:
check date,
if date is 29.10.2013->call content(not link)of page ID=2
if date is 30.10.2013->call content of page ID=3
if date is 31.10.2013->call content of page ID=4
I would do it myself but i am not even close good with this like you. So sorry for being boring but i really need to learn this stuff.
In the website every product is a post, but when we add new products we want something like a newsletter, mostly like a post so in the sidebar of the home page you can see the new products or events of the month.
I'm using pages because I don't want to re-post a product on every new newsletter so I junt wanna display the posts inside the page.
In the products page I separate every product by category and sub-category but since I want to group specific post to publish them on the sidebar I think that pages was the best way to do it.
Right now I'm using this code:
<?php
$productos = new WP_Query(array(
'post__in'=> array(81, 83),
'orderby'=>'title',
'order'=>'ASC'
)
); if ($productos->have_posts()) : while ($productos->have_posts()) : $productos->the_post();
?>
It display the posts with the id of 81 and 83, I would like to show post by slug using 'name' as the codex says because is going to take some time to be checking the ids of the new post, instead of using the name of every new product but It doesn't work in array or I'm doing something wrong.
Now I will love to make something like this work
$names = get_post_meta($post->ID, "names", $single = true);
$productos = new WP_Query(array(
'name'=> array($names),
'orderby'=>'title',
'order'=>'ASC'
)
);
So every time I publish a new page I just write the slugs of the posts that I want to include in the page in a custom field, as you can see I'm not very good with php but I trying to learn and I search a lot for something that could work before asking in here.
I try the ggis inline post plugin and although it works I need the id for every post I want to include and I will need to edit the plugin because I want a different order in the output of the post thats why I don't like to depend to much on plugins.
Update:
So I'm now looking if I can make this using shortcodes, right now I have this:
function producto_func($atts) {
extract(shortcode_atts(array(
'nombre' => ''
), $atts));
global $post;
$pieza = get_page_by_title($nombre,OBJECT, 'post');
echo '<h1>'. $pieza->ID . '</h1>';
}
add_shortcode('producto', 'producto_func');
enter code here
So I just enter the shortcode [producto nombre="ff 244"] in the page and it show its ID, and I can add any number of shortcodes depending on the number of post I need.
But how can I show the entire content of the post.
Any idea?
I find I solution using Shortcodes.
So I put this on my functions.php page
function productos($atts, $content = null) {
extract(shortcode_atts(array(
"slug" => '',
"query" => ''
), $atts));
global $wp_query,$post;
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query(array(
'name'=> $slug,
));
if(!empty($slug)){
$query .= '&name='.$slug;
}
if(!empty($query)){
$query .= $query;
}
$wp_query->query($query);
ob_start();
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<h1><?php the_title(); ?></h1>
<div><?php the_content() ?></div>
<?php endwhile; ?>
<?php $wp_query = null; $wp_query = $temp;
$content = ob_get_contents();
ob_end_clean();
return $content;
}
add_shortcode("producto", "productos");
And in my page template I just write [producto slug="MY-SLUG"] and that way I can display multiple post just with the slugs. Hope someone find this useful.
From the Wordpress Codex:
Display post by slug:
$query = new WP_Query( 'name=about-my-life' );
Display page by slug:
$query = new WP_Query( 'pagename=contact' );
UPDATE
Try changing this:
'name'=> array($names),
To this:
'name'=> $names,
The 'name' - and 'pagename' - parameter does not take in an array. Only a string. A comma delimited list SHOULD give you what you need from within your Custom Fields titled "names", though I haven't tested this approach.
Also, thank you for using WP_Query instead of query_posts.