Menu Submenu with PHP Classes and Objects - php

I need to make a menu with a submenu. Menu 'id' => 'parent1' = submenu 'parent' => 'parent1'.
If parent has no child echo only parent. If parent has childrens insert all children inside parent. Maybe try with a for loop, or some other solution?
If a child exists, each submenu child should be inserted in its parent by id. How?
The code example should echo:
<div class="menu" href="myurl">Parent 1
<div class="submenu" href="myurl">Child 1</div>
<div class="submenu" href="myurl">Child 2</div>
</div>
<div class="menu" href="myurl">Parent 2
<div class="submenu" href="myurl">Child 3</div>
</div>
class menu {
public $id;
public $parent;
public $name;
public $url;
public function add_node($args) {
echo '
<div class="menu" href="' . $args['url'] . '">' . $args['name'] . '
<div class="submenu" href="' . $args['url'] . '">' . $args['name'] . '</div>
</div>';
}
}
if (class_exists('menu')){
global $menu;
$menu = new menu();
}
function menu_parent_child(){
$args = array(
'id' => 'parent1',
'name' => 'Parent 1',
'url' => 'myurl'
);
$args = array(
'parent' => 'parent1',
'name' => 'Child 1',
'url' => 'myurl'
);
$args = array(
'parent' => 'parent1',
'name' => 'Child 2',
'url' => 'myurl'
);
$args = array(
'id' => 'parent2',
'name' => 'Parent 2',
'url' => 'myurl'
);
$args = array(
'parent' => 'parent2',
'name' => 'Child 3',
'url' => 'myurl'
);
}

Related

Custom shortcode being shown in WPBakery backend rather than the block

I have a nested element called Hero Slider. Hero slider is the container and it can only contain a Slider item.
However, my shortcode is being shown in the WPBakery backend maybe suggesting a syntax error, but I cannot see anything that is causing it to appear this way:
The block doesn't even appear when adding a new element.
Here's my custom element: (init.php)
<?php
if (!defined('ABSPATH')) die('-1');
class vcHeroSlider extends WPBakeryShortCode {
// 1. Define constants at compile time (used in mapping)
const slug = 'tp_hero_slider';
const base = 'tp_hero_slider';
// 2. Integrate with hooks
function __construct() {
// For the parent wrapper
add_action( 'vc_before_init', array( $this, 'tp_heroSlider_mapping' ) );
add_shortcode( 'tp_hero_slider', array( $this, 'tp_heroSlider_html' ));
// For child / nested
add_action( 'vc_before_init', array( $this, 'tp_heroSlider_content_mapping' ) );
add_shortcode( 'tp_hero_slider_content', array( $this, 'tp_heroSlider_content_html' ));
}
// 3. Map for parent element
public function tp_heroSlider_mapping() {
vc_map(
array(
'icon' => get_template_directory_uri().'/assets/src/images/html.svg',
'name' => __( 'Hero Slider' , "text-domain" ),
'base' => 'tp_hero_slider',
'description' => __( 'Add slick slider to your page.', "text-domain" ),
'as_parent' => array('only' => 'tp_hero_slider_content'), // set as parent of the content map/html
'content_element' => true,
'show_settings_on_create' => false,
"js_view" => 'VcColumnView',
"category" => __('Hero', "text-domain" ),
'params' => array(
array(
"type" => "textfield",
"heading" => __( "Extra Class Name", "text-domain" ),
"param_name" => "el_class",
"description" => __( "Extra class to be customized via CSS", "text-domain" )
),
array(
'type' => 'css_editor',
'heading' => __( 'Custom Design Options', "text-domain" ),
'param_name' => 'css',
'group' => __( 'Design options', "text-domain" ),
),
),
)
);
}
// 4. Map for child element
public function tp_heroSlider_content_mapping() {
vc_map(
array(
'icon' => get_template_directory_uri().'/assets/src/images/html.svg',
'name' => __('Slider Item', "text-domain" ),
'base' => 'tp_hero_slider_content',
'description' => __( 'Add slide to hero.', "text-domain" ),
"category" => __('Content', 'text-domain'),
'content_element' => true,
'as_child' => array('only' => 'tp_hero_slider'),
'params' => array(
array(
'type' => 'textfield',
'heading' => __( 'Title', 'text-domain'),
'param_name' => 'title',
'value' => esc_html__( '', 'text-domain'),
'admin_label' => true,
'weight' => 0,
'group' => __( 'Content', 'my-text-domain' ),
),
array(
'type' => 'textarea',
'class' => '',
'heading' => __( 'Standfirst', 'text-domain'),
'param_name' => 'standfirst',
'value' => esc_html__( '', 'text-domain'),
'admin_label' => false,
'weight' => 0,
'group' => __( 'Content', 'my-text-domain' ),
)
),
)
);
}
// 5. Mapping markup of parent
public function tp_heroSlider_html( $atts, $content = null) {
$output = '';
$el_class = '';
extract(
shortcode_atts(
array(
'el_class' => '',
), $atts
)
);
static $i = 0;
$output = '<div id="slickslider-'.$i++.'" class="Slick-Slider heroSlider">'. do_shortcode($content) .'</div>';
return $output;
}
// 6. Mapping markup of child
public function tp_heroSlider_content_html( $atts, $content = null ) {
$output = '';
extract(
shortcode_atts(
array(
'title' => '',
'standfirst' => '',
), $atts
)
);
$background_img = wp_get_attachment_image_src(intval($background_img), 'full');
$background_img = $background_img[0];
$output .= '
<!-- Slide -->
<div class="heroSlider__slide">
<div class="overlay"></div>
';
$output .= '
<div class="container">
<div class="row justify-content-center justify-content-lg-start">
<div class="col-10 col-md-6 d-flex flex-column text-center text-lg-left content">';
if (!empty($title)) {
$output .= '<h1>' . $title . '</h1>';
}
if (!empty($standfirst)) {
$output .= '<p class="standfist">' . $standfirst . '</p>';
}
$output .= '
</div>
</div>
</div>
</div>
<!-- Slide -->
';
return $output;
}
}
// 7. Add the container functionality (so you can choose a slider element within the hero_slider element
if(class_exists('WPBakeryShortCodesContainer')){
class WPBakeryShortCode_tp_hero_slider extends WPBakeryShortCodesContainer {}
}
if(class_exists('WPBakeryShortCode')){
class WPBakeryShortCode_tp_hero_slider_content extends WPBakeryShortCode {}
}
new vcHeroSlider(); ?>
How I've added new element:
add_action( 'vc_before_init', 'vc_before_init_actions' );
function vc_before_init_actions() {
$theme = get_template_directory();
require_once( $theme.'/vc_elements/hero-slider/init.php');]
}
Although I don't believe the above is the issue, because I have several other elements defined like that and they all work.

How to show image on front end WordPress page by visual composer?

function italy_menu( $menu1, $menu2 ){
ob_start();
$menuatts = shortcode_atts(array(
'menu_image' => '',
'menu_title' => '',
'menu_money' => '',
), $menu1 );
?>
<div class="post">
<img src="<?php echo $menuatts['menu_image']; ?>"/>
<div class="wrapper">
<h5><?php echo $menuatts['menu_title']; ?></h5>
<span><?php echo $menuatts['menu_money']; ?></span>
</div>
</div>
<?php
return ob_get_clean();
}
add_shortcode('menu','italy_menu');
if(function_exists(vc_map)){
vc_map(array(
'name' => 'Italy Food Menu',
'base' => 'menu',
'params' => array(
array(
'param_name' => 'menu_image',
'type' => 'attach_images',// How to show image on front page.
'heading' => 'Menu Image'
),
array(
'param_name' => 'menu_title',
'type' => 'textfield',
'heading' => 'Menu Title'
),
array(
'param_name' => 'menu_money',
'type' => 'textfield',
'heading' => 'Menu Money'
),
)
));
}
How to show image on front end WordPress page by visual composer ? I want to show image on my front-end by visual composer. I am using > 'type' => 'attach_images', But its not showing the image... Here is my code.
$gallery = shortcode_atts(
array(
'post_gallery' => 'post_gallery',
), $atts );
$image_ids=explode(',',$gallery['post_gallery']);
$sigle_img = wp_get_attachment_image_src($image_ids[0], "large");
$img="<div id='tourGallery'><img class='imgthumb' id='' src='".$sigle_img[0]."' style='width:100%' alt=''></div><div id='galleryThumbs'>";
for($i=0;$i<=9;$i++)
{
if($image_ids[$i]!="")
{
$imgs = wp_get_attachment_image_src($image_ids[$i], "large");
$img.="<div><img class='imgthumb' id='' src='".$imgs[0]."' style='width:100%' alt=''></div>";
}
}
$img.="</div></div>";
return $img;

Dynamic nav bar with php works properly but when trying to input sub-menu with foreach getting error

My nav bar is working properly if i have just menu items. When i add sub-menu items i get error and display only the first item.
I think that probably i have my php tags in wrong position. Tryied to change that but still get error.
Warning: Illegal string offset 'title' in
What am i doning wrong?
this is my code
<div class="container">
<div class="navbar-header">
<button class="navbar-toggle" data-toggle="collapse" data-target=".btnCollapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="collapse navbar-collapse btnCollapse">
<ul class="nav navbar-nav">
<li>Home</li>
<?php
foreach ($navItems as $item) { ?>
<li class="dropdown">
<?php echo $item['title']; ?><span class="caret"></span>
<?php
if(isset($item['dropdown'])){ ?>
<ul class="dropdown-menu" role="menu">
<?php foreach ($item as $subitem){ ?>
<li>
<div class="container">
<div class="row">
<ul class="col-sm-3">
// this line gives me error
<li> <?php echo $subitem['title'];?>
</ul>
<?php }
} ?>
<?php } ?>
</li>
</ul>
</div>
</div>
And my nav.php with arrays
//Nav Items
$navItems = array(
array(
'slug' => "navItem.php",
'title' => " navItem",
'dropdown' => array(
array(
'slug' => "subitem.php",
'title' => "subitem"
),
array(
'slug' => "subitem.php",
'title' => "subitem"
),
array(
'slug' => "subitem.php",
'title' => "subitem"
),
array(
'slug' => "subitem.php",
'title' => "subitem"
),
array(
'slug' => "subitem.php",
'title' => "subitem"
),
array(
'slug' => "subitem.php",
'title' => "subitem"
),
array(
'slug' => "subitem.php",
'title' => "subitem"
),
)
),
array(
'slug' => "navItem.php",
'title' => "navItem"
),
array(
'slug' => "navItem.php",
'title' => "navItem"
),
array(
'slug' => "navItem.php",
'title' => "navItem"
),
array(
'slug' => "navItem.php",
'title' => "navItem "
),
array(
'slug' => "navItem.php",
'title' => "navItem"
),
array(
'slug' => "navItem.php",
'title' => "navItem"
),
array(
'slug' => "navItem.php",
'title' => "navItem"
)
);
?>
In the line where the error occurs you are doing the following line of code:
<?php foreach ($item as $subitem){ ?>
$item contains 3 Elements with the keys 'title', 'slug' and 'dropdown'. Now you try to iterate over $item:
//foreach
$item = ['title'=>'mytitle', 'slug'=>'myslugh', 'dropdown'=>array(...)];
On the first iteration you try the following:
$subitem = 'mytitle';
$title = $subitem['title'];
The problem is you iterate over the wrong array, you have to use $foreach($item['dropdown'] as $subItem) instead.

custom vc elements won't render on translated pages wpml plugin

I have created a custom post type for a slider posts and then a custom visual composer element to gather those posts from that post type and then assemble it to create a slider but it won't render on the translated pages like if I create a german page, the custom vc element (slider) won't render or show unto that page. Any help, ideas please?
here's the vc element
<?php class axada_custom_elements extends WPBakeryShortCode {
// Element Init
function __construct() {
add_action( 'init', array( $this, 'axada_custom_elements_mapping' ) );
add_shortcode( 'axada_slider', array( $this, 'axada_slider_html' ) );
}
// Element Mapping
public function axada_custom_elements_mapping() {
// Stop all if VC is not enabled
if ( !defined( 'WPB_VC_VERSION' ) ) {
return;
}
// Map the block with vc_map()
vc_map(
array(
'name' => __('Axada Slider', 'text-domain'),
'base' => 'axada_slider',
'description' => __('Axada Slider', 'text-domain'),
'category' => __('Axada Elements', 'text-domain'),
"as_parent" => array('except' => ''), // Use only|except attributes to limit child shortcodes (separate multiple values with comma)
"content_element" => true,
"show_settings_on_create" => true,
"is_container" => true,
'icon' => get_template_directory_uri().'/img/axada-short-logo.png',
'params' => array(
array(
'type' => 'textfield',
'holder' => 'div',
'class' => 'title-class',
'heading' => __( 'Slider ID', 'text-domain' ),
'param_name' => 'slider_id',
'value' => __( '', 'text-domain' ),
'description' => __( 'Slider ID', 'text-domain' ),
'admin_label' => false,
'weight' => 0,
'group' => 'Custom Group',
),
array(
'type' => 'textfield',
'holder' => 'div',
'class' => 'title-class',
'heading' => __( 'Slider Classes', 'text-domain' ),
'param_name' => 'slider_class',
'value' => __( '', 'text-domain' ),
'description' => __( 'Slider Classes', 'text-domain' ),
'admin_label' => false,
'weight' => 0,
'group' => 'Custom Group',
),
array(
'type' => 'textfield',
'holder' => 'div',
'class' => 'title-class',
'heading' => __( 'Slider Custom Styles', 'text-domain' ),
'param_name' => 'slider_custom_styles',
'value' => __( '', 'text-domain' ),
'description' => __( 'Slider Custom Styles', 'text-domain' ),
'admin_label' => false,
'weight' => 0,
'group' => 'Custom Group',
),
),
)
);
}
// Element HTML
public function axada_slider_html( $atts ) {
// Params extraction
extract(
shortcode_atts(
array(
'slider_id' => '',
'slider_class' => '',
'slider_custom_styles' => '',
),
$atts
)
);
$slider_custom_styles = $slider_custom_styles !== '' ? 'style="'.$slider_custom_styles.'"' : '';
// Fill $html var with data
//query for slider
ob_start();
//query for slider
$args = array(
'post_type' => 'axada_slider',
'meta_key' => '_arrangement_value_key',
'orderby' => 'meta_value',
'order' => 'ASC',
'post_status' => 'publish',
);
$slider = new WP_Query( $args );
if ( $slider->have_posts() ) :
//start slider
?>
<section class="kenburns image-slider slider-all-controls controls-inside pt0 pb0 height-70 vid-bg bg-dark" id="home-slider-wrapper">
<ul<?php echo $slider_id !== '' ? ' id="'.$slider_id.'" ' : ' '; ?>class="slides<?php echo $slider_class !== '' ? ' '.$slider_class : ''?>"<?php echo $slider_custom_styles !== '' ? ' style="'.$slider_custom_styles.'" ' : ''; ?> >
<?php while ( $slider->have_posts() ) : $slider->the_post(); ?>
<li>
<?php if(get_post_meta( get_the_ID(), '_axada_slider_video_value_key', true ) && get_post_meta( get_the_ID(), '_axada_slider_video_value_key', true )!==''){?>
<div class="player" data-video-id="<?php echo get_post_meta( get_the_ID(), '_axada_slider_video_value_key', true ); ?>" data-start-at="0"></div>
<div class="background-image-holder bg-size-cover">
<img alt="image" class="background-image" src="<?php the_post_thumbnail_url(); ?>" />
</div>
<?php }else{ ?>
<div class="background-image-holder bg-size-cover">
<img alt="image" class="background-image" src="<?php the_post_thumbnail_url('full'); ?>">
</div>
<div class="container v-align-transform">
<div class="row text-center">
<div class="full-width divider mb-xs-64"></div>
<?php the_content(); ?>
</div>
</div>
<?php } ?>
</li>
<?php endwhile; ?>
</ul>
</section>
<?php
//end slider
endif;
return ob_get_clean();
}

How to echo dynamically selected categories of back-end in front end using wordpress

I have in functions.php
//Example of select field
$this->settings['select'] = array(
'section' => 'general',
'title' => __( 'Example Select' ),
'desc' => __( 'This is a description for the drop-down.' ),
'type' => 'select',
'std' => '',
'choices' => array(
'choice1' => 'Choice 1',
'choice2' => 'Choice 2',
'choice3' => 'Choice 3'
)
);
//Here i have managed to echo the categories dynamically in the back-end
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 0
);
$categories = get_categories($args);
$categories_name = array();
foreach($categories as $category){
$categories_name[] = $category->name;
}
$this->settings['categoriesmain1'] = array(
'section' => 'general',
'title' => __( 'Select Left Block Category' ),
'desc' => __( 'Here you can select the main category for main left block.' ),
'type' => 'select',
'std' => '',
'choices' => $categories_name // this returns the category names in a select field
);
$settings = get_option('mytheme_options');
$my_choices_cat1 = $settings['categoriesmain1'];
$this->settings['categoriesmain2'] = array(
'section' => 'general',
'title' => __( 'Select Center Block Category' ),
'desc' => __( 'Here you can select the main category for main center block.' ),
'type' => 'select',
'std' => '',
'choices' => $categories_name
);
$settings = get_option('mytheme_options');
$my_choices_cat2 = $settings['categoriesmain2'];
$this->settings['categoriesmain3'] = array(
'section' => 'general',
'title' => __( 'Select Right Block Category' ),
'desc' => __( 'Here you can select the main category for main right block.' ),
'type' => 'select',
'std' => '',
'choices' => $categories_name
);
$settings = get_option('mytheme_options');
$my_choices_cat3 = $settings['categoriesmain3'];
index.php
<?php $settings = get_option('mytheme_options');
query_posts('category_name='.$settings["categoriesmain1"].'&posts_per_page=1');
while ( have_posts() ) : the_post(); ?>
<div class="boxes-third boxes-first">
<div class="boxes-padding">
<div class="bti">
<div class="featured-images"><?php the_post_thumbnail(array(300,300)); ?></div>
<div class="featured-titles"><?php echo get_the_title($post->ID); ?></div>
</div>
<div class="featured-text"><?php the_content('',FALSE,''); ?></div>
</div>
<span class="box-arrow"></span>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<?php $settings = get_option('mytheme_options');
query_posts('category_name='.$settings["categoriesmain2"].'&posts_per_page=1');
while ( have_posts() ) : the_post(); ?>
<div class="boxes-third">
<div class="boxes-padding">
<div class="bti">
<div class="featured-images"><?php the_post_thumbnail(array(300,300)); ?></div>
<div class="featured-titles"><?php echo get_the_title($post->ID); ?></div>
</div>
<div class="featured-text"><?php the_content('',FALSE,''); ?></div>
</div>
<span class="box-arrow"></span>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<?php $settings = get_option('mytheme_options');
query_posts('category_name='.$settings["categoriesmain3"].'&posts_per_page=1');
while ( have_posts() ) : the_post(); ?>
<div class="boxes-third boxes-last">
<div class="boxes-padding">
<div class="bti">
<div class="featured-images"><?php the_post_thumbnail(array(300,300)); ?></div>
<div class="featured-titles"><?php echo get_the_title($post->ID); ?></div>
</div>
<div class="featured-text"><?php the_content('',FALSE,''); ?></div>
</div>
<span class="box-arrow"></span>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
The problem is that in index.php the $settings[] is not echoed properly.
For example
query_posts('category_name='.$settings["categoriesmain3"].'&posts_per_page=1');,
if i add var_dump or print_r it will echo something like Array {} (an empty array) instead of echoing the category name, should be
query_posts('category_name=Category3&posts_per_page=1');
where Category3 is the selected category from the back-end by the user.
My functions.txt is http://pastebin.ca/2476028
Please help me i am doing my best on learning wordpress.
ah got it where i should have var_dump, now i did it to
var_dump($settings["categoriesmain2"]) and it is echoing string(1)
"1", instead of the category. For the first one is string(1) "0" and
third one string(1) "2" that means that is echoing the first select,
second select, third select
its doing that because of it is an array, you could get each value as an id then get it's name using that id .
UPDATE: Example
// an array contains cats ids
$categories_ids = array(1,5,7,3);
// sql stmnt to get all info about each id
$sql = 'select * from categories where id in("'.implode('",'", array_values($categories_ids) ).'")';
This is just an example , you can do the same in yours .

Categories