Wordpress - duplicate search results listed? - php

The theme used is CookingPress v1.2. - http://bit.ly/zgCtE0
Problem:
If "test-blog-post" has 5 tags and is returned in search results, then it will be listed 5 times in the results list.
Question:
Is there something I can add to the code below to correct the search results from being duplicated? If not, is there any other code that might cause this?
Below is the code that renders the search results (from loop-search.php).
<?php if (!have_posts()) : ?>
// No Results
<?php endif; ?>
<?php while (have_posts()) : the_post(); ?>
// Show Posts
<?php endwhile; ?>

That is not the complete code. You have cut out the important stuff after "//Show Posts". Please post that code and please post the code that queries the database. I ask because I don't think that it is a WordPress native query. That is, I don't think vanilla Wordpress searches for tags at all. That means that this is a custom query or the developer has hooked into the WordPress query and altered it. Your question is not very answerable without that information.

Related

Wordpress, show or hide header on posts by post categories

Hi i have an wp website and i'm using avada theme. I want to hide header according to its category. For example if posts category is "A" then i dont want to show header on the top automatically. I know i can manually adjust that in the post settings on every posts, or set a layout and select that layout on the below but i dont want to deal that every time.
So i try a if statement in the archives.php.
its something like this:
<?php if(has_category( 'A' )) : ?> //i also try in_category and on_category
<?php else: ?>
<?php get_header(); ?>
<?php endif; ?>
but its probably for category page itself but not for posts.
Can anybody help with that?
Thanks :)
hi you can go with following links and follow the given solution it may worked..
here it show that how to hide specific c***ategorize header post*** ...
https://wpquestions.com/Remove_header_or_styling_from_specific_category_of_posts/11505

Wordpress single.php another get_post function destory the first function

I am trying to deal with two get_post functions, in single.php the first get_post function is the post from the wordpress, But after that I called get_post function to other post also to use both of them in the same page but after I call the first get_post ( the main post ) I get the only second data and cant reach the first data.
My code called to the second function ( The first is from wordpress post):
$main_post = get_field('main_post');
$main_p = get_post($main_post->ID);
Then I am trying to use the variable $post OR the_title() OR any other functions to get the first post and it always returning the info of the $main_p post
for example
get_the_title( get_post()->ID )
returns the $main_p post id and not the main post of the single.php
any soulutions ?
I may be wrong, but it seems to me that you are trying to post a different post format with normal post format?
I, myself use get_post_format() so it can be styled differently or have different options.
in single.php I use
<!-- checking if there are any blogposts to be shown using have_posts check which is a wordpress function-->
<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post(); ?> <!-- the correct syntax for while loop in wordpress to show all the blogposts -->
<?php get_template_part('content', get_post_format()); ?>
<?php endwhile; ?>
<?php else : //else stament if there aren't any posts (put inside if before endif)?>
<p><?php __('No Posts Found'); ?></p>
<?php endif; ?> <!-- stop checking for blog posts-->
</div><!-- /.blog-main -->
Inside functions.php I activated the post-formats inside wp_theme_setup() function
add_theme_support('post-formats', array('aside', 'gallery'));
In this case i activated the gallery and aside posts
That way I have 2 different post types on one page
like this
image from my theme blog page
Here is also a video tutorial on post formats from Traversy media
https://www.youtube.com/watch?v=CRa7eiqyiCM&list=PLc5p9nvpdwBlrNU0hr1f0kXPRkh0aGo1Q&index=7
The key reason why your post values are being overwritten, the additional get_post() declarations are overriding the default query. Now, the code in your pastebin is a pretty massive dog's breakfast, so a direct solution is a rather large undertaking (e.g. the indentation is all over the place, the code snippets are less than ideal regarding their readability, etc...). However, I can point you in the right direction for the solution.
When I pull content from another page on my WordPress sites, I avoid using get_post() in favour of declaring a fresh new WP_Query() (that's just my preference), following it up with a wp_reset_postdata() declaration.
Here's an example of multiple queries on a single template in the WordPress codex:
https://codex.wordpress.org/Class_Reference/WP_Query#Multiple_Loops
The key here is the wp_reset_postdata(). I'd recommend looking into it's purpose. It'll save you a lot of grief:
https://codex.wordpress.org/Function_Reference/wp_reset_postdata

WordPress Search results Programmatically

I want to include an automatic search results output on my 404 page based on the url that the user has type in.
The problem is that on the usual search.php file, wordpress gets the value from the URL parameter and uses the regular loop like so:
<?php while (have_posts()) : the_post(); ?>
<h1>Search Results</h1>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
How do I get it so that I can manually enter the search term in the code?
Much appreciated.
You want to automatically search with the value from url?
maybe you can show your similiar post with that value, or show them a recent post with search form , using search for your site and google.com.
may be this link could help Built Effective 404 Error Pages
Hope this help,

How to display comments on homepage when using Underscores (_s) Wordpress theme?

I have my blog set up in a way so that it always shows just one post per page, and I would like to display comments under the post on the homepage (so that posts don't have to be clicked for the relevant comments to display), but I'm getting a bit lost..
Of course, I tried looking for a possible solution, but all the similar problems were 3+ years old, and using different syntax.
I found this piece of code in the single.php file, and thought inserting it before the <?php endwhile; ?> in the loop would do the trick (as most of the solutions suggested) .. but it didn't.
<?php
if ( comments_open() || '0' != get_comments_number() ) :
comments_template();
endif;
?>
I think there needs to be something tweaked in this piece of code, but since I'm not a PHP programmer, I don't want to mess something up. Any suggestions?
Use the following piece of code wherever you want to display your comments on a template different than single.php:
<?php
global $withcomments;
$withcomments = true;
comments_template();
?>

How to have different logic for a page and a post within Wordpress?

I am modifying a wordpress template and need to slightly separate rendering logic for a post and a page, specifically in relation to how the date renders out. The problem is that I cannot find any code to do this, I am sure that it exists. Is there a variable that exists within wordpress that tells me whether the item being displayed is a page or a post?
In an ideal world it would look something like this:
<?php if (is_page()) : ?>
page logic
<?php else: ?>
post logic
Would appreciate any help!
Pages are a type of post, so get_post_type should return appropriately different values for pages vs normal blog posts.
I found this link: http://wordpress.org/support/topic/sidebar-logic-for-postblogroll-and-page-type which seemed to do the business for me.
The answer (copied straight from the page) was:
<?php if(is_singular($post)): ?>
Page Content
<?php else:?>
Post Content
<?php endif;?>

Categories