hide shortcode when one is empty - wordpress

I created a shortcode to be personalised directly in product page. I was wondering if there was a way to not show the shortcode if one of them is empty ? I saw it's not recommended to work directly and check if shortcode is empty but i don't find the solution anywhere.
What i need to check (if these shortcode is empty) : [tech_img1] [tech_img2] [tech_img3] [tech_text1][tech_text2] and [tech_img3]
For example : For each product, i can enter an url img saved in this shortcode [tech_img] and text saved in this shortcode [tech_text]. However if one of the shortcode is empty (no data in it), the function custom_tech shoudn't be display in front
I used this tutorial to add and save custom field in a product page : https://remicorson.com/mastering-woocommerce-products-custom-fields
I made a custom shortcode to fuse all the shortcode i need to display
function custom_tech() {
echo do_shortcode('<div class="flextech"><img class="picfit" src="[tech_img1]" alt="tech_image1"><p class="tech-block">[tech_text1]</p><img class="picfit" src="[tech_img2]" alt="tech_image1"><p class="tech-block">[tech_text2]</p><img class="picfit" src="[tech_img3]" alt="tech_image3"><p class="tech-block">[tech_text3]</p></div>'); } add_shortcode( 'my_custom_tech', 'custom_tech');

Related

Woocommerce product archive displays nothing when called from custom Page

I have a woocommerce shop page which shows me all of the products perfectly. This page is also set as the "Shop" page in the settings. Now I want a second page called Home, which should use the same template as the shop page. (basically a second shop page without categories and some news)
home-template.php
<?php /* Template Name: Homepage */
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
wc_get_template( 'archive-product.php' );
?>
I'm using the standard archive-product template from woocommerce's github.
I can see the template being loaded, like the header, but certain woocommerce functions don't return the expected value. woocommerce_before_shop_loop doesn't return anything, even though it should display the orderby dropdown. woocommerce_product_loop() returns true, but wc_get_loop_prop('total') returns zero.
I've also tried renaming the file to front-page.php but that didn't help. The home page is set correctly in the reading settings.
Am i missing a call to a query, or is it something else? Thank you in advance for your help!
When WooCommerce creates the builtin shop page it saves the id of the created page in the wp_options database table with option_name 'woocommerce_shop_page_id'. When WordPress loads a page WooCommerce checks if the page id of the page being loaded is equal to the option_value of option_name 'woocommerce_shop_page_id' and then executes code to generate the HTML of the shop page. If you have looked at the shop page in the page editor you will notice that the content is empty. The magic is being done with hard coded routines that execute for this special id.
Since, your custom page has a different page id none of the custom code for generating the shop page will be execute. So, you need to execute this magic code for your custom page. It can be done but you need a good understanding of WooCommerce.
I suggest you reconsider your design and instead of a new page just customize the existing shop page using actions and filters.

How to store and gett variables in a widget?

On the index page I load a custom made widget that I load with .
This custom widget is a copy of WP_Widget_Categories with
The only changes i made is that instead of a dropdown or a list I now have a form with a few radioboxes (with category names) and a submit button.
When I check for example "category-1" and press the submit button I go to the "mysiteurl/category/category-1" page and I see all the posts of the category 1 page.
In the category.php page (just as in my index.php page). I have the same custom radiobutton widget implemented with:
<?php dynamic_sidebar( 'custom-category-checkbox-menu' ); ?>
The problem is when i get to the category page. The radiobox saying "category 1" should be checked because you are on that page. I need to have some sort of variable so i can know which category i am on now.
My first tries were:
Make a hidden field in my form() function of my widget and pass variables there. But this seems messy.
I already gave my radiobuttons name="cat" and value="$categoryid" to get to the right category page. So i tried $var_value = $_GET['cat']; This also did not work.
What is the correct way to do this?
Why don't you create a custom widget for your sidebar ?
You can manage "anything" you want from your widget PHP code, and display depending.

How to add a custom field made with acf into the excerpt while using genesis in WP

I want to take the text from an custom field made with acf, and put it into the excerpts shown the posts shown on the front page. I am using genesis, and I think I have to do "remove_action" and "add_action" to do this, but I can't figure out how to do it.
You can try a filter that WordPress has called the_excerpt
add_filter('the_excerpt', 'your_function_name');
function your_function_name($excerpt) {
$my_acf_field = the_field('my_acf_field');
return $my_acf_field . ':' . $excerpt;
}
This would append the acf field string in the beginning of the excerpt. If you want this to apply only on the front page, you can only add this code in your front-page.php file OR you can put the above code in your functions.php and use the Wordpress conditional is_front_page() in the function code above.
(This code is untested)

Dropdown of existing posts in a metabox

I want to have ability to choose for each page what post should appear in a sidebar, from multiple posts type. So I understand that I need a meta box with a dropdown list of all posts, but I don't know how to build this in functions.
I only found this solution which is quite similar to what I want, but this doesn't help me to much, because I can only choose from a single post type and display only in post pages.
There is a free plugin that will solve all of your woes. It's called ACF or Advanced Custom Fields. It has the ability to add a list of posts to a field and attach that field to pages. Here's how you'd do it:
First install the plugin and navigate to the custom fields screen. Setup your field exactly like this:
Then in the options below that section you need to select these options:
That will tell ACF to put the field only on pages. After you have set that up you will get a little sidebar block like this:
You can then select each post for the page and it will return that object on the frontend. You do need to use a little code to get the frontend to spit out the posts you need. Here is the code to get a frontend option from ACF. Inside of the sidebar.php file you need to add this code:
global $post; // Get the global post object
$sidebar_posts = get_field('posts', $post->ID); // Get the field using the post ID
foreach($sidebar_posts as $sidebar_post){ // Loop through posts
echo $sidebar_post->post_title; // Echo the post title
}
This will simply loop through the posts you select and echo out the title. You can do more with this by adding some other Wordpress post functions using setup_postdata(). This will allow you to do things like the_title() and the_content().
Hope this helps!

Can WordPress post content be external data (not from the database)

I am trying to display a page based on some data returned from an external API (Amazon). This data is formatted then, has to be displayed on a page, created on the fly, based on URL querys. I can already do this with shortcodes but this has to be from the query.
I see all kinds of info in the codex on returning custom query_posts into the loop from the database. However, I cannot find info getting external data to appear on a page.
Is this possible in WordPress? (anything is possible, right?) Just point me to some functions, filters or tutorials please.
If I understand you correctly, you want to retrieve data dynamically and display it in a WordPress page?
There are many ways to do this, but here's one option:
Create a Page Template
Create a WordPress page and use the Page Template created in step 1.
Edit the Page Template to call the external API and display the data
I'm guessing you've been trying to find a way to do this from the "content" of a page or post, but the easiest way is to put the code in a custom Page Template.
UPDATE: If you want to programmatically create a page, this might work for you: http://wordpress.org/support/topic/how-to-create-pages-programmatically?replies=5#post-1230619
http://www.prelovac.com/vladimir/wordpress-shortcode-snippet-to-display-external-files
This is a snippet to display external data in a post.
Would this help? If the dynamic page was a HTML page and THEM displayed in WP.
Yep, Thats possible.
I Did by using bridges.
You can do it by add "add_meta_boxes" action.
Inside the meta box function you can add call and get the external page contents, or can give your own forms .etc
My Code :
/*
* Add Meta Product Type Field to POST
*/
add_action('add_meta_boxes', 'meta_box_product_type_add');
My Meta Box
/*
* Product Type Meta Box Init
*/
function meta_box_product_type_add()
{
add_meta_box('ptype_testing', 'Product Type', 'add_ptype', 'testing', 'normal', 'high');
}
/*
* Product Type Field
*/
function add_ptype()
{ ?>
<label>Type of Product : </label>
<select name="ptype" id="ptypes">
<option>----Select----</option>
<option>Physical</option>
<option>Virtual</option>
</select>
<label>Unit :</label>
<select name="punit" id="units">
<option>----Select----</option>
<option>KG</option>
<option>Mtr</option>
<option>Ltr</option>
</select>
<?php
}
Try It....

Resources