I have a number of flexible content fields set up.
One of these is called 'product-picker' and another is called 'product-information'.
The product picker uses the post object functionality to pull in selected products.
I can pull in the default wordpress parts such as:
<?php the_post_thumbnail(); ?>
But I can't pull in the values within 'product-information'.
Is this actually possible? Extracts of code below:
<?php if( have_rows('content_blocks') ):
while ( have_rows('content_blocks') ) : the_row();
if( get_row_layout() == 'product_information' ):
include("inc/product-information.php");
elseif( get_row_layout() == 'products_picker' ):
include("inc/products-picker.php");
endif;
endwhile;
endif; ?>
Then within 'products-picker' I am using the following code:
<?php foreach( $post_objects as $post): ?>
<?php setup_postdata($post); ?>
<div class="product">
<?php the_post_thumbnail(); ?>
<div class="description">
/* Echoing post ID returns correct value */
<h1><?php echo $post->ID; ?></h1>
/* Does not work */
<h1><?php the_sub_field('heading', $post->ID); ?></h1>
/* Does not work */
<?php the_sub_field('description', $post->ID); ?>
</div> <!-- .description -->
</div> <!-- .product-->
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
I might have the structure of my content wrong.
Maybe you can't call flexible content within flexible content?
But I thought it should be possible to do this in light of the fact that the default fields such as:
the_post_thumbnail();
and
the_title();
work.
All your time and help is greatly appreciated.
You have to set up the flexible rows part for each of your post objects. So after you setup your post data, replicate your
if( have_rows('content_blocks') ):
while ( have_rows('content_blocks') ) : the_row();
with the product information fields for each post object.
Check out https://www.advancedcustomfields.com/resources/flexible-content/ and the nested loop example. I think that should get you a little closer.
Related
I'm trying to create the single.php post page for a WordPress blog. I have used the Loop to pull through the actual content but would also like to show the tags (if there are any)!
The first code snippet works perfectly and displays everything I want it to, but is it the best way to write it? Can I use 2 if statements next to each other or is this bad practice? I have tried both methods: 2 IF statements works but 1 IF statement doesn't...see below!
Thanks in advance!
Working Code Snippet (using 2 IF statements)
<?php get_header();?>
<div class="blog-content row">
<div class="col">
<?php if(have_posts()) : while(have_posts()) : the_post();?>
<p class="single-date"><?php echo get_the_date();?></p>
<?php the_content();?>
<?php endwhile; else: endif;?>
<?php
$tags = get_the_tags();
if( $tags ) :
foreach( $tags as $tag ) : ?>
<div class="single-tag">
<a href="<?php echo get_tag_link( $tag->term_id);?>">
<?php echo $tag->name;?></a>
</div>
<?php endforeach; endif;?>
</div>
</div>
Invalid Code Snippet (attempt to use only 1 IF statement)
Here, I get the following warning: Invalid argument supplied for foreach()
<?php get_header();?>
<div class="blog-content row">
<div class="col">
<?php if(have_posts()) : while(have_posts()) : the_post();?>
<p class="single-date"><?php echo get_the_date();?></p>
<?php the_content();?>
<?php endwhile;?>
<?php $tags = get_the_tags();
foreach( $tags as $tag ) : ?>
<div class="single-tag">
<a href="<?php echo get_tag_link( $tag->term_id);?>">
<?php echo $tag->name;?></a>
</div>
<?php endforeach; endif; ?>
</div>
</div>
<div class="comments-sec">
<h4>Comments</h4>
<?php comments_template();?>
</div>
EDIT 1.2:
Can I use 2 if statements next to each other or is this bad practice?
Yes you can, but in your case you can't... tags are passed after the post has been called upon. You need to have a post to check if that post has tags.
In your case, you're talking about two different loop, one is for posts and one is for tags, Both if statement are not related. You're running a loop for tags inside a loop for posts.
Best practice is to have a fallback every time
<?php
//START Posts loop
if ( have_posts() ):
while ( have_posts() ):
the_post();
//IF posts exist
echo the_title().'<br/>'.the_content();
//START Tags loop
if( has_tag() ) {
//IF tags exist
echo the_tags();
} else {
//IF no tags exist, then fallbak
echo 'No tags yet!';
};
//END Tags loop
endwhile; else:
//IF no posts exist, then fallbak
echo 'No posts yet!';
endif;
//END Posts loop
?>
Additionally you can use has_tag() and the_tags().
More # https://developer.wordpress.org/reference/functions/has_tag/ for has_tag()
More # https://developer.wordpress.org/reference/functions/the_tags/ for the_tags()
I have a page with 3 custom fields. For each of these fields I need to use a template. So what I do is
<?php if( have_rows('page_block') ):?>
<?php while ( have_rows('page_block') ) : the_row();?>
<?php if( get_row_layout() == 'content_block' ):?>
<?php get_template_part("content-block");?>
<?php elseif( get_row_layout() == 'slider' ):?>
<?php get_template_part("slider"); ?>
<?php elseif( get_row_layout() == 'news_block' ):?>
<?php get_template_part("news-block"); ?>
<?php endif; ?>
<?php endwhile; ?>
....
<?php endif; ?>
The problem is that only the first two fields are displayed on the page. So when the order of the fields is
Content-block
Slider
News-block
Only the content-block and slider are shown. When I place the slider at the bottom, only the content-block and news-block are displayed.
Any idea what the problem might be?
EDIT
Forgot to mention that when I echo the get_row_layout(), I get the names of all 3 fields.
<?php if( have_rows('page_block') ):?>
<?php while ( have_rows('page_block') ) : the_row();?>
<?php echo get_row_layout(); ?>
<?php endwhile; ?>
<?php endif; ?>
Displays content_block slider news_block
Found the problem. The slider template was using a WP_Query and I had to add <?php wp_reset_query(); ?> to fix the problem. I think the query broke the loop or something like that. Either way, problem was fixed by adding said line!
i'm trying to build a navigation for a website, and i'm struggling with it. I'm trying to make a foldable navigation that shows only categories at first, when clicked, they link to the category, and show current category posts only.
I came this far:
<?php
// get all the categories from the database
$cats = get_categories();
// loop through the categries
foreach ($cats as $cat) {
// setup the cateogory ID
$cat_id= $cat->term_id;
// Make a header for the cateogry
echo '<h2><a href="./?cat='.$cat->term_id.'">'.$cat->name.'</h2>';
// create a custom wordpress query
query_posts("cat=$cat_id");
// start the wordpress loop!
if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php // create our link now that the post is setup ?>
<?php the_title(); ?><br>
<?php endwhile; ?> <?php endif; // done our wordpress loop. Will start again for each category ?>
<?php } // done the foreach statement ?>
The problem with this is: when i fill in the query so that it only takes the current cat, it displays current cat posts on both my categories.
this is what i want for the nav actually:
graphic design
Other Projects
when clicked on graphic design:
graphic design
Project 1
Project 2
Other Projects
when clicking on Other projects:
graphic design
Other Projects
Project 1
Project 2
so basically:
- when clicking from Index page to a category, only that category should expand
- when clicking the other category, the current category changes, so the previous category collapses and the other one expands.
and a bonus: is it possible, that when on a single post, there expands another level of info? for example, a few custom fields per post. like this:
graphic design
Other Projects
Project 1
custom field 1
custom field 2
…
Project 2
thank you very much
Try this code
<?php $article_categories = get_categories(array(
'child_of' => get_category_by_slug('graphic design')->term_id
));
$talentChildren = get_categories(array('child_of' => get_category_by_slug('Project 1')->term_id));
?>
<div id="content" class="narrowcolumn" role="main">
<?php if (have_posts()) : ?>
<div class="post-list">
<?php foreach($talentChildren as $talent): ?>
<?php
$talentSubChildren = new WP_Query();
$talentSubChildren->query(array('category_name' => $talent->slug));
?>
<h2><?php echo $talent->name; ?></h2>
<ul>
<?php while ($talentSubChildren->have_posts()) : $talentSubChildren->the_post(); ?>
<li>
<?php talent_thumbnail(); ?>
<h4>
<?php the_title(); ?>
</h4>
<p><?php the_excerpt(); ?></p>
read on »
</li>
<?php endwhile; ?>
</ul>
<?php endforeach; ?>
<?php if($wp_query->max_num_pages!=1):?>
<div class="pagination">
<?php previous_posts_link('« prev') ?>
<span class="current"><?php echo $wp_query->query['paged']; ?></span>
of <span class="total"><?php echo $wp_query->max_num_pages; ?></span>
<?php next_posts_link('next »') ?>
</div><!-- .pagination -->
<?php endif; ?>
</div>
<?php else : ?>
<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here.</p>
<?php get_search_form(); ?>
<?php endif; ?>
</div>
Ive gotten a bit further, but i need some help now.
<?php
// get all the categories from the database
$cats = get_categories();
// loop through the categries
foreach ($cats as $cat) {
// setup the cateogory ID
$cat_id= $cat->term_id;
// Make a header for the cateogry
echo '<h2><a href="./?cat='.$cat->term_id.'">'.$cat->name.'</h2>';
// create a custom wordpress query
query_posts("cat=$cat_id"); ?>
<?php if (in_category($cat_id)) { ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php // create our link now that the post is setup ?>
<?php the_title(); ?><br>
<?php endwhile; ?> <?php endif; // done our wordpress loop. Will start again for each category ?>
<?php } else { ?>
<?php } ?>
<?php } // done the foreach statement ?>
So this is working now: it lists the posts per category, if in the current category, else it doesnt show anything.
now what i still want: i want to add something extra within the loop IF i'm on a single page. I have this code:
<?php wp_reset_query(); ?>
<?php if (is_single('84')) { ?>
Yes
<?php } else { ?>
no
<?php } ?>
But that would mean i have to break a query in the middle of a loop. and the is_single thing does not work inside a loop / without the query reset.
I want it looking like this with above code:
Graphic Design
project 1 (for example id=84)
Yes
project 2 (for example id=101)
No
thanks
Ok so I am having issues with linking to next and previous posts...
Here is my code:
<?php get_header(); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
...
<div id="project-prev"> <?php previous_post_link('Prev'); ?> </div>
<div id="project-next"> <?php next_post_link('Next'); ?> </div>]
...
<?php endwhile; // end of the loop. ?>
<?php endif; ?>
<?php get_footer(); ?>
I have read places that next/prev posts requires a 'new WP_Query' query, but have had no such luck. There is no next/prev link rendered out on my site, using the above.
As always appreciate solutions and pointers.
Many thanks
Have you tried following (according to Wordpress codex)
<?php next_post_link('<strong>%link</strong>'); ?>
<?php previous_post_link('<strong>%link</strong>'); ?>
In your divs ... :) If you still encouter problems, then just try something like:
<?php echo get_previous_posts_link('Prev'); ?>
<?php echo get_next_posts_link('Next'); ?>
Should work.
EDIT:
<div id="project-prev"><?php previous_post_link('%link', 'PREV'); ?></div>
<div id="project-next"><?php next_post_link('%link', 'NEXT'); ?></div>
First try and get the next and previous posts.
<?php
$previous_post_url = get_permalink(get_adjacent_post(false, '', true));
$next_post_url = get_permalink(get_adjacent_post(false, '', false));
?>
And then create to <a> tags and echo out the URLs we set above.
<?php if ( $previous_post_url != get_the_permalink() ) : ?>
Previous Project
<?php endif; ?>
<?php if ( $next_post_url != get_the_permalink() ) : ?>
Next Project
<?php endif; ?>
One reason that could make these links not to show is having the posts set as draft. Only published posts will make the previous and next post links render.
I want to display a list of post titles from a custom taxonomy in a particular order.
I thought the best way to control the order would be to add a custom field and sort on that custom field.
The problem I'm having is that I'm trying to use the built-in functionality of Wordpress and I can't find a way to add sort functionality.
My Scenario looks like this
The calling url is ...com/taxonomy/term
This calls up a template, the filename of which is taxonomy-taxonomyname-term.php
My template is simply the index.php template renamed and edited to contain this loop
<?php if ( have_posts() ) : ?>
<?php twentyeleven_content_nav( 'nav-above' ); ?>
<ul>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php twentyeleven_content_nav( 'nav-below' ); ?>
<?php else : ?> etc
This displays a list of titles but I can't find a way to control the order of the titles.
The only way I've seen to set the order of a group of posts is to define the order of the posts in the query. But of course in this case I dont have a query because I already have the posts via the calling url.
Is there any way to add sort functionality without adding another query or is the query mandatory.
Suppose your custom fields is my_date
You can create custom query like this.
query_posts('meta_key=my_day&meta_compare=<=&meta_value=20&orderby=meta_value&order=DESC');
To use it
<?php if ( have_posts() ) : ?>
<?php twentyeleven_content_nav( 'nav-above' ); ?>
<ul>
<?php /* Start the Loop */
query_posts('meta_key=my_day&meta_compare=<=&meta_value=20&orderby=meta_value&order=DESC');
?>
<?php while ( have_posts() ) : the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php twentyeleven_content_nav( 'nav-below' ); ?>
<?php else : ?> etc
For more info http://wpengineer.com/1915/sort-posts-custom-fields/