Showing Category Count on Html Links - php

I have html links on my wordpress website sidebar and I would like to show the number of posts related to that link.
The posts are placed under custom listing category defined by the following function:
Php:
function va_cat_menu_drop_down( $location = 'menu', $taxonomy ) {
global $va_options;
$key = 'categories_' . $location;
$options = $va_options->$key;
$args['menu_cols'] = ( $location == 'menu' ? 3 : 2 );
$args['menu_depth'] = $options['depth'];
$args['menu_sub_num'] = $options['sub_num'];
$args['cat_parent_count'] = $options['count'];
$args['cat_child_count'] = $options['count'];
$args['cat_hide_empty'] = $options['hide_empty'];
$args['cat_nocatstext'] = true;
$args['cat_order'] = 'ASC';
$args['taxonomy'] = $taxonomy;
$terms_args['pad_counts'] = false;
$terms_args['app_pad_counts'] = true;
return va_categories_list($args, $terms_args);
}
How can I show that number of posts in a particular category next to my Html link as the following does not work (I guess as the listing category is probably custom taxonomy?)
<?php echo get_category(17)->count; ?>

There is a very simple method to do this. Use wp_list_categories and enter the argument 'show_count' => 1
http://codex.wordpress.org/Template_Tags/wp_list_categories

Related

Joomla module get current article title

I'm trying to build a module that should show the article title of the current article viewed.
I've started from this code in my default.php layout that shows the title of the page, but I need to edit it so that it shows the article title and not the page title.
$heading = $document->getTitle();
How should I edit it to get the article title instead of the page title?
If this is in the normal article view, the following should work:
$input = JFactory::getApplication()->input;
$id = $input->getInt('id', 0);
if(
$id > 0
&& $input->getString('option') == 'com_content'
&& $input->getString('view') == 'article'
) {
$c = JTable::getInstance('content');
$c->load($id);
echo $c->title;
}
I've solved this way:
$option = JRequest::getCmd('option');
$view = JRequest::getCmd('view');
if ($option=="com_content" && $view=="article") {
$ids = explode(':',JRequest::getString('id'));
$article_id = $ids[0];
$article =& JTable::getInstance("content");
$article->load($article_id);
$heading = $article->get("title");
}
Not sure it's the correct/best solution, but it seems work at the moment.
Any suggestion, comment about this code or better implementation?

PHP widget use class selector instead of <tag> to build menu

I hope on of you clever chaps might be able to answer this riddle.
I have an excellent widget I am trying to integrate into my site. It creates menus from <H1>, <H2> etc, tags in the document. This is very useful as I can create sub menu navigation for each of my pages by styling the titles.
I am however using a wordpress theme that has extensions added to the editor that style various parts of the pages, including for example titles.
So the output from how it styles the title in HTML output is as follows for example:
<h1 class="grve-element grve-align-left grve-title-striped" style="">Visit</h1>
The PHP script I will load in below, the relevant section it appears to grab tags is:
$tags_to_parse = "<h1>";
Is there a way I can tell this PHP script to grab this particular H1 class?
Cheers,
Tomek
<?php
/*
Plugin Name: Anchors Menu
Description: Check Wordpress static pages content and create a widget menu with links that point to words between the HTML tags you chose.
Author: Gonçalo Rodrigues
Version: 1.2
Author URI: http://www.goncalorodrigues.com
*/
/* Copyright 2010 Gonçalo Rodrigues (email : gonafr [AT] gmail [DOT] com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
//ERROR_REPORTING(E_ALL);
$count = -1;
add_action("init", "anc_insert_init");
add_filter("the_content", "anc_add_link_text");
// renders the list in your theme;
function anc_insert()
{
$tags_to_parse = "<h1>";
$current_page_id = get_the_ID();
$page_data = get_page($current_page_id);
$current_page_name = $page_data->post_name;
$current_page_cat_id = get_cat_ID($current_page_name);
$page_id = get_the_ID();
// if it's blog style page
if (is_home()){
$count = 0;
$content = "";
if ( have_posts() ) : while ( have_posts() ) : the_post();
$id_post = get_the_ID();
$post = get_post($id_post);
$content .= "<a name=\"$count\"></a>";
$content .= $post->post_content;
$count++;
endwhile; else:
//_e("Sorry, no posts matched your criteria.");
endif;
anc_list_menu($content);
//echo "Sorry but this plugin only work on Wordpress pages.";
}
//if it's page style page
else if(is_page()){
$page_id = get_the_ID();
$page_data = get_page( $page_id );
//$title = $page_data->post_title; // Get title
$content = $page_data->post_content;
//if content is empty i will fetch all pages that are child of the current page and get their titles
$fetch_children_pages = true;
if (fetch_children_pages == true && $content==""){
$content = "";
$pages = get_pages('child_of='.$page_id.'&sort_column=post_date&sort_order=desc');
foreach($pages as $page)
{
$content .= "<".$tags_to_parse.">".$page->post_title."</".$tags_to_parse.">";
if(!$content)
continue;
$count++;
}
}
anc_list_menu($content);
}
// if it's single post
else if (is_single()){
$content = "";
$content .= get_the_content();
anc_list_menu($content);
//echo "Sorry but this plugin only work on Wordpress pages.";
}
//if it's a category page
else if(is_category){
//echo $current_page_cat_id;
//echo $current_page_name;
//echo $current_page_id;
$current_cat = get_the_category();
//echo 'teste'.$current_cat[0]->cat_name;
$current_cat_id = get_cat_ID($current_cat[0]->cat_name);
$posts = get_posts('$category_name='.$current_cat[0]->cat_name);
$content = "";
if ( have_posts() ) : while ( have_posts() ) : the_post();
$id_post = get_the_ID();
$post = get_post($id_post);
$content .= "<".$tags_to_parse.">".$post->post_title."</".$tags_to_parse.">";
endwhile; else:
//_e("Sorry, no posts matched your criteria.");
endif;
anc_list_menu($content);
//echo "Sorry but this plugin only work on Wordpress pages.";
}
else {
//_e("Error: This page is not a tradicional Wordpress Page or a Wordpress Blog!");
}
}
// prints the menu with the links to the titles
function anc_list_menu($content){
// list all tags
$foo_tags = anc_get_tags($content);
if($foo_tags[1] != 0){
$foo = -1;
echo "<ul>";
foreach($foo_tags[0] as $key => $val){
$foo++;
echo "<li>".$val."</li>";
}
echo "</ul>";
}else{
//no tags found
//_e("Not found any tag of the type that was selected to be parsed.");
}
}
// retrieve all words between tags
function anc_get_tags($content){
global $tags_to_parse;
$options = get_option("anc_tags");
$tags_to_parse = $options["anc_tags"];
$pattern_search = "/(<".$tags_to_parse.".*>)(.*)(<\/".$tags_to_parse.">)/isxmU";
preg_match_all($pattern_search, $content, $patterns);
$res = array();
array_push($res, $patterns[2]);
array_push($res, count($patterns[2]));
return $res;
}
// insert widget
function anc_insert_init()
{
//register the widget
register_sidebar_widget("Anchors Menu", "anc_widget_insert");
//register the widget control
register_widget_control("Anchors Menu", "anc_widget_insert_control");
}
function anc_widget_insert($args) {
global $title, $tags_to_parse;
extract($args);
//get our options
$options = get_option("anc_title");
$title = $options["anc_title"];
$options = get_option("anc_tags");
$tags_to_parse = $options["anc_tags"];
echo $before_widget;
/*Insert any headers you want in the next line, between "?>" and "<?". Leave blank for no header. */
echo $before_title . $title . $after_title;
anc_insert();
echo $after_widget;
}
// responsable for options in backoffice
function anc_widget_insert_control() {
global $title, $tags_to_parse;
//get saved options if user change things
//handle user input
if (isset($_POST["anc_insert_submit"])){
$foo_title = strip_tags(stripslashes($_POST["anc_title"]));
$foo_tags = strtolower(stripslashes($_POST["anc_tags"]));
$options["anc_title"] = $foo_title;
$options["anc_tags"] = $foo_tags;
update_option("anc_title", $options);
update_option("anc_tags", $options);
}
else {
//default options
$options["anc_title"] = "Menu";
$options["anc_tags"] = "h2";
update_option("anc_title", $options);
update_option("anc_tags", $options);
}
//get our options
$options = get_option("anc_title");
$title = $options["anc_title"];
$options = get_option("anc_tags");
$tags_to_parse = $options["anc_tags"];
//print the widget control
include("anc-insert-widget-control.php");
}
// adds anchors to content tags
function anc_add_link_text($content){
global $tags_to_parse, $count;
$options = get_option("anc_tags");
$tags_to_parse = $options["anc_tags"];
$pattern_search = array();
$pattern_search = "/(<".$tags_to_parse.".*>)(.*)(<\/".$tags_to_parse.">)/isxmU";
return preg_replace_callback($pattern_search, "anc_replacement", $content, -1);
}
// aux funtion to add_link_text
function anc_replacement($matches){
global $tags_to_parse, $count;
$count++;
return "<a name=\"".$count."\"></a>"."<".$tags_to_parse.">".$matches[2]."</".$tags_to_parse.">";
}
?>
Replace your $pattern_search variable with this:
$pattern_search = "/(<h1 .*(".$tags_to_parse.")+ .*>)+(.*)+(<\/h1>)/isxmU";
Now you can pass class to your function:
$tags_to_parse = "grve-element";
Update
$tags_to_parse = "grve-element";
$content = '<h1 class="grve-element grve-align-left grve-title-striped" style="">Visit</h1>sf efew <h1 grve-element>rffef</h1>';
$pattern_search = "/\<h1 .(".$tags_to_parse.")?[^\>]*\>(.*?)\<\/h1\>/";
preg_match_all($pattern_search, $content, $patterns);
print_r($patterns[2]);
Output
Array ( [0] => Visit [1] => rffef )

WordPress theme is eliminating my PayPal purchase link's ampersand and all text afterwards

I've encountered a strange error in a theme called "Faith" (by Chimpstudio). When user publishes or updates the page, the PayPal link doesn't write to the database properly.
As far as I'm aware, the value being written to the database is contained in an XML array, like so:
noSimpleXMLElement Object ( [0] => https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick )
The link that should be publishing/updating (before hitting Publish/Update) is:
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=XXXXXXXXXXXXX
The "&hosted_button_id=XXXXXXXXXXXXX" is not being saved, which means users cannot purchase my client's product.
Potential solution: I've been digging through the theme's .php files, looking for an opportunity to intercept the value being passed to the WordPress database. The idea was to wrap/encode the variable before it gets send to the WP database, with the hope that the entire URL string remains unchanged at the "&hosted_button_id=XXXXXXXXXXXXX" part of the URL.
Any solutions or ideas which may help?
EDIT: This may be a relevant piece of code from the admin_functions.php file:
function events_meta_save($post_id) {
global $wpdb;
if (empty($_POST["event_ticket_price"])){ $_POST["event_ticket_price"] = "";}
if (empty($_POST["event_social_sharing"])){ $_POST["event_social_sharing"] = "";}
if (empty($_POST["event_buy_now"])){ $_POST["event_buy_now"] = "";}
if (empty($_POST["event_phone_no"])){ $_POST["event_phone_no"] = "";}
if (empty($_POST["switch_footer_widgets"])){ $_POST["switch_footer_widgets"] = "";}
if (empty($_POST["event_start_time"])){ $_POST["event_start_time"] = "";}
if (empty($_POST["event_end_time"])){ $_POST["event_end_time"] = "";}
if (empty($_POST["event_all_day"])){ $_POST["event_all_day"] = "";}
if (empty($_POST["event_address"])){ $_POST["event_address"] = "";}
if (empty($_POST["event_ticket_options"])){ $_POST["event_ticket_options"] = "";}
if (empty($_POST["event_map"])){ $_POST["event_map"] = "";}
$sxe = new SimpleXMLElement("<event></event>");
$sxe->addChild('event_ticket_price', $_POST['event_ticket_price'] );
$sxe->addChild('event_social_sharing', $_POST["event_social_sharing"]);
$sxe->addChild('event_buy_now', $_POST["event_buy_now"]);
$sxe->addChild('event_phone_no', $_POST["event_phone_no"]);
$sxe->addChild('switch_footer_widgets', $_POST["switch_footer_widgets"]);
$sxe->addChild('event_start_time', $_POST["event_start_time"]);
$sxe->addChild('event_end_time', $_POST["event_end_time"]);
$sxe->addChild('event_all_day', $_POST["event_all_day"]);
$sxe->addChild('event_ticket_options', $_POST["event_ticket_options"]);
$sxe->addChild('event_address', $_POST["event_address"]);
$sxe->addChild('event_map', $_POST["event_map"]);
echo "<pre>BPOST: ".print_r($_POST, true)."</pre>";
print_r($sxe);
$sxe = save_layout_xml($sxe);
print_r($sxe);
update_post_meta($post_id, 'cs_event_meta', $sxe->asXML());
}
Additionally, here's more code for the XML side from the events.php file:
// event custom fields start
add_action( 'add_meta_boxes', 'cs_event_meta' );
function cs_event_meta()
{
add_meta_box( 'event_meta', 'Event Options', 'cs_event_meta_data', 'events', 'normal', 'high' );
}
function cs_event_meta_data($post) {
$cs_event_meta = get_post_meta($post->ID, "cs_event_meta", true);
global $cs_xmlObject;
if ( $cs_event_meta <> "" ) {
$cs_xmlObject = new SimpleXMLElement($cs_event_meta);
$event_ticket_price = $cs_xmlObject->event_ticket_price;
$event_social_sharing = $cs_xmlObject->event_social_sharing;
$event_start_time = $cs_xmlObject->event_start_time;
$event_end_time = $cs_xmlObject->event_end_time;
$event_all_day = $cs_xmlObject->event_all_day;
$event_address = $cs_xmlObject->event_address;
$event_loc_lat = $cs_xmlObject->event_loc_lat;
$event_loc_long = $cs_xmlObject->event_loc_long;
$event_loc_zoom = $cs_xmlObject->event_loc_zoom;
$testVar = $cs_xmlObject->event_buy_now;
if (strstr($cs_xmlObject->event_buy_now, "&"))
{
echo "yes"; //$event_buy_now = ($cs_xmlObject->event_buy_now . 'FINDME(yes)');
print_r ($testVar);
}
else
{
echo "no"; //$event_buy_now = ($cs_xmlObject->event_buy_now . 'FINDME(no)');
print_r ($testVar);
print_r ($cs_xmlObject);
}
$event_ticket_options = $cs_xmlObject->event_ticket_options;
$event_map = $cs_xmlObject->event_map;
}
else {
$event_ticket_price = '';
$slider_id = '';
$event_social_sharing = '';
$event_related = '';
$event_start_time = '';
$event_end_time = '';
$event_all_day = '';
$event_address = '';
$event_loc_lat = '';
$event_loc_long = '';
$event_loc_zoom = '';
$inside_event_related_post_title = '';
$event_map = '';
$event_buy_now = '';
$event_ticket_options = '';
}
NOTE: Both of these code snippets are located in the parent theme's /include/ directory. At this point, there is no child theme -- and if necessary, a child theme will be created to implement these updates.
Just use jQuery and .change() to detect new content in an input. When the field gets changed, have it change the & to & amp; Might be a dirty hack, but if this is standing between you and payday, have at 'er.

Get specific attribute on magento category view.phtml

I am trying to create a Asos style header on my Magento category page.
In this box I have pulled in the category title and the category description, I am also some how needing to pull in a specific attribute from the layered navigation into the category view.phtml page.
At the moment I have
<?php $prod = Mage::getModel('catalog/product')->load($productId);
$att = $prod->getResource()->getAttribute('product')->getFrontend()->getValue($prod);
echo $att;
?>
But it is just pulling in the word No instead of the attributes that it is showing in the layered navigation for this specific category.
Try this:
Mage::getResourceModel('catalog/product')->getAttributeRawValue($productId, 'attribute_code', $storeId);
Thanks to #Daniel Kocherga for the original answer here.
From code/core/mage/catalog/block/product/view/attributes.php
public function getAdditionalData(array $excludeAttr = array())
{
$data = array();
$product = $this->getProduct();
$attributes = $product->getAttributes();
foreach ($attributes as $attribute) {
// if ($attribute->getIsVisibleOnFront() && $attribute->getIsUserDefined() && !in_array($attribute->getAttributeCode(), $excludeAttr)) {
if ($attribute->getIsVisibleOnFront() && !in_array($attribute->getAttributeCode(), $excludeAttr)) {
$value = $attribute->getFrontend()->getValue($product);
if (!$product->hasData($attribute->getAttributeCode())) {
$value = Mage::helper('catalog')->__('N/A');
} elseif ((string)$value == '') {
$value = Mage::helper('catalog')->__('No');
} elseif ($attribute->getFrontendInput() == 'price' && is_string($value)) {
$value = Mage::app()->getStore()->convertPrice($value, true);
}
if (is_string($value) && strlen($value)) {
$data[$attribute->getAttributeCode()] = array(
'label' => $attribute->getStoreLabel(),
'value' => $value,
'code' => $attribute->getAttributeCode()
);
}
}
}
return $data;
}
}
Pretty sure this is the responsible section for displaying a 'No' or 'N/A' with your attributes
I am not sure but see below URL. I think it is help full to you.
How to add attributes to product grid in category
http://www.magentocommerce.com/wiki/4_-_themes_and_template_customization/catalog/add-attributes-to-product-grid
Magento Attributes: Using Different Filterable Attributes Per Category
http://www.human-element.com/Blog/ArticleDetailsPage/tabid/91/ArticleID/68/Magento-Attributes-Using-Different-Filterable-Attributes-Per-Category.aspx
Get Data for Magento Category Attribute on Product Page
http://spenserbaldwin.com/magento/get-data-for-new-magento-category-attribute

Make Search and Archives match homepage

I have my homepage http://www.faberunashop.com set up as a directory. When you click on the post image, it takes you over to the artists site. Here is the code that I used to make this happen by adding it to the functions.php:
function print_post_title() {
global $post;
$thePostID = $post->ID;
$post_id = get_post($thePostID);
$title = $post_id->post_title;
$perm = get_permalink($post_id);
$post_keys = array(); $post_val = array();
$post_keys = get_post_custom_keys($thePostID);
if (!empty($post_keys)) {
foreach ($post_keys as $pkey) {
if ($pkey=='url1' || $pkey=='title_url' || $pkey=='url_title') {
$post_val = get_post_custom_values($pkey);
}
}
if (empty($post_val)) {
$link = $perm;
} else {
$link = $post_val[0];
}
} else {
$link = $perm;
}
echo '<h2>'.$title.'</h2>';
}
Now I want to do the same to my search and archive page. What do I adjust to make them behave the same?
I suppose that you use WordPress.
In that case you can change the layout and the behavior of your search results by creating a file with name search.php into your theme for your search results, and another file for archives that called archives.php.
For more information about Template Hierarchy for WordPress you can find here http://codex.wordpress.org/Template_Hierarchy

Categories