How to add a custom button on my wordpress nav menu - php

I want to add a extra item like my logo before HOME item on the main nav.
My Wordpress menu looks like this
HOME | ABOUT | BLOG | CONTACT
$nav_args = array(
'theme_location' => 'nav',
'container' => 'none',
'menu_class' => 'level-1',
'depth' => apply_filters( 'yit_main_nav_depth', 3 ),
);
if ( has_nav_menu( 'nav' ) )
$nav_args['walker'] = new YIT_Walker_Nav_Menu();
wp_nav_menu( $nav_args );
this is the view
<div id="nav">
<div class="container">
<?php do_action( 'yit_main_navigation') ?>
</div>
</div>

I have solve this by adding the following code into my function.php
add_filter( 'wp_nav_menu_items', 'add_logo_nav_menu', 10, 2 );
function add_logo_nav_menu($items, $args){
$newitems = '<li><a title="logo" href="#">LOGO</a></li>';
$newitems .= $items;
return $newitems;
}

Related

Remove page link when equal to '#' in wp_nav_menu function - For a custom html sitemap

I want to create a custom HTML sitemap using wp_nav_menu() because this function has the exact structure I want to display. It works, but the only problem I have is with some parent pages that are only used to display child pages. They have '#' as a link. So I want to display the title of these parent pages without the link.
This is the code I have so far. Maybe I'm completely wrong.
<?php
$webpages = wp_nav_menu( array(
'items_wrap' => '%3$s',
'menu' => 'Menu principal',
'container' => 'div',
'container_class' => 'pages-container',
'post_status' => 'publish'
) );
if ( $webpages ) {
$permalink = get_permalink();
foreach ( $webpages as $page ) :
setup_postdata( $page ); ?>
<li> <?php
if ($permalink === '#') {
echo strip_tags($permalink,'a');
}
else {
//'' . the_title(); '';
$permalink;
}
?> </li>
<?php
endforeach;
wp_reset_postdata();
}
?>
Any help or advise is greatly appreciated!
Thanks!

Adding divs between links in php menu

I want to add a div between the links in my Wordpress menu (not on the ends, just in between). I am using the following code to bring in my menu:
html
<?php wp_nav_menu( array( 'container_class' => 'main-nav', 'theme_location' => 'primary' ) ); ?>
functions.php
/* Register 'primary' navigation */
function wptutsplus_register_theme_menu() {
register_nav_menu( 'primary', 'Main Navigation Menu' );
}
add_action( 'init', 'wptutsplus_register_theme_menu' );
The output I'm looking for is the following:
link 1
<div class="divider"></div>
link 2
<div class="divider"></div>
link 2
This is how you get result https://www.screencast.com/t/APLrCeHCxLU.
You can try following code:-
wp_nav_menu( array( 'container_class' => 'main-nav','theme_location' => 'primary','after' => '<div class="divider"></div>') ) );

wordpress custom menu with page thumbnail

I am trying to get a menu with few links, and show their/link/page featured image. following is my code for functions.php.
function register_my_menus() {
register_nav_menus(
array(
'main-menu' => __( 'Main Menu' ),
'useful-links' => __( 'Userful Links' )
)
);
}
add_action( 'init', 'register_my_menus' );
and this is where i am calling it in the page.
<?php wp_nav_menu( array( 'theme_location' => 'useful-links', 'sort_column' => 'menu_order', 'container_class' => 'useful-links', 'menu'=>'Posts Menu' ) ); ?>
i know something is missing but kindly advice. my page look and feel allow 6 links and their images.
thanks
This should be in your functions.php to register the menu within WordPress.
if (!function_exists('wp_basicTemplateSetup')) {
function wp_basicTemplateSetup() {
// Register the Main Menus into WordPress
register_nav_menus(array(
'primary' => __('Main Menu', 'theme-name'),
));
}
}
add_action('after_setup_theme', 'wp_basicTemplateSetup');
In your theme, where the menu is sitting you need to tell the menu function, which menu to look for. So..:
// Variable that tells WordPress which menu to use, look at "Theme Location"
// Put this variable either at the top of your theme, of right *before* the function wp_nav_menu
$mainmenu = array(
'theme_location' => 'primary',
'container' => false,
'menu_class' => 'some-menu-css-class',
'depth' => 2
);
// The WordPress menu function
<?php wp_nav_menu($mainmenu); ?>
These 3 components are necessary to get your WordPress menu working.

Create own footer menu in Wordpress?

I do have two menus in my Wordpress-Page which are called
Mainmenu(Primary Menu)
Footer
I associated the "Mainmenu" to the header of the website. I want the footer of the Wordpresspage to look exactly like the header, only difference should be the Links in it. It should not be taken from the "Mainmenu", but from the menu called "Footer". The function of the Header looks like this:
echo '<nav id="rtp-primary-menu" role="navigation" class="rtp-nav-wrapper' . apply_filters( 'rtp_mobile_nav_support', ' rtp-mobile-nav' ) . '">';
rtp_hook_begin_primary_menu();
/* Call wp_nav_menu() for Wordpress Navigaton with fallback wp_list_pages() if menu not set in admin panel */
if ( function_exists( 'wp_nav_menu' ) && has_nav_menu( 'primary' ) ) {
wp_nav_menu( array( 'container' => '', 'menu_class' => 'menu rtp-nav-container clearfix', 'menu_id' => 'rtp-nav-menu', 'theme_location' => 'primary', 'depth' => apply_filters( 'rtp_nav_menu_depth', 4 ) ) );
} else {
echo '<ul id="rtp-nav-menu" class="menu rtp-nav-container clearfix">';
wp_list_pages( array( 'title_li' => '', 'sort_column' => 'menu_order', 'number' => '5', 'depth' => apply_filters( 'rtp_nav_menu_depth', 4 ) ) );
echo '</ul>';
}
rtp_hook_end_primary_menu();
echo '</nav>';
The Theme i am using is rtPanel. Can anyone tell me how i simply can change this function in the way that it will load its content from the Menu called "Footer" and not from the Primary Menu which is "Mainmenu"?
Please dont worry about the CSS and everything, i will take care about this. I really just want to now how i have to change this function to load the "Footer"-Menu in it and not the Primary-Menu(Mainmenu).
As an Attachment how it look in the menu:
Thank you very much advance!
This is really simple, in fact when you start developing wordpress theme this might be one of the very first items you need to learn.
You need to register a menu location for footer seciton. You can learn more in codex
register_nav_menu( 'footer', __( 'Footer Menu', 'theme-slug' ) );
This will register the menu for you. Since you already have the menu location just find the location parameter. We're going to use that in the following code. For now assume it 'footer' like the above one. And the code for the footer menu according to your referenced code-
echo '<nav id="rtp-primary-menu" role="navigation" class="rtp-nav-wrapper' . apply_filters( 'rtp_mobile_nav_support', ' rtp-mobile-nav' ) . '">';
//rtp_hook_begin_primary_menu(); // we better disable this hook as it intends for the header primary menu
/* Call wp_nav_menu() for Wordpress Navigaton with fallback wp_list_pages() if menu not set in admin panel */
if ( function_exists( 'wp_nav_menu' ) ) {
wp_nav_menu( array( 'container' => '', 'menu_class' => 'menu rtp-nav-container clearfix', 'menu_id' => 'rtp-nav-menu', 'theme_location' => 'footer', 'depth' => apply_filters( 'rtp_nav_menu_depth', 4 ) ) );
} else {
echo '<ul id="rtp-nav-menu" class="menu rtp-nav-container clearfix">';
wp_list_pages( array( 'title_li' => '', 'sort_column' => 'menu_order', 'number' => '5', 'depth' => apply_filters( 'rtp_nav_menu_depth', 4 ) ) );
echo '</ul>';
}
//rtp_hook_end_primary_menu(); // we better disable this hook as it intends for the header primary menu
echo '</nav>';
Notice the theme_location parameter in wp_nav_menu() function? This is the one which plays behind to get you menu correctly.
Now play around with CSS and you might get what you intend.
Hope this might help you.

New Wordpress theme is not showing widgets in admin area

I am creating new Wordpress theme. It's working but not showing widgets bar in admin panel.
Here is my code:
<?php get_header(); ?>
<div class="wrapper">
<!--Navigation start-->
<div class="navigation_content">
<nav>
<ul>
<?php $args = array(
'depth' => 0,
'sort_column' => 'menu_order, post_title',
'menu_class' => 'menu',
'include' => '',
'exclude' => '',
'echo' => true,
'show_home' => true,
'link_before' => '',
'link_after' => '' );
?>
<li class=""><?php wp_page_menu( $args ); ?></li>
</ul>
</nav>
</div>
<!--Navigation start-->
<!-- body content start-->
<div class="body_content">
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<!--end post header-->
<div class="entry clear">
<?php if ( function_exists( 'add_theme_support' ) ) the_post_thumbnail(); ?>
<p><?php the_content(); ?></p>
<?php //edit_post_link(); ?>
<?php wp_link_pages(); ?>
</div><!--end entry-->
</div><!--end post-->
<?php endwhile; /* rewind or continue if all posts have been fetched */ ?>
<?php else : ?>
<?php endif; ?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
And here is my function file code to register widgets:
function ccp_widgets_init() {
register_sidebar( array(
'name' => __( 'Main Widget Area', 'ccp' ),
'id' => 'sidebar-1',
'description' => __( 'Appears in the footer section of the site.', 'ccp' ),
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
Am i missing code?
Thanks
Add the following code in functions.php after your ccp_widgets_init function.
add_action( 'widgets_init', 'ccp_widgets_init' );
There is another way.
Definitely use a child theme
In your child-theme folder:
Create a folder called widgets
Put your widget in there
ie.
wp-content/themes/my-child-theme/widgets/my_widget.php
at the end of your widget there is no need to 'hook' it into any action like this as most posts say
function register__my_widget() {
register_widget( 'my_widget_class_name_exends_WP_Widget_class' );
}
add_action( 'widgets_init', 'register__my_widget' );
So just register it as normal at the end with a single line:-
register_widget( 'my_widget_class_name_exends_WP_Widget_class' ) see below:
<?php function get_recent_post( $beforewidget, $afterwidget, $beforetitle, $aftertitle ){ ?>
<?php echo $beforewidget; ?>
<?php echo $beforetitle; ?>Recent Posts<?php echo $aftertitle; ?>
<ul class="rp-widget">
<?php query_posts( array ( 'category_name' => 'blog', 'posts_per_page' => -1 ) );
while ( have_posts() ) : the_post(); ?>
<li>....
....very boring widget code, yawn...
...instance ) {
// outputs the content of the widget
get_recent_post( $args['before_widget'], $args['after_widget'], $args['before_title'], $args['after_title'] );
}
}
register_widget( 'my_widget_class_name_exends_WP_Widget_class' );
The important bit is the last line - the "register_widget" bit.
Nb. all widgets extend WP_widget class (I think it's a class - would be in java/c++) so you register your class name
3.Then in your child-themes's functions.php add this line
get_template_part('widgets/my_widget');
and you should be groovy!
Nb:
The line added to your child-theme functions.php should be added outside of any function
The path should exist ie. widgets/
It should be the name of your file MINUS the .php part
(It is not essential to use a separate folder but it helps keep code organised and so if you have added a widgets folder in your child theme and put my_widget.php in there then point is valid.
IF you didn't add a widgets folder to your child-theme folder then you would just use
get_template_part('my_widget');
in your child functions.php
)
What's the advantage of doing it this way??
You can separate your code to be more modular
You don't end up having a monster sized functions.php file
It should be easier to maintain or modify

Categories