hopefully my description will be clear!
I'm basically trying to create an area for portfolio work to be displayed. I've created a custom post type in Wordpress and i'd like to bring it through to the front-page.php. I have designated areas for where i'd like the work to be displayed (see image). The dark grey areas are where i'd like to place the portfolio items ONLY. Each grey area should show 1 portfolio item
I'm using this script to pull in the custom post type:
<?php
$args = array( 'post_type' => 'Portfolio', 'posts_per_page' => 4 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<div class="home-recent-thumb">'; the_post_thumbnail(); echo '</div>';
echo '<div class="home-recent-title">'; the_title(); echo '</div>';
echo '<div class="home-recent-copy">'; the_excerpt(); echo '</div>';
endwhile;
?>
How do I designate the areas in the php so that it shows 4 posts inside the correct element?
Since your layout isn't necessarily conducive to the traditional "loop" functionailty - meaning, you aren't placing the results next to each other - and you haven't mentioned any outside libraries (like masonry or isotope) - I would just do individual queries for each of the four squares.
For the first custom post type square - it would like:
$query = new WP_Query( 'post_type' => 'Portfolio', 'posts_per_page' => 1 );
And the second (to nth) would look like:
$query = new WP_Query( 'post_type' => 'Portfolio', 'posts_per_page' => 1, 'offset=1' );
Where your offset continues to increase. In my mind this continues to stay dynamic and is simple enough for four posts. Else your jumping into just a bunch of additional logic with the other squares.
<?php
$portfolioPosts = get_posts([
'post_type' => 'Portfolio',
'posts_per_page' => 4
]);
//first section
?>
<div class="home-recent-thumb"><?php the_post_thumbnail($portfolioPosts[0]->ID); ?></div>
<div class="home-recent-title"><?php echo $portfolioPosts[0]->post_title ?></div>
<div class="home-recent-copy"><?php echo $portfolioPosts[0]->post_excerpt; ?></div>
<?php
//later in code
//second section
?>
<div class="home-recent-thumb"><?php the_post_thumbnail($portfolioPosts[1]->ID); ?></div>
<div class="home-recent-title"><?php echo $portfolioPosts[1]->post_title ?></div>
<div class="home-recent-copy"><?php echo $portfolioPosts[1]->post_excerpt; ?></div>
//et cetera
Related
I have a static page with 10 containers located at different places in the index.php file, i want to show the 10 latest posts. I want to echo the data for a post (title,text,thumbnail,author,time) like this:
<div class="text">
<?php
echo '$textfrompost3';
?>
</div>
<div class="title">
<?php
echo '$titlefrompost3';
?>
</div>
...
...
My PHP file:
<?php
// the query
$the_query = new WP_Query( array(
'category_name' => 'Allgemein',
'posts_per_page' => 10,
"orderby" => "date",
"order" => "DESC"
));
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
// insert values into variables
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Thanks in advance, i appreciate any help you can provide.
I'd simply replace your call to WP_Query with get_posts() which will provide an array of post objects.
$latest_posts = get_posts( array(
'category_name' => 'Allgemein',
'posts_per_page' => 10,
) );
I've dropped order and orderby in the example above. While you may well have a different global setting for the number of posts to display, it's unlikely you're modifying the default ordering.
Documentation: https://codex.wordpress.org/Template_Tags/get_posts
So let's say I want to output the title of the third post, I could do the following:
// let's be sure we have a third post.
if ( isset( $latest_posts[2] ) ) {
echo $latest_posts[2]->post_title; // remember arrays have a zero-based index.
}
Hoping you can help me with my problem.
I am trying to pull posts that have match the same word from another custom post type to the standard WP posts.
For example: A news post about "BMW M5 being announced with new safety features" and then a product page about a BMW M5.
On the news post I want to have a click out to the product page and pull through some basic information from the CPT and want to link the two by their name i.e post title of the CPT. As the "news" posts will have more than just "BMW M5" I'd need to do a kind of reversal wordpress query where it looks for all news posts which match the car product custom post type but on the news page.
I tried using a piece of code which I found on here that searches for similar posts using a modified query in the loop args but because I think it is meant to work for more words than the product pages offer it returns nothing.
For a stop gap I am looking for an exact tag on both but it is not ideal as it is open to error on the human side and would like to automate it as much as possible.
Here is the current code using the tags.
$tags = wp_get_post_tags($post->ID);
$tag_ids = array();
foreach ($tags as $individual_tag) $tag_ids[] = $individual_tag->slug;
$args = array(
'posts_per_page' => 1,
'post__not_in' => array($post->ID),
'post_type' => 'games',
'post_status' => array('publish', 'draft'),
'tax_query' => array(
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => $tag_ids
)
)
);
$wp_query = new WP_Query($args);
if ($wp_query->have_posts()){
while ( $wp_query->have_posts() ) { $wp_query->the_post(); ?>
<?php $src = wp_get_attachment_image_src( get_field('image'), $size, false ); ?>
<?php
$originalDate = get_field('release_date');
$newDate = date("d/m/Y", strtotime($originalDate));
?>
<?php if(get_post_status() == 'publish'){ ?>
<div class="hub" style="background-image: url('<?php echo $src[0]; ?>')">
<dl id="gdbinfobar">
<dt class="info_bar_title">
<h3 style="color: #FFF"><?php the_title(); ?></h3>
</dt>
<dt class="info_bar_release">
Released on: <?php echo $newDate; ?>
</dt>
<dt class="info_bar_model">
<?php echo get_field('t_platform'); ?>
</dt>
</dl>
<a class="btn" href="<?php the_permalink()?>">View Game</a>
</div>
I'm using wordpress with custom post type UI plugin and ACF plugin.
Trying to build a “single” template with multiple feeds of custom post types by custom custom taxonomy. Using this code, with a few variations to figure out what am i doing wrong.
Got 2 pieces of code like this in a row
<?php if( get_field('collectiona') ):
$argsc = array(
'post_type' => 'products',
'product-collections' => get_field('collectiona'),
);
$prods2 = new WP_Query( $argsc );
if( $prods2->have_posts() ) {
while( $prods2->have_posts() ) {
$prods2->the_post();
?>
Whatever post code
<?php
}
}
else {
echo '';
}
?>
<?php endif; ?>
collectiona is a taxonomy field. With the piece of code, shown above, it just shows all the “products” posts out there. I’ve also tried using a text field with taxonomy slug. It shows first feed perfectly fine, if i’m not using first if statement (<?php if( get_field(‘collectiona’) ): ?>), and if that statement is present- same thing happens. All the “products” are shown. However, even with first feed shown fine, 2nd feed still shows all the “products” out there. Despite what taxonomy slug says.
I’m trying to build it the way, admin could chose a dropdown taxonomy. Text field with taxonomy slug is just an example.
p.s.
I use term object
Full template code is here jsfiddle.net/pudfbxhv . I know jsfiddle is useless for wp templates, but that's a pretty big piece of code
EDIT
Here is updated code
<?php
$taxterms = get_field("collectiona"); ?>
<?php
$args = array(
'post_type' => 'products',
'tax_query' => array(
array(
'taxonomy' => 'product-collections',
'field' => 'id',
'terms' => $taxterm->term_id
)
)
);
$myquery = new WP_Query( $args );
if($myquery->have_posts()) : ?>
<ul>
<?php while ( $myquery->have_posts() ) : $myquery->the_post(); ?>
<li> <img src="<?php the_field('prod_featured_image'); ?>" onmouseover="this.src='<?php the_field('prod_hover_featured_image'); ?>'" onmouseout="this.src='<?php the_field('prod_featured_image'); ?>'" />
<h2><?php the_field('prod_subtitle'); ?></h2>
<p>$<?php the_field('prod_price'); ?></p>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); ?>
Well, that may be a perversion, but it worked for me.
$termss = get_field('collectiona');
$slll = $termss->slug;
$args = array(
'post_type' => 'products',
'product-collections' => $slll,
);
$lineblocks = new WP_Query( $args );
if( $lineblocks->have_posts() ) {
while( $lineblocks->have_posts() ) {
$lineblocks->the_post();
Also, gotta remember to put the following code after every array
<?php wp_reset_query(); ?>
I am creating a website that integrates a portfolio which uses custom post types, this was done based off of this tutorial.
So far it is exactly what I am looking for and works great except for one small detail. In order to fetch the posts from the new custom post type the author of the tutorial used the query_posts() codex. So the top of my portfolio page looks like this:
<?php
/* Template Name: Portfolio */
get_header();
query_posts('post_type=portfolio&posts_per_page=10');
?>
What I gather is that this declares "get posts from "post type" portfolio and show 10 per page". My problem is that I can't go get content from my portfolio page. It seems that now my portfolio page only fetches the content from the custom post type, and I can't use:
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; // end of the loop. ?>
to get content from the actual page.
This is what I am trying to do, I've replaced:
query_posts('post_type=portfolio&posts_per_page=10');
with:
add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
function add_my_post_types_to_query( $query ) {
if ( is_page( 8 ) && $query->is_main_query() )
$query->set( 'post_type', array( 'portfolio' ) );
return $query;
}
This seems like the right track, but it stills doesn't work. I'm not getting the posts from my custom post type.
Any ideas how I could modify this? I am also still learning so being clear with explanations would be greatly appreciated.
Thank you!
Editing the pre_get_posts will replace the original query and you will not have the content for your page at all. I would only recommend going this approach if you only wanted to display the content of your portfolio post type and not the content of your portfolio page.
For general post queries it is recommended to use WP_Query or get_posts.
http://codex.wordpress.org/Class_Reference/WP_Query
http://codex.wordpress.org/Template_Tags/get_posts
If you use the WP_Query function the wp_reset_postdata() will restore the post data back to the original so you can get the content of your original page.
$args = array(
'posts_per_page' => 10,
'post_type' => 'portfolio',
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
Now you will be able to use the original loop to show the content of your page
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; // end of the loop. ?>
Usually, I stick my query posts in a variable, like so:
$catid = get_cat_ID('My Category Name');
$args = array(
'posts_per_page' => 5,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'category' => $catid
);
$posts_array = get_posts($args);
Then you can loop it like so:
<?php foreach ($posts_array as $post) : setup_postdata($post);?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<?php endforeach; ?>
Finally, to access your page content you can use the variable $post, it's automatically set by wordpress. No need to add any more code than this to access your page content.
<?php foreach( $posts as $post ) : setup_postdata($post); ?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<?php endforeach; ?>
The foreach loop for your page content is a little overkill, and there is a better way to do it (most likely at least), but I haven't been bothered to look into it further yet! It works though!
I have used the Wordpress Types plugin to create two new Custom Post Types. ie. Hotel and Rooms. The Room post type is the child of Hotel. Along with that I have also added some custom fields in both post types. It includes generic details like Hotel Address, Phone Number, Email etc. Under Room post type, I have added room_image, room_price and room_description custom fields.
I am able to display these custom fields on the single hotel page along with room details. But I am stuck at the index page where I have to display a summary of Hotel details along with room price sorted in descending order. Here's the code that I used to display hotel details
<?php
// WP_Query arguments
$args = array (
'post_type' => 'hotel',
'order' => 'DESC',
'orderby' => 'date',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
<article>
<header>
<figure><img width="300" height="160" alt="Placeholder" src="<?php echo get_post_meta($query->post->ID,'wpcf-image',true);?>"><div class="fit-a"></div></figure>
<h3><?php echo $query->post->post_title;?></h3>
<?php
// do something
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();?>
How can I fetch the child room price based on the parent hotel ?
Inside the loop for each Hotel, you can access child posts of your Hotel type by calling the following function included with the Types plugin : types_child_posts('name_of_your_child_CPT')
In your case it should be something like this :
<?php
// WP_Query arguments
$args = array (
'post_type' => 'hotel',
'order' => 'DESC',
'orderby' => 'date',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
<article>
<header>
<figure><img width="300" height="160" alt="Placeholder" src="<?php echo get_post_meta($query->post->ID,'wpcf-image',true);?>"><div class="fit-a"></div></figure>
<h3><?php echo $query->post->post_title;?></h3>
<?php
// are there rooms for current hotel (room is the name of your child Custom Post type)
$rooms = types_child_posts('room');
foreach($rooms as $room)
{
echo '<div class="room">';
// here we display the title of a room
echo '<div class="room_name">'.$room->post_title.'</div>';
// here we display a custom field (price) of a room
echo '<div class="room_price">'.array_pop(get_post_custom_values('wpcf-room-price', $room->ID)).'</div>';
echo '</div>';
}
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();?>
I hope this can help you.
I hope I understood your question correctly
First we need to call the Hotel Query. Then display its image and custom meta. Then within the hotel query - display the room post and meta.
<?php
$parent = array( 'post_type' => 'hotel', 'post_parent' => 0, 'order' => 'DESC', 'orderby' => 'date');
$hotelparent = new WP_Query($parent);
if ( $hotelparent->have_posts() ) : while ($hotelparent->have_posts()) : $hotelparent->the_post();?>
<article>
<header>
<figure><img width="300" height="160" alt="Placeholder" src="<?php echo get_post_meta($hotelparent->ID,'wpcf-image',true);?>"><div class="fit-a"></div></figure>
<h3><?php the_title();?></h3>
</header>
<?php $child = array('post_type' => 'hotel', 'post_parent' => $post->ID, 'order' => 'DESC', 'hierarchical'=>1);
$rooms = new WP_Query( $child );
if ( $rooms->have_posts() ) : while ($rooms->have_posts()) : $rooms->the_post(); ?>
<?php echo get_post_meta($rooms->ID,'YOUR_META',true);?>
<?php endwhile; endif; ?>
</article>
<?php endwhile; endif; ?>