How to repeat ACF Flexible Content on single pages? - wordpress

Disclaimer: If you have never used ACF Flexible Content to build a site, please reserve the hate and all that - looking just for help.
I'm creating a website that's 100% modular, done with Flexible Content.
I have Pages such as:
Home, About, Services, Blog, Portfolio, Contact
All of those pages styled using Flexible Content, these are PAGES.
I have created a custom field for Services and Portfolio, and I used WP Query to display each individal item. These are CUSTOM fields. Show this field group if post type to Services. Not a page.
So when you click it, what happens you go to single-[custom-post-type].php, however I just have single.php to keep it dynamic, which works.
The problem lays that when I have showed the flexible content page templates in the Services or Portfolio, and I add few items, I need go to every single item and set the layout.
How can I do so the layout stays all the same in Service or Portfolio custom field?

Firstly create your ACF Master block, for example, a testimonial section, this would have the repeater fields inside the block etc. In this example, we will call it Testimonial Master.
Then Create another Flexible Content Field Called Testimonial Block with a checkbox that you check to display or hide.
You will now populate Testimonial Master on your selected Parent Page or on an Options page. You then should add the Testimonial Block to any page you are looking to have that block appear on.
You will then went to check that the Testimonial Block is on the page, if it is on the page then you will check if Testimonial Master has a value if it has a value then you can pull in the values from Testimonial Master.
<?php
if( have_rows('page_structure') ):
while ( have_rows('page_structure') ) :
the_row();
if( get_row_layout() == 'testimonial_block' ):
if( get_sub_field('testimonial_block_toggle') == 'show' ):?>
<div class="testimonials">
<?php the_sub_field('testimonial_master_title', 1); ?>
</div>
<?php endif;
endif;
endwhile;
endif;
?>
The "1" is the page ID, use this if you have populated the field on a master page. Or if you are using the ACF Options Page plugin change the "1" to "'options'".
Here is some further documentation on getting values from other posts including that of repeater fields.
https://www.advancedcustomfields.com/resources/how-to-get-values-from-another-post/
Edit: Custom Header as per your example
Make a custom header file and add your code for that header block.
Now make an advanced custom field for a text area. For example header_text_block, set it to a WYSIWYG editor.
Now in your code just use:
<?php the_field('header_text_block');?>
This will then use the exact same header on every page you pull it into, however it will let you edit the text individually for each page.
Edit: Example of what this code looks like and does
Also I think you are very confused, there will only be 1 php/html file? you could use page.php.
Here's an example of what I am saying, 1 php file:
<?php if( have_rows('page_structure') ):
while ( have_rows('page_structure') ) : the_row();
if( get_row_layout() == 'page_featured' ):
$image = get_sub_field('page_featured_image');
if( !empty($image) ):?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>">
<?php endif;
endif;
if( get_row_layout() == 'page_breadcrumbs' ):
if( !in_array( 'hide', get_sub_field('page_breadcrumbs_hide') ) ):?>
<?php if ( function_exists('yoast_breadcrumb') ) {yoast_breadcrumb('<p id="breadcrumbs">','</p>');}?>
<?php else :
endif;
endif;
endwhile;
endif;
?>
This then looks like this on the page editor:
You can then edit each section as you please and also drag and drop sections where you want them which enables easy split testing.
All the styling will be the same, just you can change the content and if you wanted you could add the ability to alter stylings pretty easily. And it only uses 1 PHP file for the template.

Related

Cant' Grab WordPress Built-in Posts Through Loop

Using WordPress 3.7.1 I am trying to display all Regular Post on my created page lest say TestPage. Here are the steps I took to do this:
1- Generate a Custom Page Template called:Test Page and loaded by following code
2- Generate a Page Called TestPage based on Test Page Template
after updating the page I am not getting any of Post on the page while I have already generated some!
<?php
/*
Template Name: Test Page
*/
?>
<?php get_header(); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1><?php the_title() ;?></h1>
<?php the_content(); ?>
<?php endwhile; else: ?>
<p>Sorry, this page does not exist</p>
<?php endif; ?>
<?php get_footer(); ?>
he abouve code actually is loading the page whit title and content of the TestPage and not by Posts!Can you please let me know why this is happening?
You must understand the difference between post and page.
A page is associated with a template. Through code, you can lists posts or other pages using this page /template.
The above code will only display the Page data
You don't want to create many pages using the same tempalte in order to show different data with the same tempalte. Instead you create posts and give them a category.
Then you can fetch these posts and display them in the same way asthe test-page.
You can either use get_posts or wp_get_recent_posts.
Or, if you really want to list pages, use wp_list_pages.
Besides that, you should look at http://codex.wordpress.org/Pages and http://codex.wordpress.org/Posts

Reduce number of posts depending on post content

In a custom post archive; Is it possible to reduce the the number of posts shown, depending on the contents of the showing posts?
I have a custom post type for products. In the product archive there are 20 products showing on each page, 4 rows, 5 cols.
In some cases where the product image is super wide I'd like that product to be shown with double width.
When showing a super wide product, I'd like to reduce the number of posts to 19 (18 if there are two super wide products). Keeping the bottom line straight.
This is the loop for my custom post archive.
I got your point with numcols. And as you say, I should probably modify the query, making sure that page 2 and 3 (and on) shows the right posts.
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<a href="<?php the_permalink(); ?>" class="produkt_item normal various" data-fancybox-type="iframe">
<?php if ( has_post_thumbnail() ) { ?>
<div class="utvaldbild">
<?php the_post_thumbnail('produktbild'); ?>
</div>
<?php } ?>
<h2><?php the_title();?></h2>
<p><?php the_field('produktnotis'); ?></p>
<?php if (get_field('nedladdning')) { ?><div class="nedladdning"></div><?php } ?>
</a>
<?php endwhile; ?>
you could add a custom field to your post custom posttype called numcols or something like that. normal products would be 1 and super images 2 etc.
then just keep a count of them in your php code and cut off when appropriate.
you could probably modify the query in the lookup page to do this but I'd have to see some code to be of any help. ideally your custom product archive page and the definition of your custom post type.

How to retrieve all custom posts from all the categories in portfolio

I have created custom post type portfolio in wordpress. I have created categories like web-design, logo design, e-commerce, photography... etc. I have retrieved all these category as the navigation menu in portfolio template file (assigned to the portfolio page). Now I want there should be a link(view all) in the navigation menu which will retrieve all the posts from all the categories in portfolio. Basically I want a way for making default category which will retrieve all the posts. Can anybody please help me!!!
So, basically, you want to create a Custom Post Type Archive page.
A way to achieve this, as explained at WPBeginner.com, is like the following:
Create a custom page template (the PHP file could have any name you want) and Add the Template Name comment at the beginning of the file:
<?php /* Template Name: Custom Post Type Archive */ ?>
Create a custom loop:
<?php
/* Template Name: Custom Post Type Archive */
get_header();
?>
<?php
global $query_string;
query_posts($query_string . "post_type={YOUR-CUSTOM-POST-TYPE}&post_status=publish&posts_per_page=10");
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
?>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<?php
endwhile;
endif;
?>
<div class="navigation">
<div class="alignleft"><?php next_posts_link('Previous entries') ?></div>
<div class="alignright"><?php previous_posts_link('Next entries') ?></div>
</div>
<?php wp_reset_query(); ?>
<?php get_sidebar(); ?>
<?php get_footer();?>
Create a new page and select the template you just created in the Page Attributes box
You should be able to use the archive page template to list them all then. Try going to http://yourdomain.com/portfolio and see if that brings it up. You will need to have enabled archives for the post type when you created it though. Look for 'has_archive' => true, if you created the post type manually by dropping in the code in your functions.php file. If you see that, but its set to "false", you'll need to change it to true.
You could also create a custom archive template for this post type if you wanted to. If you create a new file in your theme directory called archive-portfolio.php and then put a custom loop in there the post type will automatically target that template file when accessing the post type archive.
Then to link from your nav menu to this archive, just create a custom menu item in your menu editor with the url http://yourdomain.com/portfolio. You won't need to create any sort of "default" category if you take advantage of the post type archives.
Let me know if you have any other questions.

WORDPRESS: How to remove certain content on single post page of post_format gallery?

First off I created my own theme from scratch. I've been trying to figure this out all day yesterday. Ended up using a couple of twentyelevens and twentyten's files just to accomplish this. Then deleted them cause there was no success. All I want to do is remove certain text such as "Filed Under" and "Posted By" that appears at the bottom of my single posts page. I want the Standard Single Posts Page to have the Meta Data, while the Gallery Single Posts Page doesn't have Meta Data.
I tried to use the loop.php, loop-single.php, loop-gallery.php, content.php method but nothing was working for me. Where can I start to get these two different post formats to display differently on their single pages?
Is there anything I need to add to my functions.php file just to make this work?
Do I need to recreate the loop files?
Please help...
If 'gallery' is a category, you could edit your single.php template and use is_category():
<?php if ( in_category('gallery') ) : ?>
<!-- Single post style for gallery posts -->
<?php else: ?>
<!-- Normal single post style -->
<?php endif; ?>
If it's a custom post type, you could use get_post_type() in single.php and use its result in a condition, e.g.
<?php
$post_type = get_post_type( $post->ID );
if ( $post_type == 'gallery' ): ?>
<!-- Single post style for gallery posts -->
<?php else: ?>
<!-- Normal single post style -->
<?php endif; ?>
If it's a post format, use get_post_format(), e.g.
<?php
$post_format = get_post_format( $post->ID );
if ( $post_format == 'gallery' ): ?>
<!-- Single post style for gallery posts -->
<?php else: ?>
<!-- Normal single post style -->
<?php endif; ?>

Drupal 6 CCK Field Not Showing

In Drupal 6, I added a custom field to a custom content type.
The name is: "field_publishedin"
I then added data into that field for some sample articles.
In the view for it, I added that field as well.
On the page that renders it I added the code to show the field as well:
<div class="press-content">
<div class="family-news">
<?php foreach($rows as $row): ?>
<div class="news">
<div class="data">Posted on <?php print $row['created'] ?> </div><!--DATA-->
<h4><?php print $row['title'] ?><span><?php print $row['field_publishedin_value']; ?></span></h4>
</div><!--NEWS-->
<?php endforeach ?>
</div>
</div>
So the code I added is <span><?php print $row['field_publishedin_value']; ?></span> since according to the view when using in a template you are suppose to add "_value".
However, I clicked rescan templates, emptied the drupal cache but that new code still does not render on the page.
When I add this to the page: <?php print '<pre>' . htmlentities(print_r($rows, 1)) . '</pre>'; ?> and it outputs the possible array values it does not show, "field_publishedin_value" so it seems like the template doesnt know that field exists even though its in the view. Help?
install the devel module, and in to *.tpl.php, place:
<?php
dpm($fields); // if views
// or dpm($node); if it's a node or page.tpl.php
// of if you don't know, dpm(get_defined_vars());
?>
Also, if this is views, the template you should be using is the views-view-fields.tpl.php. Note the PLURAL fields.
Joe

Resources