search query doesn't work on individual post pages - wordpress - wordpress

From the main page and category page, my search form works just fine. Try to search for "post".
http://blog.papermusepress.com
It will bring up the results.
but if you try to do a search from within an individual post, it doesn't do the search.
try here: http://blog.papermusepress.com/my-second-post/ and search for post, it doesn't do the actual search
anybody have an idea why it would do this?
/single.php/
<?php get_header(); ?>
<div id="main">
<div id="primary">
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<div class="post-item">
<div class="title-tape">
<h2><?php the_title(); ?></h2>
<p class="meta">Posted by <?php the_author(); ?> on <?php the_date(); ?></p>
</div><!-- end title-tape -->
<?php the_content(); ?>
</div><!-- end post-item -->
<?php endwhile; ?>
<?php else : ?>
<p>We aren't sure what you are looking for..</p>
<?php endif; ?>
<div id="comments_template">
<?php comments_template(); ?>
</div><!-- end comments_template -->
</div> <!-- end primary -->
<?php get_sidebar(); ?>
</div> <!-- end main -->
</div><!-- end wrap -->
<?php get_footer(); ?>

I believe the problem is that the search query string is being appended to the full URL each time when it should only be added to the root URL. For example, if you are on http://blog.papermusepress.com/my-second-post/ the search is added to the end of that URL (instead of just http://blog.papermusepress.com/) which only allows that current page to be searched. This will keep the current template and bypass the search results page.
Check the form action in your searchform.php file. The opening form tag should look something like this:
<form method="get" id="searchform" action="<?php echo esc_url(home_url('/')); ?>">
If your form has an empty action, action="" , it will post back to itself. This can be useful, but is definitely not what you want for wordpress search.

Related

How can I retrieve an ACF field from a custom post type to the archive page of this CPT?

I create a Custom Post Type with ACF field inside. Everything works great, but when I want to create my archive-cpt.php page, I can't get my ACF field to be seen on this page.
Here is my archive page code :
<?php get_header(); ?>
<main role="main">
<!-- section -->
<section>
<h1><?php _e( 'Archives', 'trad' ); ?></h1>
<div class="container">
<div class="row">
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<div class="col-lg-4 mx-auto">
<!-- article -->
<h2 class="titre-extranet-article">
<?php the_title(); ?>
</h2>
<li>
<?php // display a sub field value
get_sub_field('titre'); ?></li>
</div>
<!-- /post thumbnail -->
<?php endwhile; ?>
<?php else: ?>
<!-- article -->
<article>
<h2><?php _e( 'Sorry, nothing to display.', 'trad' ); ?></h2>
</article>
<!-- /article -->
<?php endif; ?>
</div>
</div>
<?php get_template_part('pagination'); ?>
</section>
<!-- /section -->
</main>
<?php get_footer(); ?>
The sub field 'titre' doesn't appear on my archive page.
Where I am wrong ?
Thank you.
get_sub_field() is for a repeater field. So you need to get the parent first e.g.
if( have_rows('parent_field') ):
while ( have_rows('parent_field') ) : the_row();
$titre = get_sub_field('titre');
// Do something...
endwhile;
endif;
You can get that field just with get_field() if it is a single text field.
Try with get_field()
<?php echo get_field('titre'); ?>
And one more thing you forgot to add echo before the get_sub_field(). So try with the echo.
<?php echo get_sub_field( 'titre' ); ?>

How to get category name from URL and pass to a template

I'm using the following page template to display the posts in a single category and plan to format the first post differently than the others. This works as desired but I have the category_name hard coded in the template. I want to use this template for several different categories and would like to learn how to pass the category_name to the template from a link.
For example, the link to the desired page using the special template is http://wildcatweb.net/5th/ and '5th' is also the category_name. How do I tell the template to get the category_name from the URL and use it in the template?
<?php
/*
Template Name: pageAssignments
*/ ?>
<?php get_header(); ?>
<div class="small-12 large-8 columns" id="content" role="main">
<header>
<h1 class="entry-title"><?php the_title(); ?></h1>
</header>
<!-- show latest post FULL -->
<?php query_posts('showposts=1&category_name=5th'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="lastest-post">
<h2 class="pagetitle"><?php the_title(); ?></h2>
<?php the_content(); ?>
</div><!--close .latest-post -->
<?php endwhile; endif; ?><!-- end lastest post -->
<!-- show older post excerpts -->
<?php query_posts('showposts=5&offset=1&category_name=5th'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="older-post">
<h3 class="pagetitle"><?php the_title(); ?></h3>
<?php the_excerpt(); ?>
</div><!--.older-post -->
<?php endwhile; endif; ?><!-- end past-entry -->
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
try this :
$current_url = $_SERVER['PHP_SELF'];
$category_name = explode ('',$current_url);
$category_name = $category_name['3'];
query_posts('showposts=1&category_name='. $category_name);

custom wordpress theme echoes gallery short code

So i'm quite a beginner, trying to make a custom theme. In a page i want to have a gallery. Uploaded images, made a gallery all fine.
When I view the page it outputs only the shortcode:
[gallery orderby="post_date"]
my page.php file basically has:
<?php $content = get_page( $page_id ) ?>
<div id='content' class='shadow'>
<div id='innercontent'>
<!---page title-->
<?php echo "<h1>".$content->post_title."</h1><br>" ; ?>
<?php echo $content->post_content ?>
</div>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
I really don't understand how to get this to show correctly, any pointers would be greatly appreciated. Cheers, Matt
get_page returns the raw page data. There are a few ways to do what you want:
BAD WAY:
<?php $content = get_page( $page_id ) ?>
<div id='content' class='shadow'>
<div id='innercontent'>
<!---page title-->
<?php echo "<h1>".$content->post_title."</h1><br>" ; ?>
<?php echo do_shortcode($content->post_content); ?>
</div>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
do_shortcode() renders all registered shortcode that's found within a given string. In this case, your page's content will have all shortcode rendered before being written to the document. I say this is the "bad" way, only because it doesn't follow the usual Wordpress format. Which leads us to the:
BETTER WAY:
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<div id='content' class='shadow'>
<div id='innercontent'>
<!---page title-->
<h1><?php the_title(); ?></h1><br>
<?php the_content(); ?>
</div>
</div>
<?php endwhile;endif; ?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
This is what's called "The Loop". It is pretty much the standard for all Wordpress Themes in retrieving all Post or Page Data, as well as running queries against the database.
I would suggest getting to know it, as well as running Wordpress queries to modify the loop using WP Query. This is getting into a more complex area of Wordpress, but it will help you in the longrun with figuring out how to gather up all of the posts and pages you want to retrieve in your theme that aren't provided by Wordpress' globals.
Good luck.

WordPress: next_posts_link() doesn't work before page template loop

I have an archive page template that I've built that has a successful navigation at the end of the loop. However, now I want to add an additional navigation at the top. However, after I copied and pasted the exact code, the 'Previous Entries' displays and links correctly, but 'Next Entries' never shows up as it does in the bottom navigation.
In my page-archive.php file, this is gist of my code:
<div class="navigation top">
<?php next_posts_link('Next Entries') ?> <!-- Doesn't appear to show up! -->
<?php previous_posts_link('Previous Entries') ?>
</div>
<?php $page = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts("showposts=1&paged=$page");
while ( have_posts() ) : the_post() ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h3><a href="<?php the_permalink() ?>" ><?php the_title(); ?></a></h3>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<div class="navigation bottom">
<?php next_posts_link('Next Entries') ?>
<?php previous_posts_link('Previous Entries') ?>
</div>
I understand that a Page template is not the ideal method to create an archive page, so if there is a different method for producing a full customizable archive page, please inform me!
Currently, I'm using my home.php file to display static content, so the page-archive.php is a method to direct users to the actual "Blog" portion of the site.
Try to put this string:
<?php $page = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts("showposts=1&paged=$page");?>
to the very top of the page. You need to do that because next_posts_link and prev_posts_link calls use some global variables set by query_posts call.

Wordpress, custom page theme next/previos posts

I have a custom page template with code:
<?php
/*
Template Name: Featured
*/
get_header(); ?>
<div id="content_box">
<div id="content" class="posts">
<img src="http://www.dinneralovestory.com/wp-content/uploads/2010/04/favorites.png" alt="Favourites"/><br clear="all" /><br />
<?php
//The Query
$my_query = new WP_Query('category_name=favourites');
if ($my_query -> have_posts()) :
?>
<?php while ($my_query -> have_posts()) : $my_query -> the_post(); ?>
<div class="featured_box">
<div class="featured_thumb">
<?php the_post_thumbnail(); ?>
</div>
<div class="featured_content">
<span class="post_title"><?php the_title(); ?></span>
<?php the_excerpt(); ?>
</div>
</div>
<br clear="all" /><br />
<?php endwhile; ?>
<?php include (TEMPLATEPATH . '/navigation.php'); ?>
<?php else : ?>
<h2 class="page_header center">Not Found</h2>
<div class="entry">
<p class="center">Sorry, but you are looking for something that isn't here.</p>
</div>
<?php
endif;
wp_reset_query();
?>
</div>
<?php get_sidebar(); ?>
</div>
<?php get_footer(); ?>
The navigation.php file has the previous / next code (it works fine for the standard post pages and archive pages)
navigation.php:
<?php if (is_single()) : ?>
<div class="navigation">
<span class="previous"><?php previous_post_link('← %link') ?></span>
<span class="next"><?php next_post_link('%link →') ?></span>
</div>
<div class="clear whitespace"></div>
<?php else : ?>
<div class="navigation">
<div class="previous"><?php next_posts_link('← Previous Entries') ?></div>
<div class="next"><?php previous_posts_link('Next Entries →') ?></div>
</div>
<div class="clear flat"></div>
<?php endif; ?>
I have set the maximum posts per page to 5, but the page using this theme template wont show the links. Any ideas? What code can i use to get them.
Thankyou
previous_post_link and next_post_link and the like don't make any sense for Pages. Pages are not ordered by date and time, they are hierarchical. In other words, there is no "next" Page. That doesn't make any sense in that respect.
Your base problem is that you're using a custom Page template to display Posts from a specific Category. This is the wrong way to do it, WordPress has perfectly valid category archives already, which work fine and which expect to display Posts properly.
Long story short: You will never get your Page Template approach to work 100% correctly. It just doesn't work that way. Posts were never meant to be displayed on Pages, and trying to do so only leads to things not working.

Resources