I'm using Wordpress page templates to pull posts with certain categories into their own page. My problem is that they are also showing on the home page. I know how to hide categories from the home page entirely, but I'd like a toggle so that if it's checked, it will post to home page and sub-page and if it's not checked, it will display only on the sub-page. Does that make sense?
This code is on the "Awards" template and pulls in posts containing the category slug "awards".
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args= array(
'category_name' => 'awards', // Change these category SLUGS to suit your use.
'paged' => $paged
);
query_posts($args);?>
The "awards" category is then blocked from the home page using a query_posts tag:
<?php if ( is_home() ){
query_posts( 'cat=-5' ); //-5 is the category id for 'awards'
}?>
I tried to get around this by also tagging the post with a category that isn't blocked from the home page, but it apparently doesn't care that it's tagged with something else.
So my conundrum is that I would like a simple option (hah) that allows me to pick and choose if it belongs on the home page or not, while still having them all show on this other page.
Well.. in the middle of writing this, I was inspired and sure enough I found the simple solution. 'Uncategorized' is the default category and has the id of 1. On the index page, I simply added the 'query_posts( 'cat=1' );' underneath the bit of code that blocks the other categories. This way, I can tag it for the category I want on one of the sub pages, but also for ones that I want to show on the homepage. Here is the code as it is on my index.php:
<?php if ( is_home() ){
query_posts( 'cat=-5' );
query_posts( 'cat=1' );
}?>
So if I want it on the home page AND the subpage, I'll categorize it as 'awards' AND 'Uncategorized' (or whatever you choose to be the homepage category). If I want it only on the subpage, I just choose that category. This works because the 'query_posts( 'cat=1' );' is written below the 'query_posts( 'cat=-5' );' part, so it's happening after the block which tells the server to display it after all.
Is it an option to use a Wordpress Plugin?
If so, you can use WP Hide Post, which enables a feature to hide certain posts to show on the frontpage.
Here is the link to the resource:
http://wordpress.org/plugins/wp-hide-post/
Related
I'm simply placing a small snippet of code on a woocommerce product page of my Wordpress website, to display an image "IF" the product has a specific TAG in place.
The code itself seems like it would be simple enough, and ive googled the heck out of this and tried many variations, with no luck on any of them.
<?php if( has_tag( 'tagnamehere') ) : ?> <div>my content</div> <?php endif; ?>
Also tried this:
<?php if( has_tag( 'tagnamehere' ) ){ echo '<div>my content</div>'; } ?>
Thats didnt work either.
basically, i just want it to look at the TAG of the product, and if the TAG exists, simply show the image (div). Nothing seems to work.
WordPress actually uses what's called Taxonomies and Terms.
Taxonomies are basically a grouping of Posts or Custom Post Types (like 'Category' or 'Post Tag', and Terms are basically the individual grouping names 'Featured Posts', etc.
WooCommerce basically registers a Custom Post Type called product, and also a Taxonomy called product_tag. Note, this is different than the default Tags on Posts.
Effectively this means you'll need to check if the term 'tagnamehere' exists in the product_tag taxonomy, the easiest way would be with the has_term() function. This is basically like the "Custom Post Type with Custom Category (aka Taxonomy)" version of has_tag()
if( has_term( 'tagnamehere', 'product_tag' ) ){
echo '<div>my content</div>';
}
Also to address your original "two versions" of code - The curly brace or alternative-syntax if statements work identically, and are mostly up to style/preference.
I'm creating my first wordpress theme, and I've looked around on google, and the wordpress codex for an answer to my question, but I can't seem to find exactly what I'm looking for, or couldn't get it working.
What I'm trying to do, or trying to figure out, is how I can make it so a certain category has a certain design.
So if I wanted to make an index.php for any music videos in "www.domain.com/music/trash/drake-song.mp4.html"
the trash category, it'd have its own design, but songs in
"www.domain.com/music/good-music/coldplay-viva-la-vida.mp4.html"
the good-music category, I want it to look pretty much completely different. I've tried using something similar inside my header.php to this;
<?php
if( is_tag( 'good-music' ) ):
$my_classes = array( 'good-class', 'good-class-two' );
else:
$my_classes = array( 'not-good-class' );
endif;_
?>
but it seemed to simply change the category page.
"www.domain.com/categories/good-music"
Anyone know what I could be doing wrong? I know basic html/css/php/javascript, new to creating a WordPress theme, and can't seem to get this working..
Also:
Using XAMPP to host locally, using Friendly URL's, properly configured
In order to generate category-specific markup for a single post layout, you can put code like the below in your single.php file after the call of get_header().
Please note that this checks for your category based upon category slug. So if your category slug (the url version of your category) does not match the first arguments in the in_array() function call below, then you should change the argument to match the slug for your category.
<?php
/* Start the Loop */
while ( have_posts() ) : the_post();
?>
<?php
$categories = get_the_category();
$catSlugs = array();
foreach ($categories as $category){
$catSlugs[] = $category->slug;
}
if (in_array('good-music',$catSlugs)){
$post_cat = 'good-music';
} else {
$post_cat = 'not-good-music';
}
get_template_part( 'template-parts/post/content', $post_cat );
?>
<?php
endwhile; // End of the loop.
?>
It is important that this code only appear where there is a query to loop against.
Specifically, the file single.php in your theme should be the default file for displaying a single post, regardless of category. When you navigate to the url of a single post, this layout should be triggered.
As part of that triggering, a wordpress query of just that post is returned to be looped through.
Then, the code from while ( have_posts() ) : the_post(); until endwhile; will run one time, because there is a single post to be processed.
If there were more than one post, such as on a category page or on your default post listing page, then the code inside of that while loop would run as many times as there are posts in the query for that layout.
If you were to place the code in the header, it won't work because the header is prepared independently of the loop that runs on this page.
You could run a custom WP_query() in the header, but that is rarely a good way to handle site content.
In this situation it would not be appropriate, because you are customizing content of existing posts, and only differentiating based upon category.
So, just use the standard layouts files with custom layout parts.
I stripped out the divs in the loop below, because you may or may not be using bootstrap.
After this code is placed on your page, you would create files called content-good-music.php and content-not-good-music.php in YOUR_THEME_DIR/template-parts/post/ directory. The key is that you would add whatever your category slug is to the end of your
These files will contain the unique markup for these kinds of posts. You can also use a similar logic in your post listing loop to give the listed posts for each category their own unique php files.
Here's some get_template_part() documentation.
https://developer.wordpress.org/reference/functions/get_template_part/
In one theme (FloZo) I've seen a nice functionality and I would like to learn how to make something similar. It's about a portfolio page, take a look at the demo "Our Work section".
You create a page and give it a template (Work archive) via Pages menu - more or less understood
You don't add any pictures there!
In your Dashboard you have a nice "work" section, where you can choose "make a new work item" - you add pictures with titles and descriptions there. - This is the big trick!
Now: my newbie idea on how it works:
The template is just the "body" of the page with the title
The dashboard "work" section must be doing something like this:
When you post a work item, it pastes/appends the whole item code into your page (like an item template code) with your specific image and text. It also creates an item-specific page (it's where you end up after clicking on an item).
My question is: is there any slightest possibility to add such a functionality to a Wordpress theme?
Here is how I see it:
The page template, 'Work Archive' has the loop that is displaying posts of 'Work' post type.
So to achieve this first you have to add the custom post type of your liking, and then in the page template add a custom loop to display these:
<?php
$args = array(
'post_type' => 'your_post_type',
'posts_per_page' => -1 //or whatever you choose
);
$work_query = new WP_Query($args);
if ($work_query->have_posts()) : while ($work_query->have_posts()) : $work_query -> the_post(); ?>
<!-- loop content goes here -->
<?php
endwhile;
endif;
wp_reset_postdata(); // always reset post data after custom loops
?>
If you need more info pleae don't hesitate to ask.
Happy coding!
I have a News page that displays all the posts within the "News" Category. This category has sub-categories such as "Merchandise, Music, Events" ect.
I am aiming to remove comments from ALL News/Sub-category posts but only display them with the "Blog" Category posts.
Right now I have my single.php set up so posts with the "Gallery" post_format structure are displayed differently.
Here is the single.php file//
http://pastebin.com/YNf3TxT6
I am wondering what I have to fix in order to get this working...
Edit: For future viewers, here is the updated paste from the conversation below for a single.php that will only show the comments template if the post is in the "Blog" Category: pastebin.com/y9ZtCN5U
Assuming you put your Blog posts on a page separate from your news posts, you should be able to use different templates based on category.
http://codex.wordpress.org/Category_Templates
So, you could make a category-blog.php template file that doesn't include the comments code.
If all of your categories are being listed on the same page, use this instead of the in_category stuff on line 50.
<?php
foreach (get_the_category() as $category) {
if ( $category->name == 'Blog' ) {
comments_template();
}
}
?>
Not 100% sure that will work, but try it out and let me know what happens.
I am working on a site with WordPress for a client who wants to showcase her products online, but does not want to sell them over the web. I have a simple photo gallery set up with the NextGEN Gallery plugin. Upon clicking the thumbnails in the gallery view, instead of simply showing them in a lightbox or their own page, I would like to add a "details" page for each photo - kind of like the individual product pages on a shopping site, but without any of the shopping functionality. Would it be possible to do this using NextGEN gallery or another plugin, or do I have to do it from scratch (and how would I do that)?
Because NextGen doesn't actually create a WordPress 'Page' to link the single image to, you may be better off using the built-in WordPress Galleries (You may of seen the [gallery] shortcode). When you upload an image to a page or post, WordPress stores that image as a post in the wp_posts table, alongside all your other posts and pages.. with a post_type of 'attachment', and adds it to a 'Gallery' for the post or page. You can manage the current gallery for any page or post by clicking the 'Add Media' icon above the text editor. While the [gallery] shortcode is quite limited, you can use code in your theme to display the gallery and images anyway you want.
If it was me, I would make a 'Products' page and individual 'Product' pages as child pages of 'Products' (or if you want more flexibility, use a custom post type for the products) and then upload all the product photos into the 'Product' page galleries (creating a gallery per product) - and setting the primary image as the 'Featured Image' for each product page. Then in your 'Products' page template (page-products.php) I would make a loop to display all the individual Product page's featured images.. something like..
<?php
//Add this to your 'Products' page temaplte, page-products.php
$products = new WP_Query(array(
'post_parent' => $post->ID,
'posts_per_page' => -1
));
while ($products->have_posts()) : $products->the_post() ; ?>
<a href="<?php the_permalink() ?>" title="<?php the_title() ?>">
<?php the_post_thumbnail() ?>
</a>
<?php endwhile;
?>
That will create a list of the Product featured images, and you can use CSS to adjust the display - and each featured image will link to the individual product page.
Then in your individual Product pages (you can use a page template to target all of them), you can display the individual product gallery (like detail shots of the product) using get_children() or use the_post_thumbnail() again to display a larger close-up version of the primary photo. And of course, then you can use the post content of your individual product page to display the info text for each photo, like so:
<?php
/* Template Name: Individual Product */
while (have_posts()) : the_post();
the_post_thumbnail();
the_content();
endwhile;
?>
To take it another step further, you could add all sorts of other data about the individual product with custom meta boxes, and display them on the side of the page or something.. like 'Product Size', 'Color', 'Foo', 'Bar'.. anything really. Hope that helps!