Trying to parse rss feed with items that have multiple categories per item. The original file is an atom structured feed which I have parsed using simplexml and outputed certain elements as an rss feed. The multiple categories in the original atom file are stated as attributes to the category element. I'm trying to display items based on any category defined. As it is now simplepie only recognizes the first category. The simplified code is as follows:
<item>
<title>Banana</title>
<category>Fruit</category>
<category>Yellow</category>
</item>
<item>
<title>Apple</title>
<category>Round</category>
<category>Fruit</category>
</item>
// display all titles from items with category 'Fruit'
<?php
foreach ($feed->get_items() as $item):
if(
$item->get_category()->get_label() == 'Fruit'
):
echo $item->get_title();
endforeach;
// result - displays only Banana but not Apple
In the latest version (1.3.1 as of this post), there are two functions in the item class:
public function get_category($key = 0)
and
public function get_categories()
You could either use the first and pass in the key of the category you want, or just use the second function, get all the categories (get_categories documentation) and and use PHP's array_search function.
Related
I am making some modifications in the way that customers explore products in a new page .tpl by overriding category
Is there a function that works but gets products from a defined category using a category ID (rather than the current category) ?
I would like to display products from a specific category
When I put this code, it shows me all the products.
{foreach from=$products item=product name=products}
{$product.name|escape:'htmlall':'UTF-8'}
{/foreach}
I am using PS version 1.7.8.6
Try this into tpl
{php}
$category = new Category($id_category_you_chose);
$products = $category->getProducts($this->context->language->id);
{/php}
see the getProducts arguments, i don't remember exactly what he need.
next_post_link as far as I'm aware, just gets the next post within the taxonomy based on date of creation. I was wondering if I could use custom fields to have it select the next post by the order in there. Like a page number field and it grabs the next post within the taxonomy with the next page number.
So we're in Taxonomy 'x' on page 2 (created 2 weeks ago), and want to move to page 3 (created today), and there's a page 4 (created 1 week ago).
It would go to page 4, but I want it to go to page 3.
Is there a good way to do this?
If you use custom fields like you mentioned, it wouldn't be using a taxonomy. You would be ordering the posts, or getting the posts, based on the get_field() function for acf.
$posts = get_posts();
foreach ($posts as $post) {
// This gets the custom field from ACF
$post->order = get_field('name-of-field', $post->ID);
if ($post->order == whatever_number_you_want) {
$active_post = $post;
}
}
echo $active_post;
I have a post type named 'Property'.
I want to show single post in two different way.
if anyone click on post then it will shows a simple layout with name of post and description.
Now i have also category for beds. now if anyone goes in category '2 Bed' then you can see all post with '2 Bed' categories('its done'). but now if anybody click on post then it have to show different single page.
my English is very bad so please excuse it.
You can set up individual templates for a single category by using the single_template hook.
Put this in your functions.php file:
function my_category_templates($single_template) {
global $post;
if ( in_category( 'property' )) {
$single_template = dirname( __FILE__ ) . '/single-property.php';
}
// Copy the above for your other categories
return $single_template;
}
add_filter( "single_template", "my_category_templates" );
You can then create individual single templates for each category, just add more conditions and point them to the template you create.
There is the concept of Category Templates as dictated by the
Template Hierarchy but because you are asking how to display a "single" post based on category, you will want to use the in_category() Conditional Tag in the Template file you use to display singe posts. The Loop article has an example of using in_category.
Or look at this concept:
http://www.nathanrice.net/blog/wordpress-single-post-templates/
Or this:
http://justintadlock.com/archives/2008/12/06/creating-single-post-templates-in-wordpress
Or this plugin:
http://wordpress.org/extend/plugins/custom-post-template/
Or this plugin:
http://guff.szub.net/2005/07/21/post-templates-by-category/
So according to the Wordpress template hierarchy there is only one single.php, that cannot be separated by category (like the archive page for example.)
https://developer.wordpress.org/themes/basics/template-hierarchy/
So in this case I suggest you read the current category id in your single.php file and then adjust the content to your needs. You can use get_the_category() do do this (reference: https://developer.wordpress.org/reference/functions/get_the_category/) which will return you an array with categories. In my simple example I just pick the first category to do something:
$categories = get_the_category();
$category_id = $categories[0]->cat_ID;
if($category_id == 1) echo 'do something here';
Thanks for you help. I am done with it myself.
I create single.php for simple format and and in category result i didn't use the_permalink and call id of post and made a url like http://localhost/demo/page?id=$id
I've successfully created my post meta boxes, saved the data and I understand how to retrieve the data. Within the custom meta boxes I have a field for the page branding that will decide what color scheme per the product line we are featuring on that page.
I have a class per the color scheme that is triggered when the body has a class of the product line name appended to it. For example:
<body class="product-drinks">
OR
<body class="product-abcwidgets">
Depending on what product line is selected in the meta box for that post will determine what style sheet will be included.
For example if I chose "product-drinks" then the stylesheet included would be product-drinks.css.
Most of the meta box data I need to use within the loop but I also need to access the page branding mega field data outside of the loop. How would I grab this data if I need it outside of the loop?
I initially thought of placing some of the data in an array while in the loop as such and then referencing the $page_options array value in the body tag as such:
(outside of the loop in the header)
<body class="<?php echo $page_options['pageBranding'];?>
from within the loop"
$page_options = array(
'pageBranding' => get_post_meta($post_id, 'pageBranding', true),
'layout' => get_post_meta($post_id, 'pageLayout', true)
);
Am I doing this correctly or is there a better way of doing this? Or should I only reference the meta fields I need within the loop and then use global $wp_query; outside of the loop and get the post meta that way for the data I need for the body and stylesheets?
If you need the data outside the loop, I'd suggest using $wp_query or global $post.
<?php global $post; ?>
Then you can call it just as normal
<?php $samplemeta = get_post_meta($post->ID, "your_meta_name", true); ?>
I need to have a index page and it can pull out the title (with link), meta key description and meta key image (meta keys from custom fields) of its child pages, but only one level down.
e.g. I have a index page call WORK, and few child index PRINT, WEB. there will be some pages under PRINT and WEB as well, but I don't want them to be shown on the WORK index. I only need to have PRINT and WEB to be listed and with the meta keys.
Anybody can help please? many thanks!
Try putting:
query_posts("cat=1,-2,-3");
...Before the loop. Replace the numbers after cat= with the category IDs in question; using the negative symbol (such as "-2,-3") excludes a category.
To do this more programmatically:
$parent = 1; //catID of parent
$children = get_categories("child_of=".$parent);
foreach ($children as $child) {$excluded .= "-".$child->term_id.","; //add minus sign}
$excluded = rtrim($excluded, ","); //remove last comma
query_posts("cat=".$parent.",".$excluded);