I am using a ACF select field to enable the page admin to select a category of posts to display under the page content. For example, if he selects "Televisions" in the ACF select field, all posts in that category will display after the page content. Here is the code for that bottom part of the page (after the main content), from the page template:
<h2>Learn more about <?php the_field('main_page'); ?></h2>
<ul>
<?php
$main_page=get_field('main_page');
$related_systems = new WP_Query( 'category_name='.$main_page );
while( $related_systems->have_posts() ) : $related_systems->the_post();
if($post->post_type == 'post'):
?>
<li><?php the_title();?></li>
<?php
endif;
endwhile; ?>
</ul>
Here is the ACF settings screenshot
The select field shows up fine on the admin side in all four pages that have the Main four page template, but both get_field('main_page') or the_field('main_page') end up blank (I tested get_field with echo and nothing shows up). How To get the field value in the page template?
I'm using WordPress 3.8.1 and ACF Version 4.3.5
Inside the main content, add $page_id = get_the_ID();.
And in the custom loop, call the fields:
the_field( 'main_page', $page_id );
get_field( 'main_page', $page_id );
Those functions work without an ID if used inside the loop, otherwise we need to specify what post we are requesting.
Also, you can filter the post type when calling WP_Query:
WP_Query ( 'post_type=post&category_name=' . $main_page );
Related
I'm using Wordpress latest version, a child theme and Visual Composer WPBakery Page Builder.
I'm getting my blog posts listed into a template php file using WP_Query and in that page I need to:
display the Featured Image (which has caption) - DONE,
the Post Date - DONE,
and just a text block from the Post Content - a specific Row with a Column with a Text Block inside. This Text Block has a class name: bblog-text-row.
I need to echo/display/show just that text, but when I do the_content (); it outputs the VC shortcodes in a row as a raw text with shortcodes and the text. I also need to limit the text for this to 250 chars.
Here is the code I have just for the content part:
<?php
$query = new WP_Query( array( 'post_type' => 'post' ) );
$posts = $query->posts;
foreach($posts as $post) {
?>
<p>
<?php
$char_limit = 250; //character limit
$content = $post->post_content; //contents saved in a variable
echo substr(strip_tags($content), 0, $char_limit);
?>
</p>
<?php
}
?>
Thank you in advanced.
H
I found a small solution to my issue with getting the post content made with WPBakery Page Builder into a php template file listing all posts - like a blog page. Probably there might be clever and better ways to do it, but this solved my problem.
<?php
$the_query = new WP_Query( array( 'post_type' => 'post' ) );
while ($the_query->have_posts() ) {
$the_query->the_post(); ?>
<!-- you may add more post content here
I just want to refer to the content, not meta content or custom fields -->
<p class="bowe-post-content-bloc">
<?php
$conteudo = get_the_content(); //get ALL content for the post
$padrao = array('(\[vc(.?)\])', '(\[/vc(.?)\])','(\[booom_baannng_anypostsharebuttons])'); //create a filter
$reparacao = preg_replace ($padrao, "",$conteudo ); //remove content based on filter
echo wp_trim_words ( $reparacao, 40, '...'); //display only 40 words
?>
</p>
<!-- you may add more post content here
I just want to refer to the content, not meta content or custom fields -->
<?php
}
wp_reset_postdata(); ?>
In a wordpress theme I am programming, I created a custom post type and its template. This custom post type displays a list thanks to advance custom fields that I attached to the post template. In fact, I used the "repeater" field of Advance custom fields pro. The user only has to insert items of the list when editing the post.
I am trying to do the following: I want to display in two specific pages templates the post with all its custom fields (the list created through the repeater). I am not able to do that, I only can retrieve the "normal" fields of the post (the title, the content...). I created a snippet so you can look at my code.
<?php //the single of the post: ?>
<ul class='slider-partners'>
<?php
//slider partners
if( have_rows('slider_partenaires_hp') ): //"slider_partenaires_hp" is the repeater field
// loop through the rows of data
while ( have_rows('slider_partenaires_hp') ) : the_row();
// display a sub field value
echo "<li class='partenaire-slide'><img src='" . get_sub_field('logo_partner') . "'></li>"; //"logo_partner" is the item inside the repeater field
endwhile;
else :
// no rows found
endif;
?>
</ul>
<?php //the template of the page where I try to retrieve the above post:
$the_query = new WP_Query(array(
'post_type' => 'our-partners',
'posts_per_page' => 1,
'order' => 'DESC'
));
while ( $the_query->have_posts() ) :
$the_query->the_post();
endwhile;
wp_reset_postdata();
?>
Could you please help me in calling a post with all its custom fields in two diferent pages ?
Thanks
I'm using the Custom Field Plugin to add a custom Field and call it to the content using the_field(); in my single.php
I just want to display this field on the all the categories except the "Articles which is 13", so I was trying somethin' like this:
<?php if !is_category( '13' ); { ?>
<h3 class="single-product__price"><?php the_field('precio'); ?></h3>
<?php } ?>
But it is not working. I'm wondering how to do this.
You should use in_category, and put the conditional statement in parentheses.
<?php if (!in_category( $cat )) { ?>
<h3 class="single-product__price"><?php the_field('precio'); ?></h3>
<?php } ?>
Make sure $cat is either ID (integer), name or slug (string) of the category.
Edit: I'm assuming you actually want to display the input on posts that are not in a particular category, hence in_category. If you do indeed want to display it on all other Category archive pages, then is_category would be correct.
I am developing locally on MAMP using "Custom Post Types" and "Custom Fields" plugins. I have a custom post type of "Products" and a custom taxonomy of "Types."
I'm trying to use my pagination in "Taxonomy.php" because that's where I'm going to display my custom post types of "products" with my custom taxonomy "types" and use conditionals to generate different content on that same taxonomy.php.
This is the code in taxonomy.php that I got from css-tricks:
<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('showposts=3&post_type=products'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
// my content from my custom field that I want paginated
<p><?php the_field('image'); ?></p>
<?php endwhile; ?>
<nav>
<?php previous_posts_link('« Newer') ?>
<?php next_posts_link('Older »') ?>
</nav>
<?php
$wp_query = null;
$wp_query = $temp; // Reset
?>
This question has been asked a lot around the web and I have read at least 30 different articles and blogs about this and still can't seem to find a solution. With this method of pagination I got from css-tricks the only problem is I keep getting a 404 error message whenever I try to go to the next page unless I set my "Blog pages show at most" to 1 in settings > reading. If I do that then it works, otherwise it gives me the 404 page. My permalinks are set to Post Name, by the way.
So I can see my pagination here:
http://localhost/MyWebsite/types/cars/
but when I get here I get the 404 page.
http://localhost/MyWebsite/types/cars/page/2/
I have read about the Flush Method but that does not work. I only have 2 plugins activated (custom post types and fields) and those are not the problem. How can I fix the 404 error?
I have diagnosed the problem further and I figured out whats going on. This is now my code in terminology.php:
<?php
$args = array(
'post_type' => array( 'products' ),
'posts_per_page' => 5,
'paged' => ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1
);
$products_query = new WP_Query( $args );
$temp = $wp_query;
$wp_query = null;
$wp_query = $products_query;
while ($products_query->have_posts()) : $products_query->the_post();
?>
// content to display
<p><?php the_field('image'); ?></p>
<?php endwhile; ?>
<nav>
<?php previous_posts_link('« Newer') ?>
<?php next_posts_link('Older »') ?>
</nav>
<?php
$wp_query = null;
$wp_query = $temp; // Reset
?>
Bear with me because this is a little bit confusing. I'll try my best to clearly explain the difference between "Blog pages show at most" in settings > reading and my posts_per_page arguments.
I have a category of Honda with a sub-term of Accord and I have 3 products of my custom post type assigned to Accord
Now if I changed my posts_per_page argument what that does is no matter how many "Products" I have under "Accord" it will always demonstrate that amount even if you do not have enough.
So if I set it to 5 and I have 3 products it will still show 5 either repeated or from another category. When I click on the next pagination the pages will only go as far as however many items that I have. That is if "Blog pages show at most" is set to 1, which brings me to my next point.
Now the difference in changing "Blog pages show at most" is that even if I have my posts per pages set to 5 and I have only 3 "Products" but I change my "Blog pages show at most" to 3 it will not go to the next page because I only have 3 "products" in accord term.
If I were to change my "Blog pages show at most" to 1, I would be able to go forward 3 pages because I have 3 items.
It's sort of like changing "Blog pages show at most" changes the core of WordPress so no matter how many you put to display in the posts_per_page args it will only really count however many you set in "Blog pages show at most".
It seems like the pagination count is based upon "Blog pages show at most", not posts_per_page. I can have posts per page set to 10 and "Blog pages show at most" set to 2 and it would show 10 but only count as if there were really 2 on each page meaning I would only be able to go to the 2nd page of my "Accord" term that has 10 displaying in both of them.
Get the pattern? WordPress counted 2 products per page even though it was displaying 10. So it told the pagination that it can't go past page 2 because I only have 3 products.
But if you have a custom post type products and a taxonomy types you will show your content on: archive-products.php and taxonomy-types.php you could use pre_get_posts action to modify the query if you need and then write the markup you want on those files, example:
Put this on your functions.php
<?php
add_action( 'pre_get_posts', 'mr_modify_archive_taxonomy_query' );
function mr_modify_archive_taxonomy_query( $query ) {
if ( !is_admin() && $query->is_main_query() ) {
if ( is_post_type_archive( 'products' ) || is_tax( 'types' ) ) {
$query->set( 'posts_per_page', 3 );
}
}
}
?>
an use a default loop
<?php
if ( have_posts() ):
while ( have_posts() ): the_post();
printf( '<p>%s</p>', get_the_field( 'image' ) );
endwhile;
previous_posts_link('« Newer');
next_posts_link('Older »');
endif;
?>
I have a custom page named 'Journal', which I use as a blog index page for my wordpress website. I've run into a rather strange problem. When I enter <?php echo get_the_title(); ?> or whatever in home.php, it returns the title of a post, instead of the page title 'Journal'. Is anyone familiar with this problem?
Thanks!
This is the expected behavior for this page. When you set a page to be your "blog", you can't access the template tags for that page. Instead, the template tags are for the loop of the posts to be displayed on that page.
To get the title, you have to first get the id of that page, and then you can pass it to a function:
<?php
$page_for_blog = get_option( 'page_for_posts' );
$page_title = get_the_title( $page_for_blog );
?>
Now you can print the $page_title and you should see "Journal".
Updated with Advanced Custom Fields
Now that you have the Journal page's id ($page_for_blog), you can get your field values with:
$field_value = get_field( 'field_name', $page_for_blog );
Obviously, replace 'field_name' with whatever field you're trying to retrieve.