Copy advanced custom field content from other page using if statement - php

Im trying to copy some content from another page with an if statement that has an input from advanced custom fields so I dont have to manage two pages with the same content. However the code has a bug in it which prevents the page from loading when this section starts. I guess I made a type somewhere. What could it be?
The first block contains the information from post id 4767 that i want to display on post id 4940. The second block is the information that would normally be loaded and is entered on all the individual pages.
<?php if(is_single('4940')) {
if( have_rows('partner', 4767) ):
while ( have_rows('partner', 4767) ) : the_row();
$partnerTag = get_sub_field('partner_tag', 4767);
$partnerName = get_sub_field('partner_name', 4767);
$partnerImage = get_sub_field('partner_image', 4767);
$partnerImageUrl = $partnerImage['sizes']['medium'];
$partnerLink = get_sub_field('link', 4767);
} else {
if( have_rows('partner') ):
while ( have_rows('partner') ) : the_row();
$partnerTag = get_sub_field('partner_tag');
$partnerName = get_sub_field('partner_name');
$partnerImage = get_sub_field('partner_image');
$partnerImageUrl = $partnerImage['sizes']['medium'];
$partnerLink = get_sub_field('link');
} ?>
ps. I tried both: is_single('4940') and is_single(4940)

You could just check the value of $post->ID directly.
if( $post->ID == 4940 ) { ... }
Are you within the query? is_single() and is_singular() will not work outside loop.

Related

How to run Wordpress Query by ACF field using Select on front-end

Basically what I'm trying to do is create filters on front end, so that users can filter all posts by specific ACF field.
There's a cattery website and the owner will need to add new litters, when new generations of kittens arrive. I've created option page for this new field group and created a repeater field with text, so that I can add new rows with names of the litters, I want to filter by later.
I've included basic loop for repeater field:
<select name="litter" id="litter">
<?php
if( have_rows('cat-litters', 'option') ):
while ( have_rows('cat-litters', 'option') ) : the_row(); ?>
<option value="<?php the_sub_field('new-litter', 'option'); ?>"><?php the_sub_field('new-litter', 'option'); ?></option>
<?php endwhile;
else :
endif;
?>
</select>
And it is working for now:
I will be adding a field to cats' posts named cat-litter, so that I can find specific posts with a litter "02" for example.
But now I'm stuck with using this select from front-end to run a query to display fitting posts.
I'm working in index.php of twentytwentyone child-theme btw.
It doesn't matter if it will need to reload page or not (however if the ajax way won't be complicated I would very much appreciate that) but I have to point out that it needs to work with pagination and I already have it set up with pre_get_posts to display 9 posts per page (3x3 grid).
Taken and modified from the Dynamic $_GET parameters section of this page per the ACF documentation: https://www.advancedcustomfields.com/resources/query-posts-custom-fields/
function my_pre_get_posts( $query ) {
// do not modify queries in the admin
if( is_admin() ) {
return $query;
}
// only modify queries for 'cat' post type
if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'cat' ) {
// allow the url to alter the query
if( isset($_GET['cat-litter']) ) {
$query->set('meta_key', 'cat-litter');
$query->set('meta_value', $_GET['cat-litter']);
}
}
// return
return $query;
}
add_action('pre_get_posts', 'my_pre_get_posts');
Then on the front end for reloading the page with the 'cat-litter' from the dropdown could be done with jQuery:
<script>
$('select#litter').on('change', function() {
window.location.href = "www.website.com/cats?cat-litter=" + $(this).val();
});
</script>

Need to reload page for show new value of meta_key in Wordpress

I tried to update meta key with custom form in wordpress
CMS: Wordpress
Plugin: advanced custom fields
Field Type: Repeater field
this is a code in single.php
if(isset($_POST['submit'])){
if( have_rows('custom_req') ){
while( have_rows('custom_req') ){ the_row();
$r_req = get_sub_field('req_fie');
if ($_POST['radio-b'] == $r_req ) {
update_sub_field('rq_answer', 'new_value');
}
}
}
}
if( have_rows('custom_req') ){
while( have_rows('custom_req') ){ the_row();
the_sub_field('rq_answer'); // This line shows the previous value
}
}
the problem is shows the previous value, and the page must be reloaded to show the new value
Why is that? How do I solve this problem?
Thanks for any help

Loading a post type in a page under a tab

I have a page with three tabs.
I am using nav-tab-wrapper class and php to to check which tab is active.
My third tab is a custom post types for logging.
I am trying to load the custom post type under the tab and not redirect to another page. I have not figured out how to do this yet.
I tried using a href="page" but it redirects to the post types page.
I tried to use href= page&tab, then include the page on that tab. This causes an error.
<?php $active_tab = isset($_GET['tab']) ? $_GET['tab'] : 'entries'; // end if ?>
<h2 class="nav-tab-wrapper">
Entries
Maps
Logs
</h2>
<form method="post" action="options.php">
<?php
if ($active_tab == 'entries') {
include 'entries.php';
} else if ($active_tab == 'maps') {
include 'maps.php';
} else if ($active_tab == 'logs'){
// THIS IS WHERE I NEED TO LOAD THE POST TYPE TABLE. I WANT TO LOAD THE PAGE UNDER THE TAB.
}
Expected: logs (post_type) table loads under logs tab
Actual Results: I can redirect to the page but I do not know how to load under the actual tab.
The thing you have to do is doing a custom query for that post type to fetch the posts of that type inside that condition ( $active_tab == 'logs' ). You can do it by WP_Query . To know more about WP_Query, please check this link: https://www.billerickson.net/code/wp_query-arguments/
Here you will find the info regarding how to use this function with all the possible parameters.
Here in your code, this would be something like the code below.
else if ($active_tab == 'logs'){
// THIS IS WHERE I NEED TO LOAD THE POST TYPE TABLE. I WANT TO LOAD THE PAGE UNDER THE TAB.
$arg = array(
'post_type' => {POST TYPE NAME},
);
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
// Do Stuff, here you print anything regarding your post, i have printed the title for the current post as an example
the_title()
} // end while
} // endif
// Reset Post Data
wp_reset_postdata();
}

Call different sidebar in one page template with if statement wordpress

I have 1 single post page template which will display 2 different taxonomy terms, and I like to call 2 different sidebar for these 2 different terms. This post template is "single-recipe.php", in which I call "content-single_recipe.php". In "content-single_recipe.php", I call for 2 different sidebars base on the different terms with a condition statement:
SINGLE-RECIPE.PHP
while ( have_posts() ) : the_post();
get_template_part( 'content', 'single_recipe' );
endwhile; // end of the loop.
CONTENT-SINGLE_RECIPE.PHP
php the_content();
// Here are code for sidebars:
$term = get_the_term_list($post->ID, 'recipe_type');
if ( $term = 'salad' ){
dynamic_sidebar('sidebar-salad');
}elseif($term = 'sandwich'){
dynamic_sidebar('sidebar-sandwich' );
}
However, no matter what the $term is, it always call the "sidebar-salad".
get_the_term_list gives you HTML list of terms. Use has_term instead. And comparing is done with ==, not =. Since you use =, which is assigning value to variable, you first if will always be true.
if( has_term( 'salad', 'recipe_type', $post->ID ) ){
dynamic_sidebar('sidebar-salad');
}
elseif( has_term( 'sandwich', 'recipe_type', $post->ID ) ){
dynamic_sidebar('sidebar-sandwich');
}
// Sidebars
if(is_front_page())
$op_sidebar_id = __('Sidebar Home','options');
// Single page and post type sidebars
elseif(is_attachment())
$op_sidebar_id = __('Sidebar Attachment','options');
elseif(is_single())
$op_sidebar_id = __('Sidebar Single','options');
elseif(is_page())
$op_sidebar_id = __('Sidebar Page','options');
else
$op_sidebar_id = __('Sidebar Home','options');
After that, I add the normal widgetized section with the variable set:
// If using the No Sidebar template
if(is_page_template('no-sidebar.php')) :
echo '';
// Else, show the sidebar
else :
echo "";
if(function_exists('dynamic_sidebar') &&
dynamic_sidebar($op_sidebar_id)) :
else :
// Default to Home page sidebar if no widgets are set
if(function_exists('dynamic_sidebar') && dynamic_sidebar(__('Sidebar
Home','options'))) :
else : _e('Add content to your sidebar through the widget control
panel.','options');
endif;
endif;
echo '</div>';
endif;

Show content if child of a specific page

I am building a website in WordPress and have a set of photos that display along the top of the page (in the background) and have them to where the set of photos can change depending on where the visitor is on the website. For example, if they are looking at the FAQ page, then photos pertaining to asking questions are shown. If they are looking at the news page, then photos pertaining to news are shown. The code I have works great, however, I'd like to now show photos based on the parent of the page they are on; for instance, if they are looking at a child page under a parent.
Here's my code as it stands now:
<?php if ( is_front_page() ) { include'media/home.php'; }
elseif ( is_404() ) { include'media/404s.php'; }
elseif ( is_page('news') ) { include'media/home.php'; }
elseif ( is_page('faqs') ) { include'media/faqs.php'; }
elseif ( is_category('africa-2') ) { include'media/faqs.php'; }
else { include'media/general.php'; }
?>
How can I tell WordPress, I want it to show a certain php file if it is a parent page and all of it's children?
If you're in the loop you can just use
if( is_page('faqs') && $post->post_parent ){
//include template here
}
If you're outside the loop you'll need to include global $post before the conditional.
global $post
You can reference this link http://codex.wordpress.org/Conditional_Tags#Testing_for_sub-Pages

Categories