Auto-formatting in Wordpress posts only - wordpress

I'm using remove_filter( 'the_content', 'wpautop' ); in my theme in order to disable Wordpress auto-formatting that adds paragraphs everywhere and often messes up the layout.
However, one of my clients wants to be able to add blog posts on his own. He needs to be able to format his blog posts from the WYSIWYG editor, minimally to have paragraphs.
Is it possible to allow the auto-formatting in blog posts only, or in posts of a particular type or belonging to a particular category?
So far all examples and articles I found use the exact same code as the one listed above. I also checked the https://codex.wordpress.org/Template_Tags but didn't see anything there that could help me solve this.

So now that you've removed the regular wpautop filer, you could add your own in its place, and in it, only apply the wpautop only on posts of type "post":
add_filter( 'the_content', 'smart_autop' );
function smart_autop($content) {
$post = get_post();
if($post->post_type != 'post') return $content; // if not a post, leave $content untouched
return wpautop($content);
}
Hope this helps!

Related

pre_get_posts not limiting posts per pages

I have set up the following in my functions.php to raise the limit of posts being shown when doing a public search:
if ( ! is_admin() && $query->is_search) {
$query->set('posts_per_page', 20);
}
}
add_action( 'pre_get_posts', 'search_results_query' );
But however much I tried to modify it, I don't seem to have any effect. What am I missing?
You are missing part of your code but do you have $query set to pass through your function?
function search_results_query( $query ) {
I just tested your code and set it to 1 using the 2019 theme and it worked fine with the above.
Thank you, the problem was that the theme had already set the posts per pages. I managed to change the number of posts from the appearance menu. The page template had already theme actions and hooks and I have a limited understanding yet of it all, but I think that was what was interfering with my hook. Or I just simply didn't understand fully how to hook it up properly. Either way, I resolved it.

Cant get a basic "If Has Tag" Statement to work - Wordpress

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.

Wordpress: Different look for different posts

I've looked everywhere to work out how to do this, but I'm having no luck. I read up on Post Formats, but I don't think that's what I'm looking for.
All the posts on the frontpage are the same, but say, for features or reviews you can have an entirely different look compared to the "standard" single.php post.
Examples:
Homepage //
http:// ausdroid.net (All posts look the same)
Standard single post //
http://ausdroid.net/2013/02/03/samsung-galaxy-nexus-from-vodafone-receives-android-4-1-2-update
Different single post (for a review in this case) //
http://ausdroid.net/2013/02/02/huawei-ascend-d1-quad-review
Thanks!
Sample on using category to present different Singles:
single.php
<?php
if ( in_category( $foo ) )
require( 'single-foo.php' );
else
require( 'single-default.php' );
So you'll have the above simple code in the usual single.php, put your previous single.php to single-default.php, and create your "other" Single layout in single-foo.php.
$foo can be category ID or Slug.
in_category()
EDIT
You can modify this to work with Post Formats:
In functions.php:
add_theme_support( 'post-formats', array( 'aside', 'gallery' ) );
... then, change the if statement in my sample above to use has_post_format() instead.
has_post_format().
http://www.wpbeginner.com/wp-themes/create-custom-single-post-templates-for-specific-posts-or-sections-in-wordpress/
this explains it pretty well using " Single Post Template" plugin, if thats an option
You can try adding custom fields to the posts and check them in the single.php to show different layouts. See this video for and example of a plugin doing just that - youtube.com/watch?v=L3VXnryN9iY

How to list all items in custom taxonomy with a permalink in WordPress

Not sure how to word this. I've set up a custom post type in WordPress called Products. I also have two taxonomies linked to Products called Brands and Categories. When you create a product you can set to which Brand and Category the product belongs. It works perfectly when using the type as permalink to list everything within a type
http://sitename.com/products
but I can't seem to get the taxonomies working in the same way, ie. list all the items within a taxonomy (like an archive, I suppose):
http://sitename.com/brands
http://sitename.com/categories
Is this possible, and if so, what could I possibly be doing wrong? I would try and set up some rewrite rules in functions.php, but I don't know what the query string looks like if you want to list items in a taxonomy, and if this is even possible. If it helps, I'm using the plugins More Types and Ultimate Taxonomy Manager to create the types and taxonomies.
There's not really a way to do this yet. WP is moving this direction and with 3.1 is setting up a top-level for custom post types, but still nothing for taxonomies. It's klunky but I added a page called page-products.php and then put my list in there.
$toplevelterms = get_terms($taxonomyname, 'hide_empty=0&hierarchical=0&parent=0');
$termchildren = array();
foreach ($toplevelterms as $toplevelterm) {
$termchildren[] = $toplevelterm->term_id;
}
foreach ($termchildren as $child) {
$term = get_term_by('id', $child, $taxonomyname);
$write .= '<li>';
$write .= '' . $term->name . '';
if ($term->description) $write .= apply_filters('the_content', $term->description);
$write .= '</li>';
}
if ($write) echo '<ul class="clearfix">' . $write . '</ul>';
FYI the correct link for that plugin (I'm the author) is now http://www.get10up.com/plugins/simple-custom-post-type-archives-wordpress/
And it should really be done using the built in archives feature in 3.1 - the plugin was built for 3.0.
There is a Trac ticket for this, but it's not clear what would the most obvious thing to show on this page:
a list of all posts attached to any term of that taxonomy, or
a list of all terms of this taxonomy?
A similar question on the WordPress Stack Exchange asked for option 2, and your question can be interpreted both ways: when you say all items within a taxonomy, do you mean all terms of this taxonomy, or all posts that are linked to a term of this taxonomy?
Currently the main query (the Loop) that you can control with the query vars will always return posts, not terms. As said in the ticket, option 2 would require the Loop to return terms for this query, possibly causing compatibility issues for themes that don't have a template for it. Until that is implemented, David's answer is probably the way to go.
Thanks for the answers! I've found a plugin that does exactly what I wanted - after manually coding everything :/
http://www.thinkoomph.com/plugins-modules/wordpress-custom-post-type-archives/
Use the WP htaccess Control Plugin
http://wordpress.org/extend/plugins/wp-htaccess-control/
In the options , you can remove the base of taxonomies you select.
Worked for me, hope It works for you as well :)

How to filter Wordpress posts using a hook in a plugin?

I'm creating a Wordpress plugin and, being a newbie in the development on this platform, I'm stuck on this problem.
I'd like to have posts in the loop filtered by categories, defined by the user through an admin page. I would actually like to be able to modify query_post() parameters in the plugin, but the only trick I found is to re-run the query_post() with my user-defined criteria, thing that I would like to avoid.
Also, due to the plugin nature, I think it make no sense to modify the theme's template.
I'm sure the solution is evident, but can't find it!
I thought there was a nicer solution, but this is how I finally solved it:
add_filter ( 'query_vars', 'myplugin_filter_posts');
function myplugin_filter_posts( $content )
{
//WP's query handler
global $wp_query;
//The id of the category whose posts I'd like to show
$catId = 1;
$result = $wp_query->query( 'cat='.$catId );
return $content;
}
If you tips for a better solution, please share :)

Resources