How to display advanced custom fields with an if statement - php

I'd like to display advanced custom fields on my archive page only but the following code keeps giving me an error:
<?php
do_action( 'editorial_post_categories' );
if ( is_single() ) {
// do something or nothing
} else {
<?php the_field('acf_123'); ?>
}
?>
The advanced custom field I like to display is "acf_123"

Looks like you're nesting <?php tags. You've opened one at the top, so you don't need to open them again. Try:
<?php
do_action( 'editorial_post_categories' );
if ( is_single() ) {
// do something or nothing
} else {
the_field('acf_123');
}
?>
That is, remove the <?php and ?> around the_field() call.

Related

How to make so slider show up just in home page and anywhere else

PICTUREI have short code <?php echo do_shortcode('[wonderplugin_slider id=1]'); ?> and have to put it above the page see in the picture
The issue is the slider (shortcode) should display JUST on Homepage or Main page.
I have tried many plugins and codes like this <?php if(is_page(<111>)) { ?><?php }>but it does not work
You can use is_front_page(), is_home() and is_page() functions.
In your code, you want to mention the page id directly means please put it without any quotes like below.
<?php
if(is_page(111)){
echo do_shortcode('[wonderplugin_slider id=1]');
}
?>
Or else you already set the page with id 111 as the home page or front page means, you can use is_home() and is_front_page() function.
if( is_front_page() ) {
echo do_shortcode('[wonderplugin_slider id=1]');
}
or
if( is_front_page() && is_home() ) {
echo do_shortcode('[wonderplugin_slider id=1]');
}
Thanks.
You shpuld check both is_home() and is_front_page(), but their usage depends on the frontpage you have. If you have a statick frontpage you can have:
if( is_front_page() ) {
...
}
In the other way around you can conbine both like:
if( is_front_page() && is_home() ) {
...
}

Show header only on shoppage Wordpress

Took me some time to find out a working code to show the header above the sidebar.
But I now have the problem that I just want to show the header on the shop-page and not on the product pages.
So I'm trying to figure out what do I need to change?
add_action('woocommerce_before_main_content','before_main_content');
function before_main_content() { ?
<img src="image url">
<?php
}
?
Thank you guys for all the answers :)
This one worked for me :
function before_main_content() {
if (is_shop()) {
?>
<img src="image url">
<?php
}
}
?>
Replace *** with your shop page name:
<?php
if ( is_page('***')) {
**Insert your header code here**;
}
?>
This works for me :)
Use the page conditional, is_page(), to determine if you should show the image. WooCommerce has a function to get the shop page ID. Test for it and return if it's not a match.
Example:
function wpse_before_main_content() { // before_main_content could be problematic, choose a prefix.
// Check if we're on the shop page.
if ( ! is_page( woocommerce_get_page_id( 'shop' ) ) ) {
return false;
} ?>
<img src=". . .
<?php
}
add_action( 'woocommerce_before_main_content', 'wpse_before_main_content' );

Link title to external link instead of permalink

I'm trying to get the same effect as the plugin Pages Link To where the title of the post is linked to an external link. The reason I don't want to use the plugin is the link is generated dynamically when the post is saved, but I'm unable to update the the permalink of the post with the external link.
Below is my code in functions.php:
function savepost( $post_id ) {
if( $_POST['post_type'] == 'books' ){
$genre = strip_tags(get_field('genre'));
$author = strip_tags(get_field('author'));
$extlink = "http://www.".$genre."/".$author.".com";
update_post_meta( $post_id, 'extlink', $extlink);
$url = strip_tags(get_field('extlink',$post));
update_post_meta( $post_id, 'post_link', $url);
}
}
add_action( 'save_post', 'savepost' );
i m trying another method in which i assigned a template to the post so that when the post loads it redirects to the link but it doesn't redirect
my code
<?php
ob_start();
?>
<?php
/**
* Template Name: post Redirection Template
*/
get_header();
$redirecturl = strip_tags(get_field('extlink',$post));
wp_redirect($redirect_url);
get_sidebar();
get_footer();
?>
<?php
ob_end_flush();
?>
What you should do, is insert the external link as a custom field in the post editor, then display the custom field value in place of the_permalink(). You could use a plugin such as Advanced Custom Fields to grab the URL from the custom field.
EDIT 1: More clarification using the Advanced Custom Fields plugin as an example. The field name for this example is url.
You should use this wherever you want the custom permalink to appear throughout your site, such as in your archive.php, category.php, etc. Replace the code that looks something like this:
<?php the_title(); ?>
with this:
<?php
$value = get_field( "url" );
if( $value ) { ?>
<?php the_title(); ?>
<?php } else { ?>
<?php the_title(); ?>
<?php } ?>
EDIT 2: Clarifying additional information.
You can add a function to the header.php of your theme that checks if the url is set, then redirects to the external link that way, if your user goes directly to the permalink, it will still redirect them. In fact, you could use this code without using the above code to display the external link.
<?php
$value = get_field( "url" );
if( $value ) {
header('Location: '.$value);
die();
} else {} ?>
Warning: make sure to use this code before any HTML (or text) has been passed to the browser, or it will not work correctly.

How to add values to a Wordpress template file programmatically?

I'm new to Wordpress & PHP, so kindly excuse the naivety of the question, if any.
I'm building a plugin where I need to select values from the database and create a new HTML page with the values. I'm using a custom template file.
What I've done till now
Extracted the values from database
Load & display my own template in my plugin file
add_action( 'init', 'leb_add_endpoint' );
function leb_add_endpoint()
{
add_rewrite_endpoint( 'result', EP_PERMALINK );
}
add_action( 'template_include', 'leb_render_template' );
function leb_render_template($default_template) {
//some code removed for brevity
if ( ! is_singular() || !isset($wp_query->query_vars['result']) )
{
return $default_template;
}
$sample_result = $wpdb->get_var($wpdb->prepare($sql));
$default_template = plugin_dir_path(__FILE__) . '/my-custom-template.php';
return $default_template;
}
The content of my-custom-template.php is as follows
<?php
/* Template Name: My Template*/
echo '<h1>Testing</h1>';
?>
The page gets displayed without any problem. All I want is to insert $sample_result and other similar results pulled form database into my-custom-template.php
I need to generate dynamic pages based on values pulled from DB. So each time, the final page created might be different. E.g. if one hits www.example.com/sample-post/result, a page will be shown with values pulled from the DB. If one hits www.example.com/another-sample-post/result, a different page will be shown with different values. Both these pages will have the same design, only a few values will be different. This is what I'm trying to achieve.
How can I do that? Please help me. I'm stuck. :(
You need to define "result" in query vars.
Use EP_ROOT Endpoint Mask ( result/{var} is located in the root )
Inside template_include hook, you can find result value inside $wp_query object.
I've already tested the code
// Add a new var to query vars
function result_add_query_vars( $vars ){
$vars[] = 'result';
return $vars;
}
add_filter( 'query_vars', 'result_add_query_vars' );
// Add endpoint
function result_add_endpoint() {
add_rewrite_endpoint( 'result', EP_ROOT );
}
add_action( 'init', 'result_add_endpoint');
// change the template
function result_render_template($template)
{
global $wp_query;
if ( array_key_exists( 'result', $wp_query->query_vars ) ) {
$result = get_query_var('result');
$new_template = plugin_dir_path(__FILE__) . '/my-custom-template.php';
return $new_template;
} else {
return $template;
}
}
add_action( 'template_include', 'result_render_template' );
Now you can retrieve the query var in your custom template
/*
* Template Name: My Custom Template
*/
$result = get_query_var('result');
echo $result;
Well why don't you use $wp_query Inside your my-custom-template.php
<?php
/* Template Name: My Template*/
global $wp_query;
echo '<pre>';
print_r($wp_query); // Use this in case you want to see what else do you have with you.
echo '<pre/>';
// Now you can use $wp_query to build your dynamic query at run time.
// This will allow you to perform task at run time
?>
To Retrieve Meta
If you have saved something as a meta then
<?php
$meta_values = get_post_meta( $post_id, $key, $single );
?>
To Retrieve Child Post
If you want to retrieve child posts then
<?php
if ( have_posts() ) :
// Start the Loop.
while ( have_posts() ) : the_post();
// Do your stuff here such as below
the_title();
the_content();
endwhile;
else:
echo 'No Post Found';
endif;
?>
1) Write this function in function.php in your active template.
function leb_render_template() {
//some code removed for brevity
//$sql = 'YOUR_QUERY_CODE'
$sample_result = $wpdb->get_var($wpdb->prepare($sql));
return $my_template;
}
add_action('wp_ajax_leb_render_template', 'leb_render_template');
add_action('wp_ajax_nopriv_leb_render_template', 'leb_render_template');
2) Call function in your custom template.
<?php
/* Template Name: My Template*/
echo '<h1>Testing</h1>';
$result = leb_render_template();
print_r($result); // Print Your function output.
?>

Trying to add two different strings in PHP

I came up with an solution for not showing an sidebar in Wordpress when this is not filled with widgets. I did this by calling for the slug from the current page and named my sidebars the same as the slugs from my pages.
I use the Custom Sidebars plugin to add sidebars. This plugin adds 'cs-' in the id of a sidebar before the name of the sidebar. So for example I've named my sidebar 'Test' than the id of the sidebar will be 'cs-test'.
Now I want my php code to check if the sidebar is named the same as the slug from the page. So I called the_slug and before the slug I want to add cs-. With some help I came up with this:
<?php if ( is_active_sidebar( 'cs-'.the_slug(false) ) ) { ?>
<?php dynamic_sidebar( is_active_sidebar( 'cs-'.the_slug(false) ) ) ?>
<?php } else { ?>
<?php } ?>
But that doesn't seems to help. Any thoughts how to fix this? Thanks already!
Your are concatenating a string cs- with a function the_slug(false). This might cause the error, depending on what the function is returning.
Solution 1:
You could call and save the function beforehand and concate two vars:
<?php
$slug = the_slug(false); //Get return value
$slug = (string)$slug; //Force string conversion
$slug = 'cs-'.$slug; //Concate
if ( is_active_sidebar( $slug ) ) { ?>
<?php dynamic_sidebar( is_active_sidebar( $slug ) ) ?>
<?php } else { ?>
<?php } ?>
.
Solution 2:
If you need this type of functionality often try to add a string parameter to the the_slug() function (should be a custom function in your functions.php) and do the concatenating within the function itself:
function the_slug($boolen, $string='') {
//Your code
return $string.$your_var;
}
Your sidebar code would look like this:
<?php if ( is_active_sidebar( the_slug(false, 'cs-') ) ) { ?>
<?php dynamic_sidebar( is_active_sidebar( the_slug(false, 'cs-') ) ) ?>
<?php } else { ?>
<?php } ?>

Categories