I have a menu that is used throughout the entire site (pages and posts). I would like to change the link of one single menu item (custom link) if the menu is displayed on on a blog post.
I tried using the plugin Conditional Menus which could do the job, but has the disadvantage that I would need to recreate the entire menu for each language and repeat this everytime the menu is updated.
I think it would be smarter to do this through php. I found this to check post vs. page:
<?php
if(get_post_type() === 'post') {
// Do something
}
?>
Would it be possible to set the href of the <a> tag in menu-item-123 within this if-statement? Also, is there a way I can add this to functions.php in the child theme or does it need to placed somewhere else? Thanks!
Please write following codes in your functions.php file Here ".primary-menu" WILL BE the css class of your menu UL, I've setup 3rd child in the menu item, you can change it accordingly.
<?php
add_action( 'wp_footer', 'change_link' );
function change_link(){
if(get_post_type() === 'post') { ?>
<script>
(function($) {
jQuery('.primary-menu li:nth-child(3) a').attr("href", "https://google.com");
})(jQuery);
</script>
<?php }
}
Related
So I have this code in php:
add_filter( 'template_include', 'my_callback' );
function my_callback( $original_template ) {
if ( some_condition() ) {
return SOME_PATH . '/some-custom-file.php';
} else {
return $original_template;
}
}
and I want to re-write this code in order to use it inside an <a> element and specifically in an onclick="" attribute of that <a> element. All in all my main goal is to create 2 template files in my theme's folder where one is for most recent posts and the other is for the most popupal posts, and the use the code above but inside the <a> element to be able to redirect to each one respectively.
Any suggestions? Thanks in advance,
Jameu
Create two wordpress native pages (inside WP Backend), use the template you want (create it) with each one.
Here is the template pages doc:
TEMPLATE FILES
Once you have done it just link them with regular html: LINK PAGE1 or with javascript if you want
I have made a simple shortcode for my wordpress theme. I'm trying to align it on right-side above the corner of my page. Is it possible to insert shortcode function.php?
I want to place my Short-code in the top-bar (right side above the corner) so that it will work for each pages. Means if i click on any menu then the shortcode should have to work on that menu also.
Actually i'm new in web development. Please suggest me what shall i have to do so it will align where i want and should have work for each module.
You can use wordpress function for that. Please open the file on which you have to place shortcode. Like if you want to place it on header open header.php and add
<?php echo do_shortcode('[your_shortcode]'); ?>
where you want your shortcode content to appear
The best way is to find the top bar menu hook of your theme and then add this code (adapted for your needs) in your function.php child theme :
function my_shotcode_inside_top_bar ( $items, $args ) {
if (is_page() && $args->theme_location == 'secondary') {
$items .= '<li class="my-class">' . do_shortcode( "[my_shortcode]" ) . '</li>';
}
return $items;
}
add_filter( 'wp_nav_menu_items', 'my_shotcode_inside_top_bar', 10, 2 );
is_page displays the shortcode in pages only but you can choose another condition or no condition at all.
secondary is the hook and it depends on your theme. You can normally find it in your theme header.php file.
A menu location selector should be added - that way some themes support multiple menus, and some are called "top" or custom added.
The shortcode could then contain a call to whatever a menu position is:
EG: [myshortcode menu="secondary"]
Didn't have enough points to add this as a comment.
If I create custom field in Joomla, it's will show in homepage (featured article) and blog category menu.
I want custom fields to only show in a single article. It should be hidden in the homepage (featured articles) and blog category menu.
Where can I setting it or modify in PHP code?
The files you need to override are at \components\com_content\views\featured\tmpl\default_item.php and \components\com_content\views\category\tmpl\blog_item.php
Important : core files should be overridden in your template, and the core code left unchanged.
The fields themselves are generated by the lines
<?php // Content is generated by content plugin event "onContentBeforeDisplay" ?>
<?php echo $this->item->event->beforeDisplayContent; ?>
Note that removing these lines will also stop any other plugin which uses this area from working. As far as I can tell, the only other thing to use it is the voting system. If you need that, then you may need to write a bespoke plugin or just hide the area with CSS.
You can create the override of Layout com_fileds - field.
(copy the file render.php from components/com_fields/layouts/field/ to templatename/html/layouts/com_fields/field/)
Add two new variables and one more condition on the if condiction
defined('_JEXEC') or die;
$app = JFactory::getApplication();
$view = $app->input->getCmd('view', '');
if (!key_exists('field', $displayData) || $view != 'article')
{
return;
}
I used wp-types toolset to create a custom post type and a post relationship to pages; there is now a Post Relationships section at the bottom of every page edit screen. The problem is, I would only like this section to show up on a couple of pages.
Is there something I can add to functions.php (or another alternative) to hide this section from all page edit screens expect for those particular ones.
The section div id that I want to hide is #wpcf-post-relationship and the data post id of the pages that I would like it to be visible are 143 and 23.
-- (update) --
As admin_init is triggered before any other hook when a user access
the admin area, we finally use instead admin_head because action is
just triggered inside the <head> of the admin page (thanks to John).
The easy way is to use a simple CSS rule with the 'admin_head' hook, to do it, like this:
1) create a css file named hide_some_field.css and put it into your active child theme folder, with this code:
#wpcf-post-relationship {
display:none;
}
2) Add this code in your active child theme functions.php file:
add_action('admin_head', 'ts_hiding_some_fields');
function ts_hiding_some_fields(){
// your 2 pages in this array
$arr = array(23, 143);
if(get_post_type() == 'page' && !in_array(get_the_ID(), $arr))
{
wp_enqueue_style( 'hide_some_field', get_stylesheet_directory_uri().'/hide_some_field.css');
}
}
If you use a theme instead, change:
get_stylesheet_directory_uri() by get_template_directory_uri().
Another similar alternative (without an external CSS file) is:
add_action('admin_head', 'ts_hiding_some_fields');
function ts_hiding_some_fields(){
// your 2 pages in this array
$arr = array(23, 143);
if(get_post_type() == 'page' && !in_array(get_the_ID(), $arr))
{
echo '<style type="text/css">
#wpcf-post-relationship {display: none;}
</style>';
}
}
I have a RocketTheme Affinity template that when it hides the component the wrapper stays untouched and i want it removed also.
I only want to disable it from the home page (id=101).
In the "index.php" the bellow code calls this:
<?php
echo $section_rows->render();
?>
The div name is "section-row3" in a file called "rt_sectionrows.php"
There are also css files called style6 and tempate.css files.
Conclusion:
How do I hide certain div and class from homepage?
Try this,
<?php
$app = JFactory::getApplication();
$menu = $app->getMenu();
if ($menu->getActive() == $menu->getDefault()) {
?>
<script type="text/javascript">jQuery(document).ready(function(){
jQuery('.section-row3').hide()
});</script>
<?php
}
?>
Add this code at the bottom of your home page. It hide that div.
Hope it helps.
You should be able to just do this with some CSS.
Go into the Joomla! Administrator, Menus, Select the menu which contains your homepage menu item and then edit it.
In the menu item, select 'Page Display'. At the bottom you will see: Page Class.
Add a space and type: homep
Then add:
.homep .section-row3 {display: none !important;}
To your Rocket Theme Custom CSS or LESS file.
Then that will just hide it with CSS, we have specified !important, so it takes priority.