How to position output of gravity forms dynamic output? - wordpress

I'm simply trying to use the value from a Gravity Forms dynamically populated URL parameter as the ID for getting a custom field from Advanced Custom Fields. Using it like this returns the result within the of the content.
What is the best way to hook this such that it appears below the form. I've tried inserting the filter into the page template itself to no avail.
<?php
add_filter('gform_field_value_subclass', 'my_custom_population_function');
function my_custom_population_function($value)
{
$repair = get_field('repair', $value);
$report = get_field('report', $value);
if ($repair && $report) : ?>
<section class="rrr">
<div class="row">
<div class="repair">
<div class="r-wrap">
<h2 class="bracket-heading"><span>Step 2:</span>Report</h2>
<?php echo $repair ?>
</div>
</div>
<div class="recognize">
<div class="r-wrap">
<h2 class="bracket-heading"><span>Step 1:</span>Immediate Action Steps</h2>
<?php echo $report; ?>
</div>
</div>
</div>
</section>
<?php endif;
} ?>

I actually realized that something didn't feel right after looking over the first 20 pages of Google and not seeing a reference to this use case. The filters in Gravity Forms are more suited towards dynamic population of other fields, not to set context for the site as a whole. At least that's what it appears.
I found the better solution to be using query_vars.
In functions.php, I made it so that the URL parameter called subclass can be queried from Wordpress itself.
<?php
// add `author_more` to query vars
add_filter( 'init', 'add_subclass_query_var' );
function add_subclass_query_var()
{
global $wp;
$wp->add_query_var( 'subclass' );
}
?>
And then in my page template file, I grab that value that I need to set the ID of the field from the URL using get_query_var().
The rest is pretty straightforward and just prints the field values out where I need them.
<?php
$value = get_query_var('subclass');
$repair = get_field('repair', $value);
$report = get_field('report', $value);
if ($repair && $report) : ?>
<section class="rrr">
<div class="row">
<div class="repair">
<div class="r-wrap">
<h2 class="bracket-heading"><span>Step 2:</span>Report</h2>
<?php echo $repair ?>
</div>
</div>
<div class="recognize">
<div class="r-wrap">
<h2 class="bracket-heading"><span>Step 1:</span>Immediate Action Steps</h2>
<?php echo $report; ?>
</div>
</div>
</div>
</section>
<?php else:
echo "No Results Found"
?>
<?php endif; ?>
https://codex.wordpress.org/WordPress_Query_Vars

Related

configure parent/child relationship between 2 Custom Post Type only in child

I would like to create a relationship between 2 Custom Post Type avoiding that the user has to go to both to create the link.
In absolute terms, I would like the user to go to only one Custom Post Type (writing) which remains fairly basic: A title field, a text field, an image field, a link field for documents or url, and finally a selector field to select its parent (created with ACF).
In the hierarchy of the site, these posts created are at the end of the race, therefore, are only post children. They will display with "single.php".
The site obviously contains different menus which also have different sub-menus, a front-page.php for the home page, for the main menus we have the archive pages and for the sub-menus of the single pages.
The other Custom Post Types are the site menus which each contain their respective submenus. The user doesn't have to go here, hence I would like to create a parent/child relationship only by going into the child Custom Post Type.
For example, in my home page (front-page.php), I have a "Company" part if I click on it, I arrive on "archive.creb.php" where there is "Office", Secretariat" and "Management", if I click on "Office" I arrive on "single.creb.php", in this part, there is "the team" and "the calendar", if I click on "the team", I arrive at "single.php".
The team page (just like the "calendar" page) is an article written by the user.
So I'm looking for a way so that, when the user creates an article, he can choose where it will be (with the ACF selector field), and therefore choose its parent AND in addition, that the link of the article appears in this parent.
I know how to do this kind of operation by configuring a relational field in the 2 Custom Post Types concerned with ACF, but not with just one.
I did some research, especially on the ACF doc and I found this:
https://www.advancedcustomfields.com/resources/relationship/
https://www.advancedcustomfields.com/resources/querying-relationship-fields/
So I tried to adapt the ACF example with my code, but it's not easy because it's not quite the same... By schematizing the ACF example to mine, it gives this:
In both cases there is indeed a selection field creation, except that in the ACF example its selector chooses the child and I choose the parent. So I tried to reverse, using the code from single.location.php in my single.creb.php.... But it doesn't work.. my single.creb.php page shows a link loop to himself
So... I hope I was clear in my question! Hoping for an answer
Here are some codes :
functions.php
function custom_post_type(){
$arg = [
'public'=> true,
'label'=> 'CREB',
'show_in_rest' => true,
'supports'=>array('title', 'thumbnail', 'excerpt'),
'has_archive'=>true,
'menu_icon'=> 'dashicons-groups'
];
register_post_type('creb', $arg);
$arg = [
'public'=> true,
'label'=> 'redaction',
'show_in_rest' => true,
'supports'=>array('title', 'thumbnail', 'excerpt'),
'has_archive'=>true,
'menu_icon'=> 'dashicons-book-alt'
];
register_post_type('redaction', $arg);
}
add_action('init', 'custom_post_type');
single-creb.php
<?php get_header(); ?>
<!-- single creb -->
<main id="" class="">
<div class="">
<h1 class="">single CREB</h1>
<?php
if(have_posts()){
while(have_posts()){
the_post();
the_title();
$redac = get_field('textesousmenu');
if($redac){
echo $redac;
};
};
};
?>
</div>
</main>
<?php get_footer(); ?>
single.php
<?php get_header(); ?>
<!-- single creb -->
<main id="" class="">
<div class="">
<h1 class="">single</h1>
<?php
if(have_posts()){
while(have_posts()){
the_post();
the_title();
$redac = get_field('texte');
if($redac){
echo $redac;
};
$img = get_field('img');
if($img){
echo wp_get_attachment_image($img, 'modale');
}
if (have_rows('liendocurl')){?>
<ul class="">
<?php while (have_rows('liendocurl')){
the_row();
$nom = get_sub_field('nomlien');
$docurl = get_sub_field('urllien');?>
<li>
<a href="<?php echo $docurl; ?>" target="_blank">
<?php echo $nom ?>
</a>
</li>
<?php } ?>
</ul>
<?php }
$lien = get_field('relation');
if($lien){?>
<ul>
<?php
$name = get_the_title($lien->ID);
$link = get_permalink($lien->ID);
?>
<li>
<a href="<?php echo $link;?>">
<?php echo $name; ?>
</a>
</li>
</ul><?php
}
};
};
?>
</div>
</main>
<?php get_footer(); ?>
thank you in advance

WooCommerce products shortcode outputs class="columns-x" twice

Just a vanilla call to product-category shortcode in functions.php is confusing me.
I'm trying to get a columns-3 of text sitting next to a columns-9 of products.
My code:
<div class="content">
<div class="columns-3">
<?php $home_kit = get_term(30, 'product_cat', ARRAY_A); ?>
<h2 class="section-title"><?php echo $home_kit['name']; ?></h2>
<p><?php echo $home_kit['description']; ?></p>
All Products ยป
</div>
<?php echo do_shortcode('[product_category category="home-kits" per_page="3" orderby="price" order="desc" columns="9"]'); ?>
</div>
The shortcode generates:
<div class="homepage-home-kit-category">
<div class="content">
<div class="columns-3">
<h2...</h2>
<p>...</p>
<a ...</a>
</div>
<div class="woocommerce columns-9 ">
<ul class="products columns-9">
<li></li>
</ul>
</div>
</div>
</div>
Note the repeat of the columns-9 on both the generated <div> and <ul>.
If both the <div> and <ul> have class columns-9 then I get 3/4 of the available 3/4.
Surely the class columns-9 only needs to be on either the WooComerce <div> or the <ul>.
How can I remove this addition from the <ul> element?
I am gratefull for the answers I received which are all valid and work well.
I suppose my underlying problem is that I cannot see the usefulness of the product_category shortcode as it does not obey the columns parameter faithfully.
Am I alone?
There are several options to remove/modify the columns-9 class from the <ul> element
Solution 1 - Through the use of a filter hook.
function filter_woocommerce_product_loop_start( $loop_start ) {
// New output
$loop_start = '<ul class="products">';
return $loop_start;
}
add_filter( 'woocommerce_product_loop_start', 'filter_woocommerce_product_loop_start', 10, 1 );
Solution 2 - Overwriting the template file.
You could overwrite the templates/loop/loop-start.php template file
This template can be overridden by copying it to yourtheme/woocommerce/loop/loop-start.php.
Replace
<ul class="products columns-<?php echo esc_attr( wc_get_loop_prop( 'columns' ) ); ?>">
With
<ul class="products">
Solution 3 - overwriting the existing function.
Since woocommerce_product_loop_start uses function_exits, see: includes/wc-template-functions.php on line 1110-1134 #version 2.5.0
More info: What's "function_exists" in Wordpress
/**
* Output the start of a product loop. By default this is a UL.
*
* #param bool $echo Should echo?.
* #return string
*/
function woocommerce_product_loop_start( $echo = true ) {
$loop_start = '<ul class="products">';
if ( $echo ) {
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $loop_start;
} else {
return $loop_start;
}
}

Using a Shortcode within a Shortcode

A client is using WPBakery and a form is being pulled into the description attribute of a shortcode. Ordinarily you would pull the form by its shortcode but the boiler plate demo has the form being pulled with a string beginning with a hashtag.
E-8_JTVCbWM0d3BfZm9ybSUyMGlkJTNEJTIyNDc0JTIyJTVE
[trx_sc_title title_style="accent" title_align="left" link_style="default" title="Stay Tuned for Our Updates" subtitle="newsletter signup" description="#E-8_JTVCbWM0d3BfZm9ybSUyMGlkJTNEJTIyNDc0JTIyJTVE"]
is equivalent to:
[trx_sc_title title_style="accent" title_align="left" link_style="default" title="Stay Tuned for Our Updates" subtitle="newsletter signup" description="[mc4wp_form id="474"]"]
I need to pull a different form from ninjaforms( [ninja_form id=3] ) instead of the mailchimp form in the example. How do I convert the shortcode in the same manner they did?
Thanks in advance for any insight as to what is going on here.
use two shortcodes. so that it won't be complicated.
function kp_shortcode1($atts, $content = null)
{
ob_start();
?>
<div class="sc1-section">
<?php echo do_shortcode($content); ?>
</div>
<?php
$output = ob_get_clean();
return $output;
}
add_shortcode('kp_shortcode1', 'kp_shortcode1');
function kp_shortcode2($atts, $content = null)
{
ob_start();
?>
<div class="sc2-section">
<h1>shortcode 2 content</h1>
</div>
<?php
$output = ob_get_clean();
return $output;
}
add_shortcode('kp_shortcode2', 'kp_shortcode2');
Inside the wordpress backend use
[kp_shortcode1]
[kp_shortcode2]
[kp_shortcode2]
[kp_shortcode2]
[/kp_shortcode1]
Output would be :
<div class="sc1-section">
<div class="sc2-section">
<h1>shortcode 2 content</h1>
</div>
<div class="sc2-section">
<h1>shortcode 2 content</h1>
</div>
<div class="sc2-section">
<h1>shortcode 2 content</h1>
</div>
</div>

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

Get a specific page's excerpt within the loop Wordpress

I am trying to create a loop that loads a random image from any posts, whilst also retrieving the excerpt of a specific page. I have done the random post part, but cannot get it to retrieve the page excerpt... I think I may need to query the pages in their own loop but I'm not sure how to do this. I have installed the function to get page excerpt support etc. but I think I am doing something wrong within the loop, any help would be appreciated.
<div class="postimage">
<?php if (have_posts()) :
query_posts('showposts=1&orderby=rand');
while (have_posts()) : the_post(); ?>
<?php the_post_thumbnail('blog-post-image'); ?>
<div class="borderimage"></div>
<div class="tagline"><h1><?php the_excerpt('$page_id=8'); ?> </h1>
</div>
</div>
</div>
<?php endwhile; else : endif; ?>
query_posts replaces the global $wp_query, which you don't want to do since you want to keep that query for your page. Try this instead...
if (have_posts()){
while(have_posts()){
the_post(); //global $post now has the page in it
$args = array("posts_per_page"=>1,"orderby"=>"rand");
$random_posts = get_posts($args); //returns an array
$random_post = $random_posts[0];
//do your stuff...
//$post contains the original page
//$random_post contains the random post
}
}

Resources