How to use Product Archive page as home page? - wordpress

I would like the page example.com/product-category/tyres to be the home page. When anyone lands on example.com it will show the page example.com/product-category/tyres
Before I attempted to do so, I thought it is a straightforward task but it doesn't appear to be so. There is no option to set up product category page as static post or home page.
Tried the following:
[product_category ids="123"]
add_action( 'customize_register', 'th_customize_register' );
function th_customize_register($wp_customize) {
// Add: Drop Down Pages
$wp_customize->add_setting( 'th_portfolio_page_id', array (
'sanitize_callback' => 'absint',
) );
$wp_customize->add_control('th_portfolio_page_id', array(
'label' => esc_html__('Portfolio Page', 'themeshash'),
'description' => esc_html__( 'You must have to define it if you want to set your portfolio as your homepage.', 'themeshash' ),
'section' => 'title_tagline',
'type' => 'dropdown-pages',
) );
}
add_action( 'customize_register', 'th_customize_register' );
if ( get_option('page_on_front') == get_theme_mod('th_portfolio_page_id') ) {
add_action("pre_get_posts", "th_assign_portfolio_page");
function th_assign_portfolio_page($wp_query){
//Ensure this filter isn't applied to the admin area
if(is_admin()) {
return;
}
if($wp_query->get('page_id') == get_option('page_on_front')):
$wp_query->set('post_type', 'tyres');
$wp_query->set('page_id', ''); //Empty
//Set properties that describe the page to reflect that
//we aren't really displaying a static page
$wp_query->is_page = 0;
$wp_query->is_singular = 0;
$wp_query->is_post_type_archive = 1;
$wp_query->is_archive = 1;
endif;
}
}

Related

Get the top most menu item of a page menu using the page id - wordpress

I want to be able to fetch the label of the topmost menu item of a child menu with the child menu page id.
For ex: - I have the page id of test page. So, I want to fetch the text L1 which is the topmost parent menu of test menu
Note: test page is not the current page - I have only its page id present.
I used the below code to solve my problem.
function get_l1_menu_label( $page_id, $menu = 'primary-menu' ) {
//get menu
$all_menus = new WP_Query( [
'post_type' => [ 'nav_menu_item' ],
'meta_key' => '_menu_item_object_id',
'meta_value' => $page_id, // page id here
'tax_query' => [
[
'taxonomy' => 'nav_menu',
'field' => 'slug',
'terms' => $menu, //menu slug here
]
],
'fields' => 'ids'
] );
$current_page_menu_id = $all_menus->posts[0];
if ( !$current_page_menu_id ) {
//looks like the page is not menu
return '';
}
$parent_menu_id = (int) get_parent_menu_label_recursion($current_page_menu_id);
//get the custom nav title set in menu admin
$title = get_the_title( $parent_menu_id );
if ( $title !== '' ) {
return $title;
} else {
//the page orginal title is the label
$parent_menu_page_id = get_post_meta( $parent_menu_id, '_menu_item_object_id', true );
return get_the_title( $parent_menu_page_id );
}
}
//recursive function to fetch a top most parent id in nav menu
function get_parent_menu_label_recursion($menu_id) {
$parent_menu_id = (int) get_post_meta( $menu_id, '_menu_item_menu_item_parent', true );
if ( $parent_menu_id !== 0 ) {
//the parent exist - send the parent menu
return get_parent_menu_label_recursion($parent_menu_id);
} else {
//the menu sent to this function is the top most menu
return $menu_id;
}
}
Hope this helps someone.

ACF Dynamic select values not showing data

This question makes me crazy for almost 2 weeks. I know I am not expert in Wordpress, so I am seeking for help here.
I have create a href that when user click it will go to new page.
Add Class2
This href post the Post id. Url display:
[http://localhost/dev6/create-class/?post=289][1]
create-class page:
At create-class page,I am using GET method to display post id from url
$post = $_GET['post'];
I have acf form in create-class page for create new post. In this form, there have dynamic select field but the select field not display any data.
<?php acf_form(array(
'post_id' => 'new_post',
'field_groups' => array(150),
'post_title' => false,
'post_content' => false,
'new_post' => array(
'post_type' => 'classes',
'post_status' => 'publish',
),
'return' => '%post_url%',
'submit_value' => 'Submit',
//'updated_message' => 'Course Submit!',
)); ?>
in my function.php I create function for dynamic select:
function acf_load_t_first_name2_field_choices($field) {
global $post;
//$post = $_GET['post'];
// reset choices
$field['choices'] = array();
// get the textarea value from options page without any formatting
$choices = get_field('t_first_name',$post->ID);
// loop through array and add to field 'choices'
if( is_array($choices) ) {
foreach( $choices as $choice ) {
$field['choices'][ $choice ] = $choice;
}
}
// return the field
return $field;
}
add_filter('acf/load_field/name=t_first_name2', 'acf_load_t_first_name2_field_choices');
Is there something wrong with my code?
I don't believe this will work in your create-class template:
$post = $_GET['post'];
You will need to set something like this up in your functions.php file:
function custom_query_vars_filter($vars) {
$vars[] .= 'post';
return $vars;
}
add_filter( 'query_vars', 'custom_query_vars_filter' );
Then, in your create-class template you can get the variable from the URL like this:
$post = get_query_var('post');
See if that gets you going in the right direction.

WooCommerce Vertical Drop down Menu of product parent categories [duplicate]

I am developing a child theme for the Storefront Theme. I use the Product Category Widget as a dropdown under the header which fits my needs perfectly, though I need the same (if possible) dropdown menu to show up on every Category Page, instead of just the Main Page.
I am customizing this code which almost does it:
/**
* WooCommerce Extra Feature
* --------------------------
*
* Register a shortcode that creates a product categories dropdown list
*
* Use: [product_categories_dropdown orderby="title" count="0" hierarchical="0"]
*/
add_shortcode( 'product_categories_dropdown', 'woo_product_categories_dropdown' );
function woo_product_categories_dropdown( $atts ) {
extract( shortcode_atts(array(
'count' => '0',
'hierarchical' => '0',
'orderby' => ''
), $atts ) );
ob_start();
// Stuck with this until a fix for http://core.trac.wordpress.org/ticket/13258
wc_product_dropdown_categories( array(
'orderby' => ! empty( $orderby ) ? $orderby : 'order',
'hierarchical' => $hierarchical,
'show_uncategorized' => 0,
'show_counts' => $count
) );
?>
<script type='text/javascript'>
/* <![CDATA[ */
jQuery(function(){
var product_cat_dropdown = jQuery(".dropdown_product_cat");
function onProductCatChange() {
if ( product_cat_dropdown.val() !=='' ) {
location.href = "<?php echo esc_url( home_url() ); ?>/?product_cat=" +product_cat_dropdown.val();
}
}
product_cat_dropdown.change( onProductCatChange );
});
/* ]]> */
</script>
<?php
return ob_get_clean();
}
Now I need to hide the counters and show the empty Categories.
I haven't be able to get that.
How can I hide the counters and show the empty Categories?
In your code there was:
some mistakes in the code like wrong 'show_counts' that is 'show_count' (without s) … Now hiding counters is enabled and functional.
missing argument 'hide_empty' to show empty categories
In this shortcode you can alter the following optional arguments:
hierarchical that is disabled by default (set to '0')
hide_empty that is disabled by default (set to '0')
show_count that is now disabled by default (set to '0')
depth that is disabled by default (set to '0')
orderby set to category "order" by default (can be by names too: "name")
Added a custom hook woocommerce_product_categories_shortcode_dropdown_args that will allow extended customizations…
Here is the new code:
add_shortcode( 'product_categories_dropdown', 'woo_product_categories_dropdown' );
function woo_product_categories_dropdown( $atts ) {
// Attributes
$atts = shortcode_atts( array(
'hierarchical' => '0', // or '1'
'hide_empty' => '0', // or '1'
'show_count' => '0', // or '1'
'depth' => '0', // or Any integer number to define depth
'orderby' => 'order', // or 'name'
), $atts, 'product_categories_dropdown' );
ob_start();
wc_product_dropdown_categories( apply_filters( 'woocommerce_product_categories_shortcode_dropdown_args', array(
'depth' => $atts['depth'],
'hierarchical' => $atts['hierarchical'],
'hide_empty' => $atts['hide_empty'],
'orderby' => $atts['orderby'],
'show_uncategorized' => 0,
'show_count' => $atts['show_count'],
) ) );
?>
<script type='text/javascript'>
jQuery(function($){
var product_cat_dropdown = $(".dropdown_product_cat");
function onProductCatChange() {
if ( product_cat_dropdown.val() !=='' ) {
location.href = "<?php echo esc_url( home_url() ); ?>/?product_cat=" +product_cat_dropdown.val();
}
}
product_cat_dropdown.change( onProductCatChange );
});
</script>
<?php
return ob_get_clean();
}
Code goes in function.php file of the active child theme (or active theme).
Tested and works.
1) Example usage - All product categories and subcategories hierarchically displayed:
[product_categories_dropdown orderby='name' hierarchical='1']
In php code you can use it this way:
echo do_shortcode("[product_categories_dropdown orderby='name' hierarchical='1']");
or inserted in html tags:
<?php echo do_shortcode("[product_categories_dropdown orderby='name' hierarchical='1']"); ?>
2) Example usage - Only "main parent" product categories:
[product_categories_dropdown depth='1' hierarchical='1']
In php code you can use it this way:
echo do_shortcode("[product_categories_dropdown depth='1' hierarchical='1']");
or inserted in html tags:
<?php echo do_shortcode("[product_categories_dropdown depth='1' hierarchical='1']"); ?>
This is how it is displayed by default, for this it is not necessary to add more code.
Theirs would be that the products were grouped to the category of products that belong, and that it had a drop-down for each category of products, instead of a single one.
example:
Cars (dropdown)
Audi
Merdeces
BMW
Motorbike (dropdown)
Honda
Yamaha
Ducati

How do you target a specific page in Wordpress functions.php?

I am currently using the Multi Post Thumbnails plugin for Wordpress, but I only want the extra thumbnails provided by the plugin to show on one specific page. The plugin does not appear to natively support this functionality but it seems like something that would be pretty easy to add, I'm just not sure of the right way to go about it as I'm fairly new to Wordpress development.
The code for Multi Post Thumbnails is the following, which simply goes in functions.php:
if (class_exists('MultiPostThumbnails')) {
new MultiPostThumbnails(
array(
'label' => 'Secondary Image',
'id' => 'secondary-image',
'post_type' => 'page'
)
);
new MultiPostThumbnails(
array(
'label' => 'Tertiary Image',
'id' => 'tertiary-image',
'post_type' => 'page'
)
);
}
It seems to me it would just be a simple case of wrapping this in a check so that it only runs for a specific page ID, but I'm not quite sure how to go about doing that.
This is probably somewhat of a hack. To my knowledge post/page id's are not accessible from inside functions.php.
// get the id of the post/page based on the request uri.
$url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$post_id = url_to_postid($url);
// the id of the specific page/post.
$specific_post_id = 3;
// check if the requested post id is identical to the specific post id.
if ($post_id == $specific_post_id) {
if (class_exists('MultiPostThumbnails')) {
new MultiPostThumbnails(
array(
'label' => 'Secondary Image',
'id' => 'secondary-image',
'post_type' => 'page'
)
);
new MultiPostThumbnails(
array(
'label' => 'Tertiary Image',
'id' => 'tertiary-image',
'post_type' => 'page'
)
);
}
}
This is also probably a hack but it worked for me. I got stung by the AJAX 'post_id' back to the admin page once the image has been selected. My usage was for a slug but the function could easily be modified for a post ID.
function is_admin_edit_page( $slug ){
if( ( isset($_GET) && isset($_GET['post']) ) || ( isset($_POST) && isset($_POST['post_id']) ) )
{
$post_id = 0;
if(isset($_GET) && isset($_GET['post']))
{
$post_id = $_GET['post'];
}
else if(isset($_POST) && isset($_POST['post_id']))
{
$post_id = $_POST['post_id'];
}
if($post_id != 0)
{
$c_post = get_post($post_id);
if( $c_post->post_name == $slug )
{
return true;
}
}
}
return false;
}
if( is_admin_edit_page('work') ) {
new MultiPostThumbnails(
array(
'label' => 'Hero 1 (2048px x 756px JPEG)',
'id' => 'am-hero-1',
'post_type' => 'page'
)
);
}

Adding User Profile Fields to Wordpress Buddypress to Include "Favorites"

I'm trying to figure out a way for members on my Wordpress (Buddypress) site to pick their "favorite movies, books, etc."
It would be nice if, instead of members simply typing a list of these things, they could select from books already in the system, and add more as the please in the future.
I'm hoping that there is an easy answer to this, such as a plugin that I can use, or, at least, modify. Does anyone know of anything that I can look into?
Your title asks how to add a user profile field. Here is the code to add as many fields as you like. Once you add the field, you can easily place additional inputs or options on the custom tab page for users to enter their own favorites.
function my_test_setup_nav() {
global $bp;
$parent_slug = ‘test’;
$child_slug = ‘test_sub’;
//name, slug, screen, position, default subnav
bp_core_new_nav_item( array(‘name’ => __( ‘Test’ ),’slug’ => $parent_slug,’screen_function’ => ‘my_profile_page_function_to_show_screen’,'position’ => 40,’default_subnav_slug’ => $child_slug ) );
/* Add the subnav items to the profile */
// name, slug, parent_url, parent slug, screen function
bp_core_new_subnav_item( array( ‘name’ => __( ‘Home’ ), ‘slug’ => $child_slug, ‘parent_url’ => $bp->loggedin_user->domain . $parent_slug.’/', ‘parent_slug’ => $parent_slug, ‘screen_function’ => ‘my_profile_page_function_to_show_screen’ ) );
bp_core_new_subnav_item( array( ‘name’ => __( ‘Random Page’ ), ‘slug’ => ‘random’, ‘parent_url’ => $bp->loggedin_user->domain . $parent_slug.’/', ‘parent_slug’ => $parent_slug, ‘screen_function’ => ‘my_profile_page_function_to_show_screen234′ ) );
}
function my_profile_page_function_to_show_screen() {
//add title and content here – last is to call the members plugin.php template
add_action( ‘bp_template_title’, ‘my_profile_page_function_to_show_screen_title’ );
add_action( ‘bp_template_content’, ‘my_profile_page_function_to_show_screen_content’ );
bp_core_load_template( apply_filters( ‘bp_core_template_plugin’, ‘members/single/plugins’ ) );
}
function my_profile_page_function_to_show_screen_title() {
echo ‘wptaskforce title’;
}
function my_profile_page_function_to_show_screen_content() {
echo ‘wptaskforce content’;
}
//random page content:
function my_profile_page_function_to_show_screen234() {
//add content here – last is to call the members plugin.php template
add_action( ‘bp_template_content’, ‘my_profile_page_function_to_show_screen234_content’ );
bp_core_load_template( apply_filters( ‘bp_core_template_plugin’, ‘members/single/plugins’ ) );
}
function my_profile_page_function_to_show_screen234_content() {
echo ‘This is a random page.’;
}
add_action( ‘bp_setup_nav’, ‘my_test_setup_nav’ );

Resources