I'm building a simple sidebar widget to display a loop of a custom post type "shows".
Each "show" has about 3 custom fields, which i'd like to output via the loop as well. This is the code i'm using:
This is the loop within my plugin code:
<?php
// WIDGET CODE GOES HERE
$args = array( 'post_type' => 'shows', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$month = get_post_meta($post->ID,'month-abbreviation',true);
$date = get_post_meta($post->ID,'date',true);
$citystate = get_post_meta($post->ID,'city-state',true); ?>
<article class="sidebar-show clearfix">
<a class="show-link" href="<?php the_permalink(); ?>">
<div class="date-box">
<span class="month"><?php echo $month; ?></span>
<span class="date"><?php echo $date; ?></span>
</div>
<div class="venue-box">
<?php echo "<h4>".get_the_title()."</h4>"; ?>
<?php echo "<p>".$citystate."</p>"; ?>
</div>
</a>
</article>
<?php endwhile;
wp_reset_query();
wp_reset_postdata();
?>
<?php
echo $after_widget;
}
}
add_action( 'widgets_init', create_function('', 'return register_widget("ShowsSidebarWidget");') );?>
This code pulls in the post titles, but doesn't display the custom fields month-abbreviation, date, and city-state.
What's missing here?
EDIT: Removed double quotes after avexdesign's reply.
are you sure $post->ID is available in this context?
maybe you should try: get_the_ID() instead.
Ok, I see a few things you can try.
month-abbreviation - should be single quotes. Like 'month-abbreviation'
Try taking out: $month= $date= and $citystate =
Might need echo
So Try:
echo get_post_meta($post->ID,'month-abbreviation', true);
echo get_post_meta($post->ID,'date', true);
echo get_post_meta($post->ID,'city-state', true);
Try something like:
<?php $loop = new WP_Query( array( 'post_type' => 'shows', 'posts_per_page' => 10 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<span class="month"><?php echo get_post_meta($post->ID, 'month-abbreviation', true); ?></span>
<span class="date"><?php echo get_post_meta($post->ID, 'date', true); ?></span>
and so on... for your other custom IDs
Related
I have a WP query that is looping all my projects with the same code. It's working great, but now I need to change the code based on a custom taxonomy. I basically want to make an if/else loop where if the custom taxonomy contains a certain value, to have x code, and if the custom taxonomy does not contain that certain value, to have y code. Is there a way to add this if/else statement to the "if ( $the_query->have_posts() ) :" section? Or do I need to lay this out differently?
Thank you!
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'orderby' => 'title',
'order' => 'ASC',
'post_type' => 'projects',
'posts_per_page' => 24,
'paged' => $paged,
);
$the_query = new WP_Query($args);
if( $the_query->have_posts() ) : while ($the_query->have_posts() ) : $the_query->the_post();
?>
EDIT: FluffyKitten (great name) asked for more info so here it is.
My custom taxonomy is called "project_services" and I want all projects with the service "commercial-damages" to loop through with one code, and all other projects to loop through with another code. I tried my crack at it below, but know that my part in the if loop is extremely wrong. I'm really not sure how to add that if condition in there since it's a WP query.
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'orderby' => 'title',
'order' => 'ASC',
'post_type' => 'projects',
'posts_per_page' => 24,
'paged' => $paged,
);
$the_query = new WP_Query($args);
if( $the_query->have_posts() ) : while ($the_query->have_posts() ) : $the_query->the_post();
if (get_field('project_services') == 'commercial-damages'):
?>
<div class="projects-column">
<div class="project-image">
<?php $image = get_field('image');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" class="project-image-tester" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
<div class="project-info">
<span class="proj-name" style="font-weight: bold;"><b><?php the_title(); ?></b></span>
</div>
<div class="project-onhover">
<span class="proj-title"><p><b><?php the_title(); ?></b></p></span>
<span class="proj-details">
<p><span style="font-weight: bold;">LOCATION: </span><?php the_field('location'); ?></p>
<p><span style="font-weight: bold;">CLAIM VALUE: </span><?php the_field('project_value'); ?></p>
<p><span style="font-weight: bold;">SERVICES: </span> <?php echo wp_strip_all_tags( get_the_term_list( $post->ID, 'project_services', '', ', ', '' ) ); ?> </p></span>
</div><!--projects-on-hover-tester -->
</div><!--project-image-->
</div><!--projects-column -->
<?php else: ?>
<div class="projects-column">
<div class="project-image">
<?php $image = get_field('image');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" class="project-image-tester" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
<div class="project-info">
<span class="proj-name" style="font-weight: bold;"><b><?php the_title(); ?></b></span>
</div>
<div class="project-onhover">
<span class="proj-title"><p><b><?php the_title(); ?></b></p></span>
<span class="proj-details">
<p><span style="font-weight: bold;">LOCATION: </span><?php the_field('location'); ?></p>
<p><span style="font-weight: bold;">PROJECT VALUE: </span><?php the_field('project_value'); ?></p>
<p><span style="font-weight: bold;">SERVICES: </span> <?php echo wp_strip_all_tags( get_the_term_list( $post->ID, 'project_services', '', ', ', '' ) ); ?> </p></span>
</div><!--projects-on-hover-tester -->
</div><!--project-image-->
</div><!--projects-column -->
<?PHP endif; ?>
<?php
endwhile;
endif;
?>
<?php wp_reset_postdata(); ?>
You just want to change the way you are processing the post details in your loop, so you don't need to make any changes to the loop itself. What you want to do is check the custom field inside the loop and then decide what to do with it.
Also FYI, you are using the_field - this will display the result to the screen immediately. Instead, to use the value without automatically displaying it, use get_field
Take a look at this - there are additional comments to help explain what it happening:
<?php
$the_query = new WP_Query($args);
// Standard while loop to get each of the returned posts one by one -
// we don't care what the post type is here, we just load it into the global "post" so we can use it...
if( $the_query->have_posts(); while ($the_query->have_posts() ) : $the_query-
>the_post();
// Now the details of that post are loaded into the global "post",
// so we can check the values in that post
// Check if the current post in out loop has commercial-damages
if (get_field('project_services') === 'commercial-damages'):
?>
// do whatever you want with the commercial-damages posts...
<?php else: ?>
// do whatever you want with the other posts
<?php endif; ?>
<?php
endwhile;
endif;
?>
Thank you to FluffyKitten who helped me work through this. I wasn't able to get an if loop to work exactly how outlined in the question, but I tried a few other approaches and just got one to work. I created an if / else statement in the HTML that I am looping. There I put if this field is empty, use another which I created and labeled differently to solve my problem. Below is the code:
<?php if( get_field('project_value') ): ?><p><span style="font-weight: bold;">PROJECT VALUE: </span><?php the_field('project_value'); ?></p>
<?php else: ?>
<p><span style="font-weight: bold;">CLAIMS VALUE: </span><?php the_field('claims_value'); ?></p><?php endif; ?>
I have created a new template page and I am displaying custom post type in that page as follows,
<div class="col-sm-4">
<?php $i = 1 ?>
<?php $posts = get_posts(array(
'post_type' => 'astroalbums',
'posts_per_page' => -1
));
foreach ($posts as $post) : start_wp(); ?>
<?php if ($i == 1): ?>
<?php $link = get_permalink($post->ID); ?>
<?php the_title( '<h3 class="entry-title">', '</h3>' );?>
<?php the_post_thumbnail(); ?>
<?php endif; ?>
<?php if($i == 3){$i = 1;} else {$i++;} ?>
<?php endforeach; ?>
My custom post type is "astroalbums" and I want to use it dynamically. I have 4 custom post types. I want to create new page in dashboard and assign the above page template i have created. and each page will call different custom post type.
It will be really great help
Thank you,
Trupti
You're retrieving the posts correctly, but it's seems that the problem is inside the foreach loop. As the default WordPress loop is not being used, you need to call functions which receive the post id as parameter or use the properties present in the $post object (which is an instance of the WP_POST class) in order to display the data.
One possible solution:
<?php
$posts = get_posts([
'post_type' => 'astroalbums',
'posts_per_page' => 1
]);
?>
<?php foreach( $posts as $post ): ?>
<?php $link = get_permalink( $post->ID ); ?>
<h3 class="entry-title">
<a href="<?php echo esc_url( $link ); ?>" rel="bookmark">
<?php echo get_the_title( $post->ID ); ?>
</a>
</h3>
<a href="<?php echo esc_url( $link ); ?>">
<?php echo get_the_post_thumbnail( $post->ID ); ?>
</a>
<?php endforeach; ?>
I didn't find enough resource to know about owl carousel WordPress plugin. If you know please explain.
I'd get the image URL and simply loop through the post instances inside a While loop (or THE loop) - essentially, the basic loop for getting post assets.
I'm using the toolset plugin and calling the elements where needed using said loop.
<ul class="attributes">
<?php
$values = array(
// 'author_name' => 'author',
'post_type' => 'attribute',
'orderby' => 'id'
);
$query = new WP_Query( $values );
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post();
$name = get_post_meta($post->ID, 'wpcf-value-name', true);
?>
<li>
<div class="values-content">
<div class="title">
<p><?php echo $name; ?></p>
</div>
<div class="text">
<p><?php echo get_post_meta($post->ID, 'wpcf-value-text', true); ?></p>
</div>
<div class="icon">
<?php $icons = get_post_meta($post->ID, 'wpcf-value-icon', true); ?>
<img src="<?php echo $icons; ?>" alt="<?php echo $name; ?>">
</div>
</div>
</li>
<?php endwhile; else: ?>
<p>Sorry, there are no posts to display</p>
<?php endif; ?>
</ul>
If you need any help with this, let me know. If you decide to stick with the plugin, that's fine but I'm not acquainted with it :) I'm calling the 'attributes' class name in the jQuery:
// carousel for about page values
jQuery(".attributes").owlCarousel({
autoWidth: true,
items: 1
});
This is what my code looks like:
<?php $loop = new WP_Query( array( 'post_type' => 'home_templates_list', 'posts_per_page' => 20, 'orderby' => 'ID', 'order' => 'DESC' ) ); ?>
<?php $isFirstTemplate = true; ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="item<?php if($isFirstTemplate) { echo ' active'; $isFirstTemplate = false; } ?>">
<?php $templateLink = get_the_excerpt(); ?>
<div class="col-sm-6"><img class="img-responsive" src="<?php echo wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>" /></div>
<?php if ($loop->current_post + 1 < $loop->post_count) { ?>
<?php $loop->next_post(); ?>
<?php $templateLink = get_the_excerpt(); ?>
<div class="col-sm-6"><img class="img-responsive" src="<?php echo wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>" /></div>
<?php } ?>
</div>
<?php endwhile; ?>
I am trying to make the main loop to be incremented by two. ie, two posts per iteration. Tried using the next_post() method but it appears to be that the get_the_excerpt() and $post->ID are still pointing to the first post!
Is there a better way for this? Thank you
EDIT
What I want to do is, two items(posts) per loop. Say for example, a simple while loop example of what am trying to achieve would look like this:
while(i < 10)
{
echo a[i];
echo a[i+1];
i=i+2;
}
Like this, I want to print two posts per iteration.
I have added tags to my Custom Post Type.
Now I want to use them to create a isotope portfolio, I can load all tags with this code:
<?php $args = array( 'post_type' => 'portfolio', 'posts_per_page' => 24;
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="all <?php echo $tag->slug; ?>">
<?php echo the_post_thumbnail(); ?>
<p><?php the_title(); ?></p>
</div>
<?php endwhile; ?>
But now I want to add the all tags that from each portfolio item to the class="".
With <div class="<?php $tag->slug; ?>"> I just get the last tag of all the tags that are used.
I know there are already a lot of posts about this problem, but every post I have found does not seem to work for me.
It now works with the following code:
<?php $tags = get_the_tags();
$tag = wp_list_pluck( $tags, 'slug' );
$tagToClass = implode(" ", $tag);
?>
And then use <?php echo $tagToClass ?>