How to store and gett variables in a widget? - wordpress

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.

Related

hide shortcode when one is empty

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');

WordPress Page Templates for Custom Post Type

I have a custom post type, "Store Pages" for instance, which is an almost identical duplicate of the default Wordpress "Page" post type.
Like the "page" post type, I would like to create page templates (not post-type templates) and be able to select them from the "Template" drop-down within the "Page attributes" box in the page editor.
I have created several templates, but the drop-down menu does not appear; I am assuming this is because a custom post types does not allow support for this.
Is there a way I can create page templates for a custom post type without using "single-{post-type-name}.php" and having a dozen queries to load up different template files?
I have double checked the comments for the templates are correct as they appear when I create a new page (post type, "Page").
Help would be greatly appreciated.
starting 4.7 you can use the new feature described here https://make.wordpress.org/core/2016/11/03/post-type-templates-in-4-7/
<?php
/*
Template Name: Full-width layout
Template Post Type: post, page, product
*/
// … your code here
If I understood correctly you want a Select template dropdown for your custom post type.
You can do this easily through Advanced Custom Fields, here's a quick guide how to get through.
After installing the plugin you can access the Custom Field section, if you open it and click Add new it bring you to the field editor. Name it whatever you want, it's just for administrative display.
On Field type choose "Select", this will allow you to construct a select box on your backend, keep in mind the value of "Field name" you will need this later on the code.
Scrolling down you can add the values of the select box in the following format: "key value : Textual label" just assume for now you want 2 templates, one for audio posts and one for video posts.
If you keep scrolling down you can see the display rule for this field group, now you will have "post" and "page" by default, when you add different content types you will have the additional content types here to select, just go ahead and pick yours.
And, ta-da. If you go on the custom content type edit window you will find your new fresh select box here waiting for you.
The code integration now is very simple, just go onto your single-{post-type-name}.php template and pull the custom field data into your loop. Then you can use this to use get_template_part() to pull your custom templates.
<?php $template_type = get_field('template'); // This must match with the field name value ?>
<?php if (isset($template_type) && !empty($template_type)): ?>
<?php get_template_part( 'store', $template_type ); ?>
<?php else: ?>
// You should have a fallback for the all the existing posts without template set or if you create a new post without a template.
<?php endif; ?>
In this specific example the pulled template files will be in the format of store-{key-value-of-the-selectbox}.php, of course you can readapt that for you best convenience.

Archive displayed on a custom page to be used in menu structure

I would like to direct users to appropriate archive pages from within the menu. If I want this I need a page that I can attach to the menu.
How would I display the exact same stuff as on the archive page (archive.php) on another page so that pagination and functionalities remain the same, but some stuff will be taken from the actual page ? (can create custom page template of course)
I'll still have to show a sidebar for the page that you visited and not archive's sidebar
Breadcrumbs path will still have to show current menu item position and not archive page path
To show sidebar from the actual page is of most importance here.
EDIT
What I want to achieve is this actually:
Lets say I have a page
http://my.page/subpage/something/notifications/
I want that on this page, I can display exactly the same stuff as on the certain archive page which is here:
http://my.page/subpage/notification/
('notification' is a custom post type here)
I already have a solution that displays all archive stuff on another page (created a page template for that), but its a bit complicated to display title, breadcrumbs and sidebar properly for each page, since some of these should stay the same as they would be, but some should take the value of another site.
You can create a custom page template and assign it to a page. On that page you can use get_posts() to query the posts you want, like this:
global $post;
$posts = query_posts( /* Your args here*/ );
foreach($posts as $post) {
setup_postdata($post);
// Do your stuff as in archive.php
}
wp_reset_postdata();

Wordpress: Updating tags in large number of Posts

I have more than 2000 posts and each post contains almost 1000+ tags. Now i want to update all the posts with new additional tags in one go. Is there any simple way??
Regards-
From the dashboard:
Go to Posts -> All Posts
Beside Title there is a checkbox, check it and it will check all the boxes down the page.
Above the checkbox there is a dropdown list, select Edit and press Apply
You will see an input box called Tags, add the new tag in there
Next click the update button and those posts will now have the new tag.
I would recommend that you do this for one or two posts first to make sure it will work as you want and not break/mess anything up.
You have one more option, if some of the tags are similar for all the posts. You can post a piece of code in your function.php file and put the list of common tags there :
function my_tag_update () {
GLOBAL $post;
wp_set_post_tags( $post->ID, 'tag1, tag2, tag3, tag4', true );
}
add_action('publish_post', 'my_tag_update');
add_action('save_post', 'my_tag_update');
So, in this case when you create a new post or edit a post, these defined tags will add to it automatically.

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!

Resources