Menu highlight based on url pattern - php

I have a sidebar menu that is created if a specific tag is found in a category. For example
Apples
Oranges
When clicked, the URL pattern would be as below:
/category/?tag=apples
The code that renders the menu is as below:
<?php if ( $term->slug == 'apples' ) {?>
<section class="item">
<h4><a href="<?php $category_id = get_query_var('cat');
echo get_category_link( $category_id );?>?tag=apples"> Apples</a>
</h4></section>
<?php } if ( $term->slug == 'oranges') {?>
<section class="item">
<h4><a href="<?php $category_id = get_query_var('cat');
echo get_category_link( $category_id );?>?tag=oranges"> Oranges</a>
</h4></section>
<? } ?>
<?php } } ?>
I'm having a difficult time to highlight "Apples" with a red background when that is the active URL pattern, and the same with Oranges having an orange background.
I have tried various Jquery fiddle examples that works perfectly on fiddle, but not when i implement them. Most are for static html pages which is easier to do.

This is probably something you should put in a loop, but an example for your apples section:
<section class="item <?php echo ($term->slug === $_GET('tag')) ? 'active' : '';">
<h4><a href="<?php $category_id = get_query_var('cat');
echo get_category_link( $category_id );?>?tag=apples"> Apples</a>
</h4></section>
Note that I have added an active class to the section element so that you can style that (or its children) differently.

Why not verify the $_GET variable and then highlight the active URL ?
if ($_GET['tag'] == 'apples')
echo ''; //Highlight : ON, with CSS
else
echo ''; //Highlight : OFF
Same goes for "oranges".

Since you tagged it with jQuery, you can solve it on the client side:
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" :
decodeURIComponent(results[1].replace(/\+/g, " "));
}
$(function(){
var tag = getParameterByName('tag');
if(tag){
$('h4').filter(function(){
return $(this).text().toLowerCase() === tag;
}).css({ color: 'red' });
}
});
Allthough I suggest you solve it directly on the server using PHP. Just an alternative.

Related

How to limit the number of wordpress search results from each category on each result pagel

I am using the code below to show search results for different post types separately/segmented form.
if (have_posts()) {
// In the below line of code you can set the loop order in array (1, 2, 3, 4)
// You can also turn a post type off by removing it from the below line.
$types = array('post', 'page', 'sfwd-courses', 'product');
foreach ($types as $type) {
// Here you can customize both the titles, section heading title & side title
if ($type == 'post') {
$head_title = "Results in Posts";
$side_title = "post";
}
if ($type == 'page') {
$head_title = "Results in Pages";
$side_title = "page";
}
if ($type == 'sfwd-courses') {
$head_title = "Results in Courses";
$side_title = "course";
}
if ($type == 'product') {
$head_title = "Results in Products";
$side_title = "product";
}
// This div below shows up the head title for each section
echo '<div class="search-section"><h4>' . $head_title . '</h4></div>';
while (have_posts()) {
the_post();
if ($type == get_post_type()) {
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(''); ?> role="article">
<header class="article-header">
<div class="article-header-content">
<h3 class="title head-2"><?php the_title(); ?></h3>
<!-- This div below shows up the side title for each section -->
<div class="posttypetext"><i> <?php echo $side_title; ?> </i></div>
</div>
<!-- <?php get_template_part('parts/content', 'byline'); ?> -->
</header> <!-- end article header -->
</article> <!-- end article -->
<?php }
}
rewind_posts();
}
} else {
get_template_part('template-parts/content', 'missing');
}
This shows me a list of search results showing all the posts first and then the results from pages, then courses and finally products.
The pagination limit right now is 20 per page.
I want that on each page I should get 5 search results from post, 5 from page, 5 courses and 5 products.
this is needed because if there are 30-40 posts results, it will cover up the first 2 pages of search results and users will not see any courses or products that will be lying on the 3, 4 page.
I hope it makes sense, is there a way to accomplish that?
Thanks
JS
There are multiple ways to set this up. The simplest way is to take advantage of the current_post property of your query which would give you the index number of the current post.
So in your code you could replace this line:
if( $type == get_post_type()){
// your template section
}
With this line:
global $wp_query;
if( $type == get_post_type() && $wp_query->current_post <= 4){
// your template section
}
It's $wp_query->current_post <= 4 because current_post is zero based and you only need to output the first 5 posts!
There are others ways to achieve the same thing using wp_query, but you haven't provided any details about how you've created your query. You just provided us with your loop template, we don't know how it's been queried and where this loop is located. So I think using current_post property would be simplest way here!

Ghost post_title from my WP LOOP script

The code below is displaying the title in black text before it displays blue title with hyperlink under it.
I only want the link to appear.
if ( $query2->have_posts() ) {
// The 2nd Loop
while ( $query2->have_posts() ) {
$query2->the_post();
if ($post->ID == $do_not_duplicate)
continue;
$permalink = get_the_permalink($query2->post->ID);
$ID = $post->ID;
$titleAtribute = the_title_attribute();
$title = get_the_title();
echo '<h2 id="post-' .$ID.' ">
<a href="'.$permalink.'" rel="bookmark" title="Permanent Link to '.$permalink.' ">
'.$title.'</a></h2>';
}
// Restore original Post Data
wp_reset_postdata();
}
For example, on my website: http://skkelti.cz/, the following text appears in black above the link with the same text:
-Martin Davídek ml. : „Fanoušci jsou vždy to, co vás žene kupředu“-
Where is this coming from and what do I need to do to stop it from appearing?
The problem is with the_title_attribute(). This is displaying the value directly instead of returning it.
The function accepts echo in $args to specify whether to display or return the value. The default value is true (to display it), so pass false to return the value e.g.:
$titleAtribute = the_title_attribute('echo=0');

Change style for php generated links

I'm trying to put together a code to display links of subcategories.
I want the current active category to change style. I've put together the following code but can't seem to get it to work properly.
$object = new Mage_Catalog_Block_Navigation();
$actualCategoryId = $object->getCurrentCategory()->getId();
$actualCategory = Mage::getModel('catalog/category')->load($parentid);
$subCategories = explode(',', $actualCategory->getChildren());
foreach ( $subCategories as $subCategoryId )
{
$category = Mage::getModel('catalog/category')->load($subCategoryId);
if ( $category->getIsActive() )
{
{
echo '<li><a href="'.$category->getURL().'" style="text-decoration: none;'.($magentoCurrentUrl == $category->getURL() ? 'color:#fff;' : '').'" >'.$category->getName().'</a> </li>';
}
}
}
I want the above code to change the link to white if it is active. But it fails to include the color style. However, if I change $magentoCurrentUrl == $category->getURL() to $magentoCurrentUrl = $category->getURL() It will change the color style to #fff but also apply it to all links and not just the active one.
Can anyone point me in the right direction with this?
I think you'll want to use 3 equals signs:
(($magentoCurrentUrl === $category->getURL()) ? 'color:#fff;' : '')
The following test code produces the effect you are looking for. Here is the code I tested:
<!doctype html>
<head>
</head>
<body>
<?php
$var1 = "http://location.php";
$magentoCurrentUrl = "http://location.php";
$categoryName = "Testing";
echo '<li><a href="'.$var1.'" style="text-decoration: none;'.(($magentoCurrentUrl === $var1) ? 'color:#fff;' : '').'" >'.$categoryName.'</a> </li>';
?>
</body>
</html>
And here is the resulting HTML:
<!doctype html>
<head>
</head>
<body>
<li><a href="http://location.php" style="text-decoration: none;color:#fff;" >Testing</a> </li>
</body>
</html>
All I have done is insert my own variables in place of your variables and function calls. That is why I suggested you use var_dump() to make sure the returned values were actually identical for the link you are trying to highlight.

Pass WP post content to a JQuery plugin (Fancybox) - now with link

I've been struggling with this for days... hopefully this will bring some light into it.
I'm building a portfolio WP. It only displays image thumbnails, with a.video elements that call the fancybox. In the tags, the attributes have the information for the fancybox to load the content. The "href=" is the url of the video to show, the "title=" is the title of the post, etc.
I want to somehow bring the content of the post to the fancybox window and display it as the title (caption under the video).
My approach so far has been to bring the content of the post into the <a> tags and then passing that content into the FBox .js file.
I've brought the post content (the_content) to a custom attribute for <a>, eventid= and also have brought it to INSIDE the tags.
I've got that working: the post content are loaded in the index page both as the "eventid=" attribute and as the text inside the <a> tags.
This is how the Fbox calling element is loaded inside the WP loop in the index.
<a class="thumbnail-frame excerpt-text <?php if($video_url !='' || $embeded_code != '') : ?>video<?php else: ?>shots<?php endif; ?>"
<?php if($video_url !='' || $embeded_code != '') : ?><?php else: ?>rel="set-<?php the_ID(); ?>"<?php endif; ?>
href="<?php if($video_url !='' || $embeded_code != '') : ?>#embed-<?php the_ID(); ?><?php else: ?><?php echo $upload_image_1; ?><?php endif; ?>"
title="<?php the_title(); ?>" eventid="<?php the_content(); ?>"><span class="post"><?php the_content(); ?></span></a>
However, I'm still having trouble in sending that text into the FBox javascript plugin so that the content is grabbed from the index and loaded in the Fbox.
The most I've done is
'titleFormat' : function(title, currentArray, currentIndex, currentOpts) {
return '<span id="fancybox-title-over">' + jQuery("a.video").html() + '</span>';
That posts the content of the FIRST post only inside the Fbox (it gets the content of the first a.video element it finds).
I've tried with
'titleFormat' : function(title, currentArray, currentIndex, currentOpts) {
return '<span id="fancybox-title-over">' + jQuery(this).html() + '</span>';
But it returns a null or undefined value and it prints it in the Fbox.
I'm stuck, I don't know how to grab the specific text content for each post and bring it into the FBox. I was thinking maybe using a global variable?
I'm a student so I'm clearly super novice, I just really want to surpass this obstacle.
Here's the link to the blog so you understand what I'm talking about.
http://realitynext.heliohost.org/wordpress/
Thanks!
If you want to get all the a.video elements you need the .each() iterator:
'titleFormat' : function(title, currentArray, currentIndex, currentOpts) {
var string = '<span id="fancybox-title-over">';
jQuery("a.video").each(function() { string = string + $(this).html(); });
return string + '</span>';
}
And if you want only the content of the element that was clicked you need this:
'titleFormat' : function(title, currentArray, currentIndex, currentOpts) {
return '<span id="fancybox-title-over">' + jQuery(currentArray[currentIndex]).html() + '</span>';
}

How do I print a certain wordpress category anywhere I want on any page?

Can someone tell me what the PHP would look like in order to get a category from Wordpress and then print it where ever I want?
I'm guessing I have to build a PHP function. Something like:
function get_a_category() {
$category = get_the_category(); <-----( not sure how to ge ta specific category )
echo $category;
}
I have no idea I know nothing about PHP
can someone help me please ?
You don't have to write your own function; you do have to work with the The Loop (WordPress Codex) and a new query (Function Reference/query posts « WordPress Codex) to keep the original WP loop in place. Run this new query in a page template to get the first post from "mycategory". Change showposts to the number of posts you want.
<?php $my_query = new WP_Query('category_name=mycategory&showposts=1'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<?php the_title(); ?></a>
<?php endwhile; ?>
Try this
function get_a_category() {
$category = get_the_category();
foreach ($category AS $key => $value) {
$category_name[] = $value->cat_name;
}
$categories = implode(', ',$category_name);
echo $categories;
}

Categories