OK, so I need to display ACF custom field inside my posts loop in my custom category.php file. Here is the loop:
<div class="container">
<div class="row">
<?php
if ( have_posts() ) : ?>
<?php
/* Start the Loop */
while ( have_posts() ) : the_post();
?>
<div class="col-xs-12 col-sm-4">
<?php the_title( '<h2>', '</h2>' ); ?>
<div><?php MY_ACF_FIELD_GOES_HERE ?></div>
</div>
<?php
/* End the Loop */
endwhile;
?>
</div><!-- .row -->
</div><!-- .container -->
As you can see loop displays pages from category (titles), but I need also display short description. I know I could use:
<?php the_excerpt(); ?>
But not in this case because excerpt contains text that I do not need inside loop. So I need to create my own short description field for every page. How can I display it in category.php template? Custom field (my own short desc) is on all pages.
You can retrieve the ACF field value using get_field('field_name'). Example-
<?php
$args = array( 'post_type' => 'speakers', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<div class="entry-content">';
echo '<h2 class="speaker-name">';
the_title();
echo '</h2>';
echo '<img src="' . get_field('field_name') . '" alt="" />';
echo '<span class="speaker-title">';
the_field('title'); echo ' / '; the_field('company_name');
echo '</p>';
the_content();
echo '</div>';
endwhile;
?>
Related
I'm trying to output all posts under custom post type "philosophy" on a page in a list. Alternating between categories "img-left" and "img-right".
I can get the posts to display ALL "philosophy" posts however i want to lay out the posts in two layouts depending on their custom category.
If the category is "img-right" i want the post to be shown with the text on the left and image on the right and vice-versa for "img-left".
I have tried the below code which doesn't work at all.
<?php
$args = array( 'post_type' => 'philosophy', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
if( in_category( 'img-right' ) ):
while ( $loop->have_posts() ) : $loop->the_post();
echo '<div class="col-md-12"><h2>';
the_title();
echo '</h2></div><div class="row content"><div class="col-md-6"';
the_content();
echo '</div><div class="col-md-5 offset-1 float-right">';
the_post_thumbnail('array(100,100)');
echo '</div></div>';
endwhile;
endif;
?>
by removing the "if" and "endif" i have the code that lists all the posts in one layout. What I need is conditionals that can output both "img-right" and "img-left" layouts based on the post's category. The only layout shown in my example above is "img-right".
Any help would be greatly appreciated. This PHP is making my head spin!
So...with all the help from the guys answering i figured it out using the CSS approach touched on by #Mohsin.
Here is my code:
<div id="content" class="col-12" role="main">
<?php get_template_part('loops/page-content'); ?>
</div>
<div class="row">
<?php
$args = array( 'post_type' => 'philosophy', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<div class="row content"><div class="col-md-12"><h2>';
the_title();
echo '</div><div class="col-md-6">';
the_content();
echo '</div><div class="col-md-6">';
the_post_thumbnail();
echo '</div></div>';
endwhile;
?>
I then applied this:
.row.content:nth-child(even) {
flex-direction: row-reverse;
}
and we're golden.
Thank you to everyone who helped.
If I understand correctly:
<?php $args = array( 'post_type' => 'philosophy', 'posts_per_page' => 10 ); ?>
<?php $loop = new WP_Query( $args ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="col-md-12"><h2>
<?php the_title(); ?>
</h2></div>
<div class="row content">
<?php if( in_category( 'img-left' ) ): ?>
<div class="col-md-5 offset-1 float-left">
<?php the_post_thumbnail('array(100,100)'); ?>
</div>
<?php endif; ?>
<div class="col-md-6">
<?php the_content(); ?>
</div>
<?php if( in_category( 'img-right' ) ): ?>
<div class="col-md-5 offset-1 float-right">
<?php the_post_thumbnail('array(100,100)'); ?>
</div>
<?php endif; ?>
</div>
<?php endwhile; ?>
The above code will output all of the posts, with a thumbnail to the left if it is category img-left and thumbnail to the right if in img-right (and outputting both if it is categorized as both, and neither if it is neither category. You may want different behavior, but it should be simple to adjust the conditionals).
If your template becomes anymore complicated, I would recommend moving sections out to template-parts/using functions like get_sidebar().
I have a WordPress loop that is pulling an ACF field. I need to determine if the field names are the same and if so I want to wrap them in a div. I have created a custom index page, but we want to be able to style fields with the same author name as a dropdown. So I need to somehow compare if the are the same.
This is the site I am working on http://test.improveyourenglish.com/library/
So for instance I would like to wrap "Jane Austin" in a div so that I can style it as a dropdown.
Thanks so much any help is greatly appreciated.
This is the code I am currently using
add_action('genesis_loop', 'book_archive_page');
function book_archive_page() {
echo '<div class="left-side">';
echo '<p>The following titles are sorted by author surnames.</p>';
?><div class="enter"><a href="#$term->name"><?php echo $term->name; ?>
</div></a><?php
$post_type = 'book';
// Get all the taxonomies for this post type
$taxonomies = get_object_taxonomies( array( 'post_type' => $post_type )
);
foreach( $taxonomies as $taxonomy ) :
// Gets every "category" (term) in this taxonomy to get the
respective posts
$terms = get_terms( $taxonomy );
foreach( $terms as $term ) : ?>
<section class="category-section">
<div class="row">
<div class="span12">
<a name="<?php echo $term->name; ?>"><h2 style="padding-
top: 300px; margin-top: -300px;"><?php echo $term->name; ?></h2>
</a>
</div>
<?php
$args = array(
'post_type' => $post_type,
'posts_per_page' => -1, //show all posts
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $term->slug,
)
)
);
$posts = new WP_Query($args);
if( $posts->have_posts() ): while( $posts->have_posts() ) :
$posts->the_post(); ?>
<div class="span4">
<article class="inner-post clearfix">
<div class="inner-content">
<div class="author-archive-text"><?php the_field('author_full_name'); ?></div><div class="title-archive-book"><?php echo get_the_title(); ?></div>
</div>
</article>
</div>
<?php endwhile; endif; ?>
</div>
<hr>
</section>
<?php endforeach;
endforeach; ?>
<?php
}
echo '</div>';
If I understand what you want correctly, what you need to do is to record the "current" author name in a variable, and use it in your loop to compare it to the next author name. If it is a different author, then end the previous author's wrapper and start a new wrapper for the next author.
$current_author = ""; // variable to store the current author
$posts = new WP_Query($args); ?>
<div class="author-wrapper"> <!-- start the wrapper div for the first author -->
<?php if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post(); ?>
<?php
// check if we have a new author
if (the_field('author_full_name') != $current_author) {
// update current_author var to make the new author the current one
$current_author = the_field('author_full_name');
// close the previous author's wrapper and start a new one
?>
</div>
<div class="author-wrapper">
<?php } ?>
<div class="span4">
<article class="inner-post clearfix">
<div class="inner-content">
<div class="author-archive-text"><?php the_field('author_full_name'); ?></div><div class="title-archive-book"><?php echo get_the_title(); ?></div>
</div>
</article>
</div>
<?php endwhile; endif; ?>
</div> <!-- end the wrapper div for the last author -->
I'm trying to add another class to <div class="postthumbnail">. I wanted to style one post different. Therefore, after researching, I found that the best method is to use custom field to add class to the post.
This tutorial, How to style WP posts different, especially this section at bottom, explained how I can add class to the post using the custom field. I did so by giving the custom field a name "en_proceso_class" and the value, "en_proceso" which is an css class. But I'm confused by the last two codes I need to add. The tutorial wasn't clear on where I need to add them.
My original code is:
<?php
// set the "paged" parameter (use 'page' if the query is on a static front page)
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
// the query
$the_query = new WP_Query( 'cat=9&posts_per_page=9&paged=' . $paged );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php
// the loop
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<div class="proyectpost">
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="innerpost">
<div class="postthumbnail">
<?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
$image_src = wp_get_attachment_image_src( get_post_thumbnail_id(),'full' );
echo '<img src="' . $image_src[0] . '" width="100%" />';
} ?>
</div>
<div class="posttitle">
<h2><?php the_title(); ?></h2>
<span><?php echo get_post_meta($post->ID, 'location', true); ?></span>
</div><!-- .entry-header -->
<div class="postsubtitle">
<div class="datepanel">
</div>
</div>
</div>
</article><!-- #post-## -->
</div>
<?php endwhile; ?>
<div class="paginationbar">
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $the_query->max_num_pages,
'show_all' => True,
'prev_next' => False
) );
?>
</div>
<?php
// clean up after the query and pagination
wp_reset_postdata();
?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
This is section of the code that I'm trying to add the new code to:
<div class="innerpost">
<?php $custom_values = get_post_meta($post->ID, 'en_proceso_class'); ?>
<div class="postthumbnail <?php en_proceso_class('class-1 class-2 ' . $custom_variable); ?>">
<?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
$image_src = wp_get_attachment_image_src( get_post_thumbnail_id(),'full' );
echo '<img src="' . $image_src[0] . '" width="100%" />';
} ?>
</div>
What do I need to do to get it working?
The last part isn't 100% clear but I'll try to answer as best I can.
This piece of code:
$custom_values = get_post_meta($post->ID, 'en_proceso_class');
Gets the value of a post meta field name: 'en_proceso_class'. You actually need to set that first in order for this to work. And you need to add 'true' as another parameter to that function. See here for more info: https://developer.wordpress.org/reference/functions/get_post_meta/
Then there's this:
div class="postthumbnail <?php en_proceso_class('class-1 class-2 ' . $custom_variable); ?>">
Which calls a function named 'en_proceso_class' -- I don't think this is what you want to do. Unless you declared that function beforehand you'll to do something like:
div class="postthumbnail <?php echo 'class-1 class-2 ' . $custom_variable; ?>">
So the whole code put together would look like this:
<div class="innerpost">
<?php
// Get post meta that is already set
$custom_values = get_post_meta($post->ID, 'en_proceso_class', true);
?>
<div class="postthumbnail <?php echo 'class-1 class-2 ' . $custom_values; ?>">
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
$image_src = wp_get_attachment_image_src( get_post_thumbnail_id(),'full' );
echo '<img src="' . $image_src[0] . '" width="100%" />';
}
?>
</div>
Cheers
I have a few advanced custom fields pages, they all have the same structure using a repeater field. I want them all to share the same single.php file. (which i believe should be fine?)
All my php is in the right order as its working fine when i add this above if have posts:
<?php
// The Query
$args = array(
'post_type' => 'chemicals'
);
$the_query = new WP_Query( $args );
?>
But once it is removed it is not getting the post information.. Maybe I have my php structure wrong? im very new to php and wordpress so any help would be great.
<div class="section group">
<div class="col span_3_of_12">
<?php wp_nav_menu(
array(
'menu' => 'consumables',
'container' => 'div',
'container_class' => 'product-menu',
'menu_class' => 'product-menu',
)); ?>
</div>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if( have_rows('product') ): ?>
<div class="col span_3_of_12">
<?php while( have_rows('product') ): the_row();
// vars
$title = get_sub_field('product_title');
$thumbnail = get_sub_field('thumbnail');
$blurb = get_sub_field('main_blurb');
$technical = get_sub_field('technical');
$description = get_sub_field('description');
?>
<a href""><img class="open-image" src="<?php echo $thumbnail; ?>"></a>
</div>
<div class="col span_6_of_12">
<div class="product-details">
<h4><?php echo $title; ?></h4>
<p><?php echo $blurb; ?></p>
</div>
<div class="product-tech">
<div class="technical">
<h5>Technical</h5>
<p><?php echo $technical; ?></p>
</div>
<div class="technical">
<h5>Description</h5>
<p><?php echo $description; ?></p>
</div>
</div>
<?php endwhile; ?>
</div>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
I see that yours is a custom post. The file name of the template should be single-chemicals.php instead of single.php
From WordPress codex:
single-{post_type}.php If your custom post type were 'product', and/or query_var = "product", WordPress would look for
single-product.php to display the single or permalink of the post.
I have the current loop working to display posts, but I can't seem to get the title or content to populate. What am I missing?
<?php
// LOOP ARGUMENTS
$args = array( 'post_type' => 'cbd_slider', 'posts_per_page' => -1 ); // -1 Shows ALL Posts
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
// CUSTOM CONTENT
$slideLink = get_post_meta($post->ID,"slideLink",true);
$thumb = wp_get_attachment_image_src(get_post_thumbnail_id(), 'thumbnail_name');
$imgURL = (isset($thumb[0]) ? $thumb[0] : get_template_directory_uri() . "/images/placeholder.jpg");
?>
<li class="clearfix"><!-- Start of featured slide -->
<a href="<?php echo $slideLink ?>">
<img src='<?php echo get_template_directory_uri(); ?>/thumb.php?src=<?php echo urlencode($imgURL); ?>&h=330&w=496' alt='featuredimage' /></a>
<div class="description">
<h2>TITLE GOES HERE</h2>
<p>POST CONTENT GOES HERE</p>
more
</div>
</li><!-- End of featured slide --><?php /* END WHILE AND RESET QUERY */ endwhile; wp_reset_query(); ?>
Have you tried the_title? Substituting <?php the_title(); ?> for your TITLE GOES HERE and <?php the_content(); ?> for CONENT GOES HERE?