I have the code below which I'm using to show categories only.
The problem I'm facing is it shows categories list on all pages.
I want to use this piece of code on front page or home page only.
How can I show categories list on front page/home page only?
I only have knowledge of html and css, so any help would be appreciated.
Thanks and Regards.
add_action( 'genesis_entry_content', 'list_categories' );
function list_categories() {
echo '<div class="archive-description">';
wp_list_categories(
array(
'title_li' => '')
);
echo '</div>';
}
You have to test if page is home or front page:
add_action( 'genesis_entry_content', 'list_categories' );
function list_categories() {
if ( is_front_page() || is_home() ) {
echo '<div class="archive-description">';
wp_list_categories(['title_li' => '']);
echo '</div>';
}
}
Related
I am a junior dev and also new to stackoverflow. I am working on a plugin and would like to display different tabs on the admin page according to the logged in user (mainly admin and shop manager). My current implementation displays the different tabs as required but there is a problem. Some tabs are displaying empty pages, while others are working correctly. What might be wrong with my code? Thanks as you help me.
class my_plugin_admin{
public $plugin_tabs = array();
public function __construct() {
add_action('admin_init', array( $this, 'show_different_tabs' ));
}
public function show_different_tabs(){
if(current_user_can('administrator')){
$this->plugin_tabs = array(
'tabname1'=> __('Tabname1' , 'woocommerce-myplugin'),
'tabname2'=> __('Tabname2' , 'woocommerce-myplugin'),
'tabname3'=> __('Tabname3' , 'woocommerce-myplugin'),
'tabname4'=> __('Tabname4' , 'woocommerce-myplugin')
);
}elseif(current_user_can('manage_woocommerce')){
$this->plugin_tabs = array(
'tabname1'=> __('Tabname1' , 'woocommerce-myplugin'),
'tabname2'=> __('Tabname2' , 'woocommerce-myplugin')
);
}else{
$this->plugin_tabs = array(
'tabname1'=> __('Tabname1' , 'woocommerce-myplugin'),
'tabname2'=> __('Tabname2' , 'woocommerce-myplugin')
);
}
}
}
There is frontend and backend for this plugin. There are tabs that are interacting with db, one exports file to .csv(this one lis loading infinitely), one shows logs(also this loads infinitely), one loads orders e.t.c(this isn't loading orders but appears to display page, i.e buttons,inputs etc). Also on the frontend there is a foreach that loops threough the tabs like so ...
<?php foreach ($plugin_tabs as $plugin_tab => $plugin_tab_translated) {?> <a class="nav-tab <?php echo $active_tab == $$plugin_tab ? 'nav-tab-active' : '' ?>" href="<?php echo admin_url( 'admin.php?page=' . $menu_slug . '&tab=' . $plugin_tab ) ?>"><?php echo $plugin_tab _translated ?></a> <?php } ?>
I develop my first plugin, which creates some pages programatically by this method:
$page_id = wp_insert_post(
array(
'comment_status' => 'close',
'ping_status' => 'close',
'post_author' => 1,
'post_title' => 'HSN Form',
'post_name' => strtolower(str_replace(' ', '-', trim('hsn-form'))),
'post_status' => 'publish',
'post_type' => 'page',
)
);
And I set template file for it:
add_filter( 'page_template', 'hsn_service_form_page_template', 10, 1 );
function hsn_service_form_page_template( $page_template ){
if ( is_page( 'hsn-form' ) ) {
$page_template = plugin_dir_path(__DIR__) . 'service-form/form-template.php';
}
return $page_template;
}
After I would like to hide it completely from wordpress dashboard, but those have to available following way:
wwww.example.com/hsn-form.
I can hide it following code from Pages menu:
add_filter( 'parse_query', 'ts_hide_pages_in_wp_admin' );
function ts_hide_pages_in_wp_admin($query) {
global $pagenow,$post_type;
$page = get_page_by_path('hsn-form');
if (is_admin() && $pagenow=='edit.php' && $post_type =='page') {
$query->query_vars['post__not_in'] = array($page->ID);
}
}
It's ok, but it's still available in Appereance->Menu, where you can create nav menus.
I searched for it, but I can't find complete solution for my problem:
My plugin has to create some pages which are available this way: www.example.com/hsn-form
I need to hide this pages completely from admin dashboard
I would like to apply templates for these pages to add some custom php code.
So If somebody should know a complete solution for it, that should be great.
Thank you in advance!
change parse_query to pre_get_posts
In order to remove a page from the admin nav menu, you can hook into the admin_menu action, then manipulate the global $submenu variable.
add_action('admin_menu', function(){
global $submenu;
array_walk($submenu, function(&$child, $parent){
if( $parent != 'edit.php' )
return;
foreach($child as $key=>$submenu){
if( $submenu[2] == 'post-new.php' ) {
unset($child[$key]);
break;
}
}
});
});
In that example, we are looking for a subpage with the slug of post-new.php
underneath the top level page with the slug edit.php. If found, it gets altogether removed from the navigation menu.
The $submenu[2] part is looking for the slug element in the array. If you want to match by the submenu name instead, you can replace it with $submenu[0].
I am using the Genesis framework and need to load a custom footer when a certain page template loads, but I am having issues passing the page template file name into the function. I cannot use the slug because the template can be used for multiple pages, so I really need to use the template file name instead. I believe I'm on the right path with what I have so far, but I don't quite understand what I'm reading about get_query_var and how it can help pass the value into my function.
So far my code is as follows:
remove_action( 'genesis_footer', 'genesis_do_footer' );
if (is_page_template( 'page-alternate.php' )) {
add_action( 'genesis_footer', 'do_alternate_footer' );
} else {
add_action( 'genesis_footer', 'do_main_footer' );
}
function do_alternate_footer() {
echo 'xxx';
}
function do_main_footer() {
echo 'yyy';
}
Any assistance would be greatly appreciated. Thank you
The "Is Page Template" function actually looks for the template page name not the file so in this example if the page template name was "alternate" you would have:
remove_action( 'genesis_footer', 'genesis_do_footer' );
$CurrPageTemplate = get_page_template_slug( get_queried_object_id() );
if ($CurrPageTemplate == 'page-alternate.php' ) {
add_action( 'genesis_footer', 'do_alternate_footer' );
} else {
add_action( 'genesis_footer', 'do_main_footer' );
}
function do_alternate_footer() {
echo 'xxx';
}
function do_main_footer() {
echo 'yyy';
}
See how you go with that.
I am trying to use a custom menu on my landingpage.
I used this code:
<?php
function change_wp_nav_menu_args($args = ''){
$pageID = get_the_id();
if($pageID == '63') //custom menu for site with id 63
{
$args['menu'] = 'homepage';
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'change_wp_nav_menu_args' );
?>
It works fine, but not only the main menu changes.
The footer menu changes also.
But the footer menu should be the same on every page.
How can I affect this?
This code fixed my problem:
<?php
add_filter( 'wp_nav_menu_args', 'bb_wp_nav_menu_args' );
function bb_wp_nav_menu_args( $args = '' ) {
// change the menu in the Header menu position
if( $args['theme_location'] == 'primary' && is_page('63') ) {
$args['menu'] = '6'; // 32 is the ID of the menu we want to use here
}
return $args;
}
?>
Its great that you find your answer, but it's a suggestion to make the code dynamic rather set the menu value as static [ $args['menu']='6' ].
Suggestion :
Create a Meta Box [ Dropdown with list of menu ] for Page with the Label Menu. And use the Menu Id for the wp_nav_menu .
For Dropdown [ listing Menus ]
function your_menus()
{
$menu_arr=NULL;
$menus=get_terms( 'nav_menu', array( 'hide_empty' => true ) );
$menu_arr['your-nomenu']='Default';
foreach ( $menus as $menu ){
$menu_arr[$menu->slug]=$menu->name;
}
return $menu_arr;
}//end of function
For Nav Menus : [ _your_page_menu : meta name ]. You can place the code inside a function and called it in Header or you can place this code directly into the header.
$page_menu_name=get_post_meta(get_the_ID(),'_your_page_menu',true)==''?'your-nomenu':get_post_meta(get_the_ID(),'_your_page_menu',true);
if($page_menu_name==='your-nomenu')
{
wp_nav_menu(array('theme_location' => 'primary','menu_id'=> 'main-menu','container'=>false,'fallback_cb'=>'','menu_class'=>'main-navigation'));
}
else
{
wp_nav_menu(array('menu_id'=> 'main-menu' , 'container'=>false, 'menu'=>$page_
menu_name,'fallback_cb'=>'','menu_class'=>'main-navigation'));
}
Hope it helps you.
I have wordpress flash game website. Into the admin panel, where I add or edit post I have field named Swf URL: where I add SWF link and it links to the post where it shows flash game. I want to do same but not in the post, I want to use this function (adding game with the help of swf) to home page.
How it is now:
How I want to see:
functions code:
load_theme_textdomain( "freebabyhazelgames", TEMPLATEPATH."/languages" );
$locale = get_locale( );
$locale_file = TEMPLATEPATH."/languages/{$locale}.php";
if ( is_readable( $locale_file ) )
{
require_once( $locale_file );
}
$sp_boxes = array( "Game Details" => array( array( "thumb", "Game image URL:" ), array( "game", "Swf URL:" ) ) );
add_action( "admin_menu", "sp_add_custom_box" );
add_action( "save_post", "sp_save_postdata", 1, 2 );
if ( !function_exists( "get_custom_field" ) )
{
function get_custom_field( $field )
{
global $post;
$custom_field = get_post_meta( $post->ID, $field, true );
echo $custom_field;
}
}
post code:
<div id="playgame">
<script type="text/javascript">
<!--
swf("<?php $values = get_post_custom_values("game"); echo $values[0]; ?>", "701", "550");//-->
</script>
</div>
By the way I tried to change dirrectory or to copy single.php to index.php but both not worked.
If I understand you right, you want the game to appear on the main page without entering a post? In that case, I think you can try using a static frontpage instead of latest posts.
You can find it on Appearance -> Customize, and then choose the frontpage to be static. That way you can have your game on the static frontpage.